Class: EspnRb::Headline

Inherits:
Object
  • Object
show all
Defined in:
lib/espn_rb/headline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Headline

Returns a new instance of Headline.

Raises:

  • (StandardError)

    Will raise StandardError if api_key isn’t specified by either options passed into initialize or set from ENV



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_keyObject (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_methodsHash

Returns the ESPN methods as defined [here](developer.espn.com/docs/headlines#parameters).

Returns:

  • (Hash)

    methods, and their associated urls and brief description.



23
24
25
# File 'lib/espn_rb/headline.rb', line 23

def api_methods
  @api_methods ||= YAML::load(File.read(File.expand_path(File.join(File.dirname(__FILE__), "..", 'espn_rb/api_definitions/headline_methods.yaml'))))
end

#api_resourcesHash

Returns the ESPN resources as defined [here](developer.espn.com/docs/headlines#parameters).

Returns:

  • (Hash)

    of sports, urls and their descriptions.



16
17
18
# File 'lib/espn_rb/headline.rb', line 16

def api_resources
  @api_resources ||= YAML::load(File.read(File.expand_path(File.join(File.dirname(__FILE__), "..", 'espn_rb/api_definitions/headline_resources.yaml'))))
end

#create_headline_methodsHeadlineResponse

This will define singleton methods for all resources defined in EspnRb::Headline.api_resources

Returns:

  • (HeadlineResponse)

    which contains the espn response object and assocated methods.



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

Examples:

internal call

for_athlete({:for_athlete => "123"}) => '/athletes/123/news'


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

Examples:

internal call

for_date({:for_date => "2012-01-01"}) => '/news/dates/20120101'


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

Note:

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.

Examples:

Call with string

get_api_method("news") #=> '/news'

Call with Hash (for_date)

get_api_method({:for_date => "2012-01-01"}) #=> '/news/dates/20120101'

Parameters:

  • options (Hash)

    the options passed in by user.

Returns:

  • (String)

    a url entry from EspnRb::Headline.api_methods defaults to :news



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.

Returns:



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

#helpObject

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