Class: EspnRb::Headline
- Inherits:
-
Object
- Object
- EspnRb::Headline
- Defined in:
- lib/espn_rb/headline.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
-
#api_methods ⇒ Hash
Returns the ESPN methods as defined [here](developer.espn.com/docs/headlines#parameters).
-
#api_resources ⇒ Hash
Returns the ESPN resources as defined [here](developer.espn.com/docs/headlines#parameters).
-
#create_headline_methods ⇒ HeadlineResponse
This will define singleton methods for all resources defined in EspnRb::Headline.api_resources.
-
#for_athlete(opt) ⇒ Object
Takes EspnRb::Headline.api_methods[:url] and subs out the options passed by user.
-
#for_date(opt) ⇒ Object
Takes EspnRb::Headline.api_methods[:url] and subs out the options passed by user.
-
#get_api_method(opt) ⇒ String
Attempts to parse which EspnRb::Headline.api_methods the user has passed to method defined by EspnRb::Headline.create_headline_methods.
-
#get_results(resource, method) ⇒ HeadlineResponse
The final request to ESPN api after all option parsing has been completed.
-
#help ⇒ Object
Provides help text.
-
#initialize(opts) ⇒ Headline
constructor
A new instance of Headline.
- #opt_from_hash(opt) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Headline
Returns a new instance of Headline.
6 7 8 9 10 11 |
# File 'lib/espn_rb/headline.rb', line 6 def initialize(opts) @api_key = opts && !opts[:api_key].nil? ? opts[:api_key] : ENV['espn_api_key'] raise StandardError, "You must specify an API key." if @api_key.nil? create_headline_methods end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
3 4 5 |
# File 'lib/espn_rb/headline.rb', line 3 def api_key @api_key end |
Instance Method Details
#api_methods ⇒ Hash
Returns the ESPN methods as defined [here](developer.espn.com/docs/headlines#parameters).
23 24 25 |
# File 'lib/espn_rb/headline.rb', line 23 def api_methods @api_methods ||= YAML::load(File.read(File.(File.join(File.dirname(__FILE__), "..", 'espn_rb/api_definitions/headline_methods.yaml')))) end |
#api_resources ⇒ Hash
Returns the ESPN resources as defined [here](developer.espn.com/docs/headlines#parameters).
16 17 18 |
# File 'lib/espn_rb/headline.rb', line 16 def api_resources @api_resources ||= YAML::load(File.read(File.(File.join(File.dirname(__FILE__), "..", 'espn_rb/api_definitions/headline_resources.yaml')))) end |
#create_headline_methods ⇒ HeadlineResponse
This will define singleton methods for all resources defined in EspnRb::Headline.api_resources
39 40 41 42 43 44 45 |
# File 'lib/espn_rb/headline.rb', line 39 def create_headline_methods api_resources.each do |k,v| define_singleton_method(k) do |opt=nil| get_results(v[:url], get_api_method(opt)) end end end |
#for_athlete(opt) ⇒ Object
Takes EspnRb::Headline.api_methods[:url] and subs out the options passed by user
85 86 87 |
# File 'lib/espn_rb/headline.rb', line 85 def for_athlete(opt) api_methods[:for_athlete][:url].gsub(":athleteId", opt[:for_athlete]) end |
#for_date(opt) ⇒ Object
Takes EspnRb::Headline.api_methods[:url] and subs out the options passed by user
92 93 94 |
# File 'lib/espn_rb/headline.rb', line 92 def for_date(opt) api_methods[:for_date][:url].gsub(":yyyymmdd", Date.parse(opt[:for_date]).strftime("%Y%m%d")) end |
#get_api_method(opt) ⇒ String
This will accept either a string or a hash.
Attempts to parse which EspnRb::Headline.api_methods the user has passed to method defined by EspnRb::Headline.create_headline_methods.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/espn_rb/headline.rb', line 63 def get_api_method(opt) case opt.class.to_s when "Symbol" api_method = api_methods.keys.include?(opt) ? api_methods[opt][:url] : (raise StandardError, "The parameter you sent is not available.") when "Hash" api_method = opt_from_hash(opt) else api_method = api_methods[:news][:url] end end |
#get_results(resource, method) ⇒ HeadlineResponse
The final request to ESPN api after all option parsing has been completed.
29 30 31 32 33 |
# File 'lib/espn_rb/headline.rb', line 29 def get_results(resource, method) http = Net::HTTP.new("api.espn.com") request = Net::HTTP::Get.new("/#{EspnRb::API_VERSION}#{resource}#{method}?apikey=#{@api_key}") HeadlineResponse.new JSON.parse(http.request(request).body) end |
#help ⇒ Object
Provides help text. Passes the methods available from create_headline_methods all prettified into a set of strings and passes them to EspnRb::Utilities.help which then inserts it below some nice ascii art.
98 99 100 |
# File 'lib/espn_rb/headline.rb', line 98 def help EspnRb::Utilities.help api_resources.map {|k,v| "\t#{(':' + k.to_s).ljust(25)} #{v[:description]}"}.join("\n") end |
#opt_from_hash(opt) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/espn_rb/headline.rb', line 74 def opt_from_hash(opt) if !opt[:for_date].nil? for_date opt elsif !opt[:for_athlete].nil? for_athlete opt end end |