Sha256: 370c9e45e4a1390b7c4e9fccb3276e251596e6ab52defda559400f1718339572

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

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

module TwhereHelperMethods
  def hashtag(str)
    md = str.match(/#([^#\s]+)/) and md[1]
  end
end

class Twhere
  include TwhereHelperMethods

  def initialize(locations, template, twitter_username, twitter_password, google_maps_api_key, tweets = nil)
    @locations           = locations
    @template            = template
    @twitter_username    = twitter_username
    @twitter_password    = twitter_password
    @google_maps_api_key = google_maps_api_key
    @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|
      ht = hashtag(t[:text])
      if ht and @locations.keys.include? 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.7 lib/twhere.rb