Sha256: c3097ad968337bd3f0676621a6f1bd46623871d2ac8c0f77138eefe9bd6049a5

Contents?: true

Size: 1.05 KB

Versions: 7

Compression:

Stored size: 1.05 KB

Contents

# NOTE: It's important to cache duped objects
module Earthquake
  module Twitter
    attr_reader :twitter
  end

  init do
    @twitter = TwitterOAuth::Client.new(config.slice(:consumer_key, :consumer_secret, :token, :secret))

    output do |item|
      next if item["text"].nil? || item["_disable_cache"]
      item = item.dup
      item.keys.select { |key| key =~ /^_/ }.each { |key| item.delete(key) } # remote optional data like "_stream", "_highlights"
      cache_key = "status:#{item["id"]}"
      cache.write(cache_key, item) unless cache.exist?(cache_key)
    end
  end

  once do
    class ::TwitterOAuth::Client
      [:status, :info].each do |m|
        define_method("#{m}_with_cache") do |*args|
          key = "#{m}:#{args.join(',')}"
          if result = Earthquake.cache.read(key)
            result.dup
          else
            result = __send__(:"#{m}_without_cache", *args)
            Earthquake.cache.write(key, result.dup)
            result
          end
        end
        alias_method_chain m, :cache
      end
    end
  end

  extend Twitter
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
earthquake-0.7.0 lib/earthquake/twitter.rb
earthquake-0.6.12 lib/earthquake/twitter.rb
earthquake-0.6.11 lib/earthquake/twitter.rb
earthquake-0.6.10 lib/earthquake/twitter.rb
earthquake-0.6.9 lib/earthquake/twitter.rb
earthquake-0.6.8 lib/earthquake/twitter.rb
earthquake-0.6.7 lib/earthquake/twitter.rb