Skip to content

Instantly share code, notes, and snippets.

@cupakromer
Created June 9, 2012 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cupakromer/2901020 to your computer and use it in GitHub Desktop.
Save cupakromer/2901020 to your computer and use it in GitHub Desktop.
module Fetcher
class Base
# ... other code ...
def uri
@cue
end
def options
{}
end
def fetch
send(fetcher_options[:before]) if fetcher_options[:before]
@data = http_request(uri, options) { |response_data|
fetcher_filters.inject(response_data) { |data, filter|
filter.call data
}
}
@data = fetcher_options[:after] ? send(fetcher_options[:after]) : @data
end
private
class << self
attr_reader :fetcher_options
attr_reader :fetcher_filters
end
def self.add_fetcher_options( methods )
@fetcher_options ||= {}
@fetcher_options.merge! methods
end
def self.add_fetcher_filters( *filters )
@fetcher_filters ||= []
@fetcher_filters += filters
end
def fetcher_options
self.class.fetcher_options || {}
end
def fetcher_filters
self.class.fetcher_filters || []
end
# ... more code ...
end
end
module Fetcher
class Vimeo < Base
format :json
base_uri "vimeo.com/api/v2/channel"
MostRecentVideo = lambda{ |videos| videos.max_by{ |v| v[:upload_date] } }
add_fetcher_options after: :generate_hash
add_fetcher_filters MostRecentVideo
def uri
"/#{@cue}/videos.json"
end
private
def generate_hash
{
#...
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment