espn 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +91 -6
  3. data/lib/espn.rb +5 -0
  4. data/lib/espn/arguments.rb +41 -0
  5. data/lib/espn/client.rb +29 -5
  6. data/lib/espn/client/athletes.rb +39 -21
  7. data/lib/espn/client/audio.rb +20 -19
  8. data/lib/espn/client/headlines.rb +58 -27
  9. data/lib/espn/client/medals.rb +8 -0
  10. data/lib/espn/client/notes.rb +31 -21
  11. data/lib/espn/client/now.rb +11 -8
  12. data/lib/espn/client/scores.rb +33 -21
  13. data/lib/espn/client/sports.rb +20 -14
  14. data/lib/espn/client/standings.rb +20 -16
  15. data/lib/espn/client/teams.rb +39 -21
  16. data/lib/espn/client/video.rb +14 -17
  17. data/lib/espn/configuration.rb +29 -12
  18. data/lib/espn/error.rb +15 -3
  19. data/lib/espn/helpers.rb +39 -0
  20. data/lib/espn/mapper.rb +142 -0
  21. data/lib/espn/request.rb +86 -7
  22. data/lib/espn/version.rb +1 -1
  23. data/lib/faraday/response/raise_espn_error.rb +33 -11
  24. data/spec/espn/arguments_spec.rb +76 -0
  25. data/spec/espn/client/athletes_spec.rb +38 -47
  26. data/spec/espn/client/audio_spec.rb +30 -29
  27. data/spec/espn/client/headlines_spec.rb +80 -35
  28. data/spec/espn/client/medals_spec.rb +1 -1
  29. data/spec/espn/client/notes_spec.rb +39 -45
  30. data/spec/espn/client/now_spec.rb +1 -2
  31. data/spec/espn/client/scores_spec.rb +56 -39
  32. data/spec/espn/client/sports_spec.rb +56 -18
  33. data/spec/espn/client/standings_spec.rb +42 -19
  34. data/spec/espn/client/teams_spec.rb +61 -43
  35. data/spec/espn/client/video_spec.rb +9 -13
  36. data/spec/espn/helpers_spec.rb +31 -0
  37. data/spec/espn/mapper_spec.rb +136 -0
  38. data/spec/espn/request_spec.rb +65 -0
  39. data/spec/espn_spec.rb +11 -2
  40. data/spec/faraday/response/raise_espn_error_spec.rb +80 -0
  41. data/spec/responses/athletes.json +51 -0
  42. data/spec/responses/audio.json +101 -0
  43. data/spec/responses/headlines.json +174 -0
  44. data/spec/responses/medals.json +273 -0
  45. data/spec/responses/notes.json +100 -0
  46. data/spec/responses/now.json +44 -0
  47. data/spec/responses/scores.json +209 -0
  48. data/spec/responses/sports.json +164 -0
  49. data/spec/responses/standings.json +2689 -0
  50. data/spec/responses/teams.json +51 -0
  51. data/spec/responses/videos.json +28 -0
  52. data/spec/spec_helper.rb +11 -2
  53. metadata +37 -3
  54. data/lib/espn/connection.rb +0 -32
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MGYxMjBmZGVlZTE0NDM5NWRkNWM3MmQ1Yjg3ODhjOTVkNjQ5ODhjMw==
4
+ ODhiNTFjZWMxNmZiY2M0NDdlY2EwMTk1ZDI1ZmNiMzE4N2I0NWQzZQ==
5
5
  data.tar.gz: !binary |-
6
- ODE1YzlmNzA2NmE4YTdmMGRjYzYwYjcxOTA3ZDNiODA0M2FmODU5OA==
6
+ ZDM5YmFkZTE4OTlhZjg5NmE3OTMyOWIxYWYyOTU0ODNhZjEwZWRiNQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NWNiNjBmMDJjODJmZTg0ZTM5MjBmNDI3MjRmZjdiNmYyNmFlNjllMDY3NmI2
10
- ZjY0OGIyNGIwYzg1YTQ5Y2Q2YzY5ZmIxZjg0Nzk3YzFiZWZlMGNmNzU5ZDRm
11
- NTFlYWRkOGI2NDU5NzI1Y2U4NGI2N2E2MTI4NDdiMjMwYTdmOGM=
9
+ ZTY0NDUzYTU3ZmNmMTIyMWRmZTFkNTYxMjBiY2ZkYWFlMmJhMTJkYWZhMDBm
10
+ NGNmZTMxZTMyMjc4Zjc3NDMzYjI5N2VlZGY3Yzg5MDk5YmFjMmU1ZWEzODkw
11
+ ZDZhMTZlNmZhY2U4OTFiNmIxYjM5MzQzMGM1NWZmZTdmYTE3NjM=
12
12
  data.tar.gz: !binary |-
