Class: Twitter2Campfire

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter2campfire.rb,
lib/twitter2campfire/cli.rb

Defined Under Namespace

Classes: CLI

Instance Attribute Summary collapse

Instance Method Summary collapse

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', options = {})
  self.feed = feed
  self.campfire = campfire
  self.room = campfire.find_room_by_name room
  self.cachefile = cachefile
  self.options = options
end

Instance Attribute Details

#cachefileObject

Returns the value of attribute cachefile.



12
13
14
# File 'lib/twitter2campfire.rb', line 12

def cachefile
  @cachefile
end

#campfireObject

Returns the value of attribute campfire.



12
13
14
# File 'lib/twitter2campfire.rb', line 12

def campfire
  @campfire
end

#feedObject

Returns the value of attribute feed.



12
13
14
# File 'lib/twitter2campfire.rb', line 12

def feed
  @feed
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/twitter2campfire.rb', line 12

def options
  @options
end

#roomObject

Returns the value of attribute room.



12
13
14
# File 'lib/twitter2campfire.rb', line 12

def room
  @room
end

Instance Method Details

#archive_fileObject



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_checksumsObject



52
53
54
# File 'lib/twitter2campfire.rb', line 52

def archived_checksums
  archive_file.split("\n")
end

#checksumsObject



48
49
50
# File 'lib/twitter2campfire.rb', line 48

def checksums
  entries.map { |e| e.checksum }.to_a
end

#coderObject



77
78
79
# File 'lib/twitter2campfire.rb', line 77

def coder
  HTMLEntities.new
end

#entriesObject



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_tweetObject



39
40
41
# File 'lib/twitter2campfire.rb', line 39

def latest_tweet
  entries.first
end

#new_archive_contentsObject



69
70
71
# File 'lib/twitter2campfire.rb', line 69

def new_archive_contents
  "#{new_checksums.join("\n")}"
end

#new_checksumsObject



56
57
58
# File 'lib/twitter2campfire.rb', line 56

def new_checksums
  checksums.flatten.uniq[0,1000]
end

#postsObject



73
74
75
# File 'lib/twitter2campfire.rb', line 73

def posts
  entries.reject { |e| archived_checksums.include?(e.checksum) }
end

#publish_entriesObject



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 options[:twicture]
      room.speak post.twicture
    else
      room.speak "#{coder.decode(post.from)}: #{coder.decode(post.text)} #{post.link}"
    end
  end
  save_latest
end

#raw_feedObject



22
23
24
# File 'lib/twitter2campfire.rb', line 22

def raw_feed
  @doc ||= Hpricot(rio(feed) > (string ||= ""))
end

#save_latestObject



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