Sha256: 5615051c833092a83d2c3efb85ef8d02a166beb8b9600051493e42e14ec8b80b

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

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

module TwhereHelperMethods
  def hashtags(str)
    str.scan(/#([A-Za-z0-9]+)/).flatten.uniq
  end
end

class Twhere
  include TwhereHelperMethods

  def initialize(locations, template, twitter_username, twitter_password, tweets = nil)
    @locations           = locations
    @template            = template
    @twitter_username    = twitter_username
    @twitter_password    = twitter_password
    @tweets              = tweets || []
  end

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

    # 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

    # group tweets by location
    @located_tweets = {}
    @tweets.each do |t|
      located_hashtags = hashtags(t[:text]) & @locations.keys
      located_hashtags.each do |ht|
        @located_tweets[ht] ||= []
        @located_tweets[ht] << t
      end
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sarnesjo-twhere-0.0.12 lib/twhere.rb