Sha256: cee0c4574942cbf1c051e68e22cde9661b95a7117ef4817e9d28de0d29427470

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

require 'rubygems'
require 'erb'
require 'twitter'

class Twhere
  def initialize(config_file)
    config = YAML::load(File.open(config_file))
    @locations_file       = config[:twhere][:locations_file]
    @template_file        = config[:twhere][:template_file]
    @tweets_file          = config[:twhere][:tweets_file]
    @twitter_username     = config[:twitter][:username]
    @twitter_password     = config[:twitter][:password]
    @google_maps_api_key  = config[:google_maps][:api_key]
  end

  def result
    # load known locations
    @locations = YAML::load(File.open(@locations_file))

    # create Twitter proxy object
    twitter = Twitter::Base.new(Twitter::HTTPAuth.new(@twitter_username, @twitter_password))

    # load saved tweets (or don't)
    tweets = []
    begin
      tweets = YAML::load(File.open(@tweets_file)) if @tweets_file
    rescue Errno::ENOENT
    end

    # add new tweets to collection
    twitter.user_timeline.each do |t|
      # we only care about these variables
      h = {:twitter_id => t.id, :text => t.text, :created_at => t.created_at}
      # this appears to be necessary since neither Array#| nor Array#uniq works as expected
      tweets << h unless tweets.include? h
    end

    # save tweets to file
    File.open(@tweets_file, 'w') { |f| f.puts tweets.to_yaml } if @tweets_file

    # group tweets by location
    @located_tweets = {}
    tweets.each do |t|
      md = t[:text].match(/#([a-z]+)/)
      if md and @locations.keys.include? md[1]
        @located_tweets[md[1]] ||= []
        @located_tweets[md[1]] << t
      end
    end

    # open template file and process with ERB
    ERB.new(File.read(@template_file)).result(binding)
  end

  def run
    puts self.result
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sarnesjo-twhere-0.0.4 lib/twhere.rb
sarnesjo-twhere-0.0.5 lib/twhere.rb