Created
June 9, 2012 13:43
-
-
Save cupakromer/2901020 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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