13
- MmFkOThkZTk1ZDZiNDIxYmE0ZmY1MzNiNWFmNWY3M2IwMTMxMjg2NDBiZTJh
14
- YzRmOGFjOTdjMjNkZGY0ZTc1NzYxMjdhNzEzMmJmYzc0OWQ2MDcxNDQ2MDRl
15
- M2I1N2U4MzYxODhlZTk4ODlhMjFkNTI0NzVhZTE1ZGYyZThjZTM=
13
+ MzM1MDI4Mzc5M2ZlOWZhYjEyMzJlYzQ4Njg5Zjg4YzBlZDI5NmUyYTgwNTRl
14
+ N2IzMDBjM2Y5ZTFiMmIwODU5MWRmYzc5MTBkYzY2ZTA0YzUyZTRlMTM1Yzg2
15
+ YWVlYjZmNzZlNTUzZDZhYzVkNzQ0NzNhZmRlYzVmMjJjZjQ5YzE=
data/README.md CHANGED
@@ -19,15 +19,100 @@ Next, make calls against the ESPN API.
19
19
 
20
20
  ### Sports
21
21
 
22
- sport = client.sports(sport: 'baseball', league: 'mlb').sports.first
23
- puts sport.leagues.first.groups.first.name
24
- # => American League
22
+ The Sports API is what they call a 'Helper' API. This allows you to pull out data for different sections of the API. The Sports API is not documented, but you can see how it works on their [API explorer.](http://developer.espn.com/io-docs)
23
+
24
+ client.sports(:mlb)
25
+ # => [#<Hashie::Mash name="baseball"...>]
26
+
27
+
28
+ ### Athletes
29
+
30
+ For more information about the Athletes API, [view the documentation.](http://developer.espn.com/docs/athletes)
31
+
32
+ client.athletes(:mlb)
33
+ # => [#<Hashie::Mash fullName="Chase Utley"...>]
34
+
35
+ client.athlete(12345, :nfl).fullName
36
+ # => #<Hashie::Mash fullName="LeSean McCoy"...>
37
+
38
+ ### Audio
39
+
40
+ For more information about the Audio API, [view the documentation.](http://developer.espn.com/docs/audio)
41
+
42
+ client.audio(podcast_id: 5)
43
+ # => [#<Hashie::Mash id=8656148 premium=false description=...>]
44
+
45
+ ### ESPN Now
46
+
47
+ For more information about the ESPN NOW API, [view the documentation.](http://developer.espn.com/docs/now)
48
+
49
+ client.now
50
+ # => [#<Hashie::Mash headline="Halladay's future"...>]
25
51
 
26
52
  ### Headlines
27
53
 
28
- headline = client.headlines(sport: 'baseball', league: 'mlb').headlines.first
29
- puts headline.title
30
- # => Atlanta Braves outfielder Jason Heyward hit in jaw with pitch
54
+ For more information about the Headlines API, [view the documentation.](http://developer.espn.com/docs/headlines)
55
+
56
+ client.headlines(:mlb)
57
+ # => [#<Hashie::Mash headline="Ronnie Brewer has deal with Rockets"...>]
58
+
59
+ client.headline(12345, :nfl)
60
+ # => #<Hashie::Mash headline="Eagles won't be good this year"...>
61
+
62
+ ### Medals
63
+
64
+ For more information about the Medals API, [view the documentation.](http://developer.espn.com/docs/medals)
65
+
66
+ client.medals
67
+ # => #<Hashie::Mash season=#<Hashie::Mash...> sports=[] competitors=[]...>
68
+
69
+ ### Research Notes
70
+
71
+ For more information about the Resarch Notes API, [view the documentation.](http://developer.espn.com/docs/research)
72
+
73
+ client.notes
74
+ # => [#<Hashie::Mash headline="Cowboys Stink"...>]
75
+
76
+ client.note(8659824)
77
+ # => #<Hashie::Mash headline="Cowboys Stink"...>
78
+
79
+ ### Scores & Schedules
80
+
81
+ For more information about the Scores & Schedules API, [view the documentation.](http://developer.espn.com/docs/scores)
82
+
83
+ client.scores(:mlb)
84
+ # => [#<Hashie::Mash competitions=[#<Hashie::Mash competitors=[]...]...>]
85
+
86
+ client.score(12345)
87
+ # => #<Hashie::Mash competitions=[#<Hashie::Mash competitors=[]...]...>
88
+
89
+ ### Standings
90
+
91
+ For more information about the Standings API, [view the documentation.](http://developer.espn.com/docs/standings)
92
+
93
+ client.standings(:nba)
94
+ # => [#<Hashie::Mash name="Eastern" groups=[]...>]
95
+
96
+ ### Teams
97
+
98
+ For more information about the Teams API, [view the documentation.](http://developer.espn.com/docs/teams)
99
+
100
+ client.teams(:mlb)
101
+ # => [#<Hashie::Mash name="Phillies"...>]
102
+
103
+ client.team(12345, :nfl)
104
+ # => #<Hashie::Mash name="Eagles"...>
105
+
106
+ ### Video
107
+
108
+ For more information about the Video API, [view the documentation.](http://developer.espn.com/docs/video)
109
+
110
+ client.videos
111
+ # => [#<Hashie::Mash name="Video Channel" videos=[]...>]
112
+
113
+ ## More Information
114
+
115
+ This is a Ruby Wrapper around the ESPN developer API. If you want to see how their API works, you can [visit their developer center.](http://developer.espn.com/overview)
31
116
 
32
117
  ## Inspiration
33
118
 
@@ -1,7 +1,12 @@
1
1
  require 'espn/configuration'
2
2
  require 'espn/client'
3
3
  require 'espn/error'
4
+ require 'espn/arguments'
5
+ require 'uri_template'
4
6
 
7
+ # Public: The ESPN Module that all other modules and classes will be a part of.
8
+ # This module extends the Configuration module so you can modify defaults at
9
+ # this level.
5
10
  module ESPN
6
11
  extend Configuration
7
12
 
@@ -0,0 +1,41 @@
1
+ require 'espn/mapper'
2
+ require 'espn/helpers'
3
+
4
+ module ESPN
5
+
6
+ # Internal: Extract options from method arguments.
7
+ class Arguments < Array
8
+ include ESPN::Mapper
9
+ include ESPN::Helpers
10
+
11
+ # Public: Gets the options Hash.
12
+ attr_reader :options
13
+
14
+ # Public: Initialize an instance of Arguments.
15
+ #
16
+ # args - The Array of arguments passed into a method.
17
+ # defaults - The Hash of default values for the options (default: {}).
18
+ # required - The Array of fields that are required (default: []).
19
+ def initialize(args, defaults={}, required=[])
20
+ @options = args.last.is_a?(::Hash) ? args.pop : {}
21
+
22
+ # Set defaults, only if the value in @options is nil.
23
+ @options.merge!(defaults) { |k, v1, v2| v1 }
24
+
25
+ # Extract sport and league
26
+ @options[:sport], @options[:league] =
27
+ extract_sport_and_league(args, @options)
28
+
29
+ # Validate required fields
30
+ required.each do |field|
31
+ if blank?(@options[field.to_sym])
32
+ raise ArgumentError, "You must supply a valid #{field}."
33
+ end
34
+ end
35
+
36
+ super(args)
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -1,6 +1,5 @@
1
- require 'espn/connection'
1
+ require 'espn/helpers'
2
2
  require 'espn/request'
3
-
4
3
  require 'espn/client/athletes'
5
4
  require 'espn/client/audio'
6
5
  require 'espn/client/headlines'
@@ -15,10 +14,35 @@ require 'espn/client/video'
15
14
 
16
15
  module ESPN
17
16
 
17
+ # Public: The class that handles the bulk of the work between the API and
18
+ # the wrapper.
19
+ #
20
+ # Examples
21
+ #
22
+ # client = ESPN::Client.new(api_key: 'abc123')
23
+ # # => ESPN::Client
18
24
  class Client
19
25
 
20
- # Public: An attr_accessor for each configuration option.
21
- attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
26
+ # Public: Gets/Sets the Symbol adapter.
27
+ attr_accessor :adapter
28
+
29
+ # Public: Gets/Sets the Fixnum api version.
30
+ attr_accessor :api_version
31
+
32
+ # Public: Gets/Sets the String api key.
33
+ attr_accessor :api_key
34
+
35
+ # Public: Gets/Sets the Fixnum open timeout.
36
+ attr_accessor :open_timeout
37
+
38
+ # Public: Gets/Sets the String proxy.
39
+ attr_accessor :proxy
40
+
41
+ # Public: Gets/Sets the Fixnum timeout.
42
+ attr_accessor :timeout
43
+
44
+ # Public: Gets/Sets the String user agent.
45
+ attr_accessor :user_agent
22
46
 
23
47
  # Public: Initialize a new Client. To see all options that can be
24
48
  # configured, look at the Configuration module, specifically
@@ -47,8 +71,8 @@ module ESPN
47
71
  !api_key.nil?
48
72
  end
49
73
 
50
- include ESPN::Connection
51
74
  include ESPN::Request
75
+ include ESPN::Helpers
52
76
  include ESPN::Client::Athletes
53
77
  include ESPN::Client::Audio
54
78
  include ESPN::Client::Headlines
@@ -1,32 +1,50 @@
1
1
  module ESPN
2
2
  class Client
3
+
4
+ # Public: The mapping to the ATHLETES API of the ESPN developer API.
5
+ #
6
+ # Examples
7
+ #
8
+ # class Client
9
+ # include ESPN::Client::Athletes
10
+ # end
3
11
  module Athletes
4
12
 
13
+ # Public: Get a specific athlete from the ESPN API.
14
+ #
15
+ # id - An id of an athlete.
16
+ # sport - A Symbol or String of the sport (optional).
17
+ # league - A Symbol or String of the league. If a league is passed, but
18
+ # not a sport, an attempt will be made to map the league to a
19
+ # sport (required).
20
+ # opts - Hash options used to refine the selection. If sport and/or
21
+ # league are passed in, they will override the mapped values
22
+ # You can find a full list of options on the ESPN developer API
23
+ # website (default: {}).
24
+ #
25
+ # Returns a Hashie::Mash.
26
+ def athlete(id, *args)
27
+ arguments = ESPN::Arguments.new(args, {}, [:league])
28
+ pattern = "sports/:sport/:league/athletes/#{id}"
29
+ get(pattern, arguments.options).sports.first.leagues.first.athletes.first
30
+ end
31
+
5
32
  # Public: Get athlete stats and information from the ESPN API.
6
33
  #
7
- # opts - Hash options used to refine the selection (default: {}).
8
- # - :sport - The name of the sport (default: nil).
9
- # - :league - The name of the league (default: nil).
10
- # - :athlete_id - The id of the athlete (default: nil).
34
+ # sport - A Symbol or String of the sport (optional).
35
+ # league - A Symbol or String of the league. If a league is passed, but
36
+ # not a sport, an attempt will be made to map the league to a
37
+ # sport (required).
38
+ # opts - Hash options used to refine the selection. If sport and/or
39
+ # league are passed in, they will override the mapped values
40
+ # You can find a full list of options on the ESPN developer API
41
+ # website (default: {}).
11
42
  #
12
43
  # Returns an Array of Hashie::Mash.
13
- def athletes(opts={})
14
- url = "sports"
15
-
16
- unless opts[:sport].to_s.empty?
17
- url += "/#{opts[:sport]}"
18
- unless opts[:league].to_s.empty?
19
- url += "/#{opts[:league]}"
20
- end
21
- end
22
-
23
- url += "/athletes"
24
-
25
- unless opts[:athlete_id].to_s.empty?
26
- url += "/#{opts[:athlete_id]}"
27
- end
28
-
29
- get(url, opts)
44
+ def athletes(*args)
45
+ arguments = ESPN::Arguments.new(args, {}, [:league])
46
+ pattern = "sports/:sport/:league/athletes"
47
+ get(pattern, arguments.options).sports.first.leagues.first.athletes
30
48
  end
31
49
 
32
50
  end
@@ -1,35 +1,36 @@
1
1
  module ESPN
2
2
  class Client
3
+
4
+ # Public: The mapping to the AUDIO API of the ESPN developer API.
5
+ #
6
+ # Examples
7
+ #
8
+ # class Client
9
+ # include ESPN::Client::Audio
10
+ # end
3
11
  module Audio
4
12
 
5
13
  # Public: Get audio podcasts and clips from ESPN.
6
14
  #
7
- # opts - Hash options used to refine the selection (default: {}).
8
- # - :method - The name of the sport (default: 'podcasts').
9
- # - :podast_id - The id of the podcast (default: nil).
10
- # - :recording_id - The id of the recording (default: nil).
15
+ # opts - Hash options used to refine the selection. If sport and/or
16
+ # league are passed in, they will override the mapped values
17
+ # You can find a full list of options on the ESPN developer API
18
+ # website (default: {}).
19
+ # :method - The name of the sport (default: 'podcasts').
20
+ # :podcast_id - The id of the podcast (default: nil).
21
+ # :recording_id - The id of the recording (default: nil).
11
22
  #
12
23
  # Returns an Array of Hashie::Mash.
13
24
  def audio(opts={})
14
- opts [:method] ||= 'podcasts'
15
-
16
- url = 'audio'
25
+ opts[:method] ||= 'podcasts'
17
26
 
27
+ # Correct invalid method.
18
28
  if opts[:method] == 'podcast_recordings'
19
- url += '/podcasts/recordings'
20
- else
21
- url += "/#{opts[:method]}"
22
- end
23
-
24
- unless opts[:podcast_id].to_s.empty?
25
- url += "/#{opts[:podcast_id]}"
26
- end
27
-
28
- unless opts[:recording_id].to_s.empty?
29
- url += "/#{opts[:recording_id]}"
29
+ opts[:method] = 'podcasts/recordings'
30
30
  end
31
31
 
32
- get(url, opts)
32
+ pattern = "audio/#{opts[:method]}/:podcast_id/:recording_id"
33
+ get(pattern, opts).recordings
33
34
  end
34
35
 
35
36
  end
@@ -1,38 +1,69 @@
1
1
  module ESPN
2
2
  class Client
3
+
4
+ # Public: The mapping to the HEADLINES API of the ESPN developer API.
5
+ #
6
+ # Examples
7
+ #
8
+ # class Client
9
+ # include ESPN::Client::Headlines
10
+ # end
3
11
  module Headlines
4
12
 
13
+ # Public: Get a specific headline from the ESPN API.
14
+ #
15
+ # id - The id of the headline to get.
16
+ # opts - Hash options used to refine the selection. You can find the full
17
+ # list of options on the ESPN developer API website (default: {}).
18
+ #
19
+ # Examples:
20
+ #
21
+ # client.headline(12345)
22
+ # # Request made to 'sports/news/12345'
23
+ #
24
+ # Returns a Hashie::Mash.
25
+ def headline(id, opts={})
26
+ get("sports/news/#{id}", opts).headlines.first
27
+ end
28
+
5
29
  # Public: Get latest sports news and analysis from the ESPN API.
6
30
  #
7
- # opts - Hash options used to refine the selection (default: {}).
8
- # - :section - The news section (default: 'sports').
9
- # - :sport - The name of the sport (default: nil).
10
- # - :league - The name of the league (default: nil).
11
- # - :method - The method of headlines (default: nil).
12
- # - :headline_id - The id of a specific headline (default: nil).
31
+ # sport - A Symbol or String of the sport (optional).
32
+ # league - A Symbol or String of the league. If a league is passed, but
33
+ # not a sport, an attempt will be made to map the league to a
34
+ # sport (optional).
35
+ # opts - Hash options used to refine the selection. If sport and/or
36
+ # league are passed in, they will override the mapped values
37
+ # You can find a full list of options on the ESPN developer API
38
+ # website (default: {}).
39
+ # :section - The news section (default: 'sports').
40
+ # :sport - Name of the sport (default: nil, optional).
41
+ # :league - Name of the league (default: nil, optional).
42
+ # :method - Method of headlines (default: nil, optional).
43
+ #
44
+ # Examples
45
+ #
46
+ # client.headlines(:mlb)
47
+ # # Request made to 'sports/baseball/mlb/news'
48
+ #
49
+ # client.headlines('baseball')
50
+ # # Request will be made to 'sports/baseball/news'
51
+ #
52
+ # client.headlines('horse-racing', :mlb)
53
+ # # Request will be made to 'sports/horse-racing/mlb/news'
54
+ #
55
+ # client.headlines(:mlb, sport: 'basketball')
56
+ # # Request will be made to 'sports/basketball/mlb/news'
57
+ #
58
+ # client.headlines(:baseball, :mlb, sport: 'basketball', league: 'nba')
59
+ # # Request will be made to 'sports/basketball/nba/news'
13
60
  #
14
61
  # Returns an Array of Hashie::Mash.
15
- def headlines(opts={})
16
- url = opts[:section] || 'sports'
17
-
18
- unless opts[:sport].to_s.empty?
19
- url += "/#{opts[:sport]}"
20
- unless opts[:league].to_s.empty?
21
- url += "/#{opts[:league]}"
22
- end
23
- end
24
-
25
- url += '/news'
26
-
27
- unless opts[:method].to_s.empty?
28
- url += "/headlines/#{opts[:method]}"
29
- end
30
-
31
- unless opts[:headline_id].to_s.empty?
32
- url += "/#{opts[:headline_id]}"
33
- end
34
-
35
- get(url, opts)
62
+ def headlines(*args)
63
+ arguments = ESPN::Arguments.new(args, section: 'sports')
64
+ pattern = arguments.options[:section]
65
+ pattern += '/:sport/:league/news/headlines/:method/:id'
66
+ get(pattern, arguments.options).headlines
36
67
  end
37
68
 
38
69
  end