# Always be on the lookout for duplicated code. If you find yourself doing
# asset.incomings.find(:all, :limit => 10) in multiple places, then perhaps you need to
# pull that into a method of its own. Given the use of the magic number "10", it might
# not be a bad idea to do so anyway.
class Asset < ActiveRecord::Base
has_many :incomings do
def recent(count=10)
find(:all, :limit => count)
end
end
end
# Then, you just have to do @asset.incomings.recent to get the first 10 items.
#!/usr/bin/env ruby
require 'rubygems'
require 'twitter'
require 'net/http'
require 'fileutils'
require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"
COCOA_DIALOG = ENV['TM_SUPPORT_PATH'] + "/bin/CocoaDialog"
CACHEDIR = ENV['HOME'] + '/Library/Caches/TwitterMonitor'
FileUtils.mkdir_p CACHEDIR unless File.exist?(CACHEDIR)
template = <<EOF
# .twitter
#
# Please fill in fields like this:
#
# email: bla@bla.com (username also works)
# password: secret
#
email:
password:
EOF
# ensure config file for twitter gem exists
begin
config = YAML::load open(ENV['HOME'] + "/.twitter")
rescue
open(ENV["HOME"] + '/.twitter','w').write(template)
config = YAML::load open(ENV['HOME'] + "/.twitter")
end
# make sure there's actually account information in there
if config == false or config.nil? or config['email'].nil? or config['password'].nil?
res = %x{"#{COCOA_DIALOG}" ok-msgbox --no-newline --float \
--text "Please edit ~/.twitter to include your twitter email and password" \
--informative-text "Press OK to edit your ~/.twitter file in a new tab."}
TextMate.exit_show_tool_tip("Cancelled!") if res == "2"
`cygstart "txmt://open?url=file://#{ENV['HOME']}/.twitter"`
end
# attempt to retrieve statuses from friend timeline
begin
statuses = Twitter::Base.new(config['email'], config['password']).timeline
rescue
success = false
end
isEven = true
bgStyle = ""
statuses.select do |status|
# ensure messages are suitable for display
next if status.text.nil? || status.user.name.nil?
status.text.gsub!(/"/, "'")
#p status
# try to find the most likely icon for this user
icon = Dir.glob("#{CACHEDIR}/#{status.user.screen_name}.{gif,jpeg,jpg,png}").first
# download and cache icons if nonexistant or over a month old
unless icon and File.mtime(icon) > Time.now - 2629743.83
image = Net::HTTP.get(URI.parse(status.user.profile_image_url))
icon = CACHEDIR + "/" + status.user.screen_name + "." + status.user.profile_image_url.match(/\.(\w+)\?/)[1]
icf = open(icon, "w")
icf.write(image)
icf.close
end
winicon = `cygpath -w #{icon}` # need to leave this out for TextMate.
(isEven = !isEven) ? bgStyle = "background-color:#EEEEFF;" : bgStyle = ""
puts "<p style='font-family:Helvetica,Arial,sans-serif; #{bgStyle}'><img src='file://#{winicon}' style='float:left' />#{status.user.name} (#{status.user.screen_name}):<br />#{status.text}</p>"
end
class ApplicationController < ActionController::Base
exempt_from_layout('iphone_html.erb')
before_filter :check_iphone
protected
def iphone?
request.user_agent.include?('iPhone')
end
def check_iphone
if iphone?
request.parameters[:format] = 'iphone_html'
end
end
end
class DashboardController < ApplicationController
def index
@top_movies = Movie.top_movies
@movie = @top_movies.first
respond_to do |format|
format.html # index.html.erb
format.iphone_html #index.iphone_html.erb
end
end
end