public
Description: This contains various plugins for Feather
Clone URL: git://github.com/eldiablo/feather-plugins.git
Click here to lend your support to: feather-plugins and make a donation at www.pledgie.com !
eldiablo (author)
Fri Apr 25 18:22:31 -0700 2008
commit  7178168c37f4185eb9f33df859c20fc69e92381b
tree    844f52e0dbc6c784e3b20c888bfd47195e46e6c0
parent  d243741e2970883f36c1cf7b871a00a727df0b5b
feather-plugins / feather-twitter / models / twitter_setting.rb
100644 53 lines (49 sloc) 2.003 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class TwitterSetting < DataMapper::Base
  property :username, :string
  property :ignore_replies, :boolean, :nullable => false, :default => true
  property :last_scan_at, :datetime
  
  after_update :rescan
  
  ##
  # This rescans for new Tweets, if the last scan time was more than 30 minutes ago
  def rescan
    scan if self.last_scan_at.nil? || ((self.last_scan_at.to_time + (60 * 30)) < Time.now)
    true
  end
 
  ##
  # This scans for new Tweets
  def scan
    # Ensure we have valid settings
    unless self.username.nil? || self.username == ""
      # Grab Twitter statuses as xml
      doc = Hpricot(Net::HTTP.get(URI.parse("http://twitter.com/statuses/user_timeline/#{self.username}.xml")))
      statuses = (doc/"statuses"/"status")
      # Loop through statuses
      statuses.each do |status|
        # Ensure it's unique, and that we don't already have it
        if Tweet.first(:twitter_id => (status/"id").first.innerText).nil?
          # Save the new tweet
          tweet = Tweet.new
          tweet.twitter_id = (status/"id").first.innerText
          tweet.text = (status/"text").first.innerText
          tweet.source = (status/"source").first.innerText
          tweet.in_reply_to = ((status/"in_reply_to").first.nil? || (status/"in_reply_to").first.innerText == "") ? nil : (status/"in_reply_to").first.innerText
          tweet.username = (status/"user"/"screen_name").first.innerText
          tweet.published_at = DateTime.parse((status/"created_at").first.innerText)
          tweet.created_at = DateTime.now
          tweet.save
        end
      end
      # Set the last scan time on the settings
      self.last_scan_at = DateTime.now
      self.save
    end
    true
  end
 
  ##
  # This returns the current settings, creating them if they aren't already present
  def self.current
    twitter_settings = TwitterSetting.first
    twitter_settings = TwitterSetting.create(:username => nil, :ignore_replies => true) if twitter_settings.nil?
    twitter_settings
  end
end