Sha256: 1416ae486ee8f62cb8aeb3c1515f39c405e75f6d0fe6ee867217a5c7f27a5ce5

Contents?: true

Size: 1.23 KB

Versions: 17

Compression:

Stored size: 1.23 KB

Contents

# Create custom parser
class TwitterSearchParser < Faraday::Response::Middleware
  METADATA_KEYS = [:completed_in, :max_id, :max_id_str, :next_page, :page, :query, :refresh_url, :results_per_page, :since_id, :since_id_str]

  def on_complete(env)
    json = MultiJson.load(env[:body], :symbolize_keys => true)
    data = json.delete(:results)
    errors = [json.delete(:error)].compact
    env[:body] = {
      :data => data,
      :errors => errors,
      :metadata => json
    }
  end
end

class MyCache < Hash
  def read(key)
    if cached = self[key]
      Marshal.load(cached)
    end
  end

  def write(key, data)
    self[key] = Marshal.dump(data)
  end

  def fetch(key)
    read(key) || yield.tap { |data| write(key, data) }
  end
end

$cache = MyCache.new

# Initialize API
Her::API.setup :url => "http://search.twitter.com" do |connection|
  connection.use Faraday::Request::UrlEncoded
  connection.use FaradayMiddleware::Caching, $cache
  connection.use TwitterSearchParser
  connection.use Faraday::Adapter::NetHttp
end

# Define classes
class Tweet
  include Her::Model

  def self.search(query, attrs={})
    get("/search.json", attrs.merge(:q => query))
  end
end

get "/" do
  @tweets = Tweet.search("justin bieber", :rpp => 30)
  haml :index
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
her-0.5.3 examples/twitter-search/app.rb
her-0.5.2 examples/twitter-search/app.rb
her-0.5.1 examples/twitter-search/app.rb
her-0.5 examples/twitter-search/app.rb
extended_her-0.5 examples/twitter-search/app.rb
her-0.4.1 examples/twitter-search/app.rb
her-0.4 examples/twitter-search/app.rb
her-0.3.8 examples/twitter-search/app.rb
her-0.3.7 examples/twitter-search/app.rb
her-0.3.6 examples/twitter-search/app.rb
her-0.3.5 examples/twitter-search/app.rb
her-0.3.4 examples/twitter-search/app.rb
her-0.3.3 examples/twitter-search/app.rb
her-0.3.2 examples/twitter-search/app.rb
her-0.3.1 examples/twitter-search/app.rb
her-0.3 examples/twitter-search/app.rb
her-0.2.6 examples/twitter-search/app.rb