Class: Twitter2Campfire
- Inherits:
-
Object
- Object
- Twitter2Campfire
- Defined in:
- lib/twitter2campfire.rb,
lib/twitter2campfire/cli.rb
Defined Under Namespace
Classes: CLI
Instance Attribute Summary collapse
-
#cachefile ⇒ Object
Returns the value of attribute cachefile.
-
#campfire ⇒ Object
Returns the value of attribute campfire.
-
#feed ⇒ Object
Returns the value of attribute feed.
-
#options ⇒ Object
Returns the value of attribute options.
-
#room ⇒ Object
Returns the value of attribute room.
Instance Method Summary collapse
- #archive_file ⇒ Object
- #archived_checksums ⇒ Object
- #checksums ⇒ Object
- #coder ⇒ Object
- #entries ⇒ Object
-
#initialize(feed, campfire, room, cachefile = 'archived_latest.txt', options = {}) ⇒ Twitter2Campfire
constructor
A new instance of Twitter2Campfire.
- #latest_tweet ⇒ Object
- #new_archive_contents ⇒ Object
- #new_checksums ⇒ Object
- #posts ⇒ Object
- #publish_entries ⇒ Object
- #raw_feed ⇒ Object
- #save_latest ⇒ Object
Constructor Details
#initialize(feed, campfire, room, cachefile = 'archived_latest.txt', options = {}) ⇒ Twitter2Campfire
Returns a new instance of Twitter2Campfire.
14 15 16 17 18 19 20 |
# File 'lib/twitter2campfire.rb', line 14 def initialize(feed,campfire,room, cachefile = 'archived_latest.txt', = {}) self.feed = feed self.campfire = campfire self.room = campfire.find_room_by_name room self.cachefile = cachefile self. = end |
Instance Attribute Details
#cachefile ⇒ Object
Returns the value of attribute cachefile.
12 13 14 |
# File 'lib/twitter2campfire.rb', line 12 def cachefile @cachefile end |
#campfire ⇒ Object
Returns the value of attribute campfire.
12 13 14 |
# File 'lib/twitter2campfire.rb', line 12 def campfire @campfire end |
#feed ⇒ Object
Returns the value of attribute feed.
12 13 14 |
# File 'lib/twitter2campfire.rb', line 12 def feed @feed end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/twitter2campfire.rb', line 12 def @options end |
#room ⇒ Object
Returns the value of attribute room.
12 13 14 |
# File 'lib/twitter2campfire.rb', line 12 def room @room end |
Instance Method Details
#archive_file ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/twitter2campfire.rb', line 60 def archive_file begin `touch #{cachefile}` unless File.exist?(cachefile) return File.read(cachefile) #rescue # '' end end |
#archived_checksums ⇒ Object
52 53 54 |
# File 'lib/twitter2campfire.rb', line 52 def archived_checksums archive_file.split("\n") end |
#checksums ⇒ Object
48 49 50 |
# File 'lib/twitter2campfire.rb', line 48 def checksums entries.map { |e| e.checksum }.to_a end |
#coder ⇒ Object
77 78 79 |
# File 'lib/twitter2campfire.rb', line 77 def coder HTMLEntities.new end |
#entries ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/twitter2campfire.rb', line 26 def entries (raw_feed/'entry').map do |e| OpenStruct.new( :from => (e/'name').inner_html, :text => (e/'title').inner_html, :link => (e/'link[@rel=alternate]').first['href'], :checksum => Digest::SHA1.hexdigest((e/'title').inner_html), :date => Time.parse((e/'updated').inner_html), :twicture => "http://twictur.es/i/#{(e/'id').inner_html.split(':').last}.gif" ) end end |
#latest_tweet ⇒ Object
39 40 41 |
# File 'lib/twitter2campfire.rb', line 39 def latest_tweet entries.first end |
#new_archive_contents ⇒ Object
69 70 71 |
# File 'lib/twitter2campfire.rb', line 69 def new_archive_contents "#{new_checksums.join("\n")}" end |
#new_checksums ⇒ Object
56 57 58 |
# File 'lib/twitter2campfire.rb', line 56 def new_checksums checksums.flatten.uniq[0,1000] end |
#posts ⇒ Object
73 74 75 |
# File 'lib/twitter2campfire.rb', line 73 def posts entries.reject { |e| archived_checksums.include?(e.checksum) } end |
#publish_entries ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/twitter2campfire.rb', line 81 def publish_entries posts.reverse.each do |post| if [:twicture] room.speak post.twicture else room.speak "#{coder.decode(post.from)}: #{coder.decode(post.text)} #{post.link}" end end save_latest end |
#raw_feed ⇒ Object
22 23 24 |
# File 'lib/twitter2campfire.rb', line 22 def raw_feed @doc ||= Hpricot(rio(feed) > (string ||= "")) end |
#save_latest ⇒ Object
43 44 45 46 |
# File 'lib/twitter2campfire.rb', line 43 def save_latest f = File.exist?(cachefile)? File.open(cachefile, 'a') : File.new(cachefile, 'w') f.write("\n#{new_archive_contents}") end |