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
@@ -3,71 +3,88 @@ require 'spec_helper'
3
3
  describe ESPN::Client::Scores do
4
4
  before do
5
5
  @client = ESPN::Client.new
6
+ stub_get 'scores.json'
7
+ end
8
+
9
+ describe '#score' do
10
+ it 'should return an event' do
11
+ @client.score(1).competitions.is_a?(Array).should be_true
12
+ end
13
+
14
+ it 'should request the specific event' do
15
+ @client.score(1)
16
+ assert_requested :get, espn_url('sports/events/1')
17
+ end
6
18
  end
7
19
 
8
20
  describe '#scores' do
21
+ it 'returns an array of events with scores' do
22
+ @client.scores.first.respond_to?(:competitions).should be_true
23
+ end
9
24
 
10
25
  it 'gets all scores' do
11
- stub_get('sports/events')
12
26
  @client.scores
13
27
  assert_requested :get, espn_url('sports/events')
14
28
  end
15
29
 
16
- context 'with a sport param' do
17
- it 'should include the sport in the request' do
18
- stub_get('sports/baseball/events')
19
- @client.scores(sport: 'baseball')
20
- assert_requested :get, espn_url('sports/baseball/events')
30
+ context 'when passing values as args' do
31
+ context 'when passing the sport' do
32
+ it 'should request scores for that sport' do
33
+ @client.scores(:baseball)
34
+ assert_requested :get, espn_url('sports/baseball/events')
35
+ end
21
36
  end
22
37
 
23
- context 'with a event_id param' do
24
- it 'should include the sport and event_id params in the request' do
25
- stub_get('sports/baseball/events/5')
26
- @client.scores(sport: 'baseball', event_id: 5)
27
- assert_requested :get, espn_url('sports/baseball/events/5')
38
+ context 'when passing the league' do
39
+ it 'should request scores for that league' do
40
+ @client.scores(:nba)
41
+ assert_requested :get, espn_url('sports/basketball/nba/events')
28
42
  end
29
43
  end
30
- end
31
44
 
32
- context 'with a sport and a league param' do
33
- it 'should include the sport and league params in the request' do
34
- stub_get('sports/baseball/mlb/events')
35
- @client.scores(sport: 'baseball', league: 'mlb')
36
- assert_requested :get, espn_url('sports/baseball/mlb/events')
45
+ context 'when passing the sport and league' do
46
+ it 'should request scores for that league and sport' do
47
+ @client.scores(:baseball, :nba)
48
+ assert_requested :get, espn_url('sports/baseball/nba/events')
49
+ end
37
50
  end
38
51
 
39
- context 'with a event_id param' do
40
- it 'should include the sport, league and event_id params' do
41
- stub_get('sports/baseball/mlb/events/1')
42
- @client.scores(sport: 'baseball', league: 'mlb', event_id: 1)
43
- assert_requested :get, espn_url('sports/baseball/mlb/events/1')
52
+ context 'when passing the league in the opts hash' do
53
+ it 'should override the arg' do
54
+ @client.scores(:nba, league: 'mlb')
55
+ assert_requested :get, espn_url('sports/basketball/mlb/events')
44
56
  end
45
57
  end
46
- end
47
58
 
48
- context 'with a event_id param' do
49
- it 'should include the event_id in the request' do
50
- stub_get('sports/events/5')
51
- @client.scores(event_id: 5)
52
- assert_requested :get, espn_url('sports/events/5')
59
+ context 'when passing the sport in the opts hash' do
60
+ it 'should override the arg' do
61
+ @client.scores(:baseball, sport: 'basketball')
62
+ assert_requested :get, espn_url('sports/basketball/events')
63
+ end
53
64
  end
54
65
  end
55
66
 
56
- context 'with a league param and no sport param' do
57
- it 'should not include either in the request' do
58
- stub_get('sports/events')
59
- @client.scores(league: 'mlb')
60
- assert_requested :get, espn_url('sports/events')
67
+ context 'when passing values in the opts hash' do
68
+ context 'with a sport param' do
69
+ it 'should include the sport in the request' do
70
+ @client.scores(sport: 'baseball')
71
+ assert_requested :get, espn_url('sports/baseball/events')
72
+ end
73
+ end
74
+
75
+ context 'with a sport and a league param' do
76
+ it 'should include the sport and league params in the request' do
77
+ @client.scores(sport: 'baseball', league: 'mlb')
78
+ assert_requested :get, espn_url('sports/baseball/mlb/events')
79
+ end
61
80
  end
62
81
 
63
- context 'with a event_id param' do
64
- it 'should include the event_id in the request' do
65
- stub_get('sports/events/2')
66
- @client.scores(league: 'mlb', event_id: 2)
67
- assert_requested :get, espn_url('sports/events/2')
82
+ context 'with a league param and no sport param' do
83
+ it 'should map the league to a sport' do
84
+ @client.scores(league: 'mlb')
85
+ assert_requested :get, espn_url('sports/baseball/mlb/events')
68
86
  end
69
87
  end
70
88
  end
71
-
72
89
  end
73
90
  end
@@ -3,39 +3,77 @@ require 'spec_helper'
3
3
  describe ESPN::Client::Sports do
4
4
  before do
5
5
  @client = ESPN::Client.new
6
+ stub_get 'sports.json'
6
7
  end
7
8
 
8
9
  describe '#sports' do
10
+ it 'returns an array of sports' do
11
+ @client.sports.first.name.should eq('baseball')
12
+ end
9
13
 
10
14
  it 'gets all sports' do
11
- stub_get('sports')
12
15
  @client.sports
13
16
  assert_requested :get, espn_url('sports')
14
17
  end
15
18
 
16
- context 'with a sport param' do
17
- it 'should include the sport param in the request' do
18
- stub_get('sports/baseball')
19
- @client.sports(sport: 'baseball')
20
- assert_requested :get, espn_url('sports/baseball')
19
+ context 'when passing values as args' do
20
+ context 'when passing the sport' do
21
+ it 'should request that sport' do
22
+ @client.sports(:baseball)
23
+ assert_requested :get, espn_url('sports/baseball')
24
+ end
21
25
  end
22
- end
23
26
 
24
- context 'with a sport and a league param' do
25
- it 'should include both params in the request' do
26
- stub_get('sports/baseball/mlb')
27
- @client.sports(sport: 'baseball', league: 'mlb')
28
- assert_requested :get, espn_url('sports/baseball/mlb')
27
+ context 'when passing the league' do
28
+ it 'should request that league' do
29
+ @client.sports(:mlb)
30
+ assert_requested :get, espn_url('sports/baseball/mlb')
31
+ end
32
+ end
33
+
34
+ context 'when passing the sport and league' do
35
+ it 'should request that sport and league' do
36
+ @client.sports(:baseball, :nba)
37
+ assert_requested :get, espn_url('sports/baseball/nba')
38
+ end
39
+ end
40
+
41
+ context 'when passing the league in the opts hash' do
42
+ it 'should override the arg' do
43
+ @client.sports(:nba, league: 'mlb')
44
+ assert_requested :get, espn_url('sports/basketball/mlb')
45
+ end
29
46
  end
30
- end
31
47
 
32
- context 'with a league param but no sport param' do
33
- it 'should not include the league param in the request' do
34
- stub_get('sports')
35
- @client.sports(league: 'mlb')
36
- assert_requested :get, espn_url('sports')
48
+ context 'when passing the sport in the opts hash' do
49
+ it 'should override the arg' do
50
+ @client.sports(:baseball, sport: :basketball)
51
+ assert_requested :get, espn_url('sports/basketball')
52
+ end
37
53
  end
38
54
  end
39
55
 
56
+ context 'when passing values in the opts hash' do
57
+ context 'with a sport param' do
58
+ it 'should include the sport param in the request' do
59
+ @client.sports(sport: 'baseball')
60
+ assert_requested :get, espn_url('sports/baseball')
61
+ end
62
+ end
63
+
64
+ context 'with a sport and a league param' do
65
+ it 'should include both params in the request' do
66
+ @client.sports(sport: 'baseball', league: 'mlb')
67
+ assert_requested :get, espn_url('sports/baseball/mlb')
68
+ end
69
+ end
70
+
71
+ context 'with a league param but no sport param' do
72
+ it 'should map the league to a sport' do
73
+ @client.sports(league: 'mlb')
74
+ assert_requested :get, espn_url('sports/baseball/mlb')
75
+ end
76
+ end
77
+ end
40
78
  end
41
79
  end
@@ -3,39 +3,62 @@ require 'spec_helper'
3
3
  describe ESPN::Client::Standings do
4
4
  before do
5
5
  @client = ESPN::Client.new
6
+ stub_get 'standings.json'
6
7
  end
7
8
 
8
9
  describe '#standings' do
10
+ it 'returns an array of standings' do
11
+ team = @client.standings(:mlb).first.groups.first
12
+ team.competitors.is_a?(Array).should be_true
13
+ end
9
14
 
10
- it 'gets all standings' do
11
- stub_get('sports/standings')
12
- @client.standings
13
- assert_requested :get, espn_url('sports/standings')
15
+ it 'gets all standings for a given league' do
16
+ @client.standings(:mlb)
17
+ assert_requested :get, espn_url('sports/baseball/mlb/standings')
14
18
  end
15
19
 
16
- context 'with a sport param' do
17
- it 'should include the sport in the request' do
18
- stub_get('sports/baseball/standings')
19
- @client.standings(sport: 'baseball')
20
- assert_requested :get, espn_url('sports/baseball/standings')
20
+ context 'when league is not passed' do
21
+ it 'should raise an argument error' do
22
+ expect { @client.standings }.to raise_error(ArgumentError)
21
23
  end
22
24
  end
23
25
 
24
- context 'with a sport and a league param' do
25
- it 'should include the sport and league params in the request' do
26
- stub_get('sports/baseball/mlb/standings')
27
- @client.standings(sport: 'baseball', league: 'mlb')
28
- assert_requested :get, espn_url('sports/baseball/mlb/standings')
26
+ context 'when invalid league is passed' do
27
+ it 'should raise an argument error' do
28
+ expect { @client.standings(:foobar) }.to raise_error(ArgumentError)
29
29
  end
30
30
  end
31
31
 
32
- context 'with a league param and no sport param' do
33
- it 'should not include either in the request' do
34
- stub_get('sports/standings')
35
- @client.standings(league: 'mlb')
36
- assert_requested :get, espn_url('sports/standings')
32
+ context 'when passing values as args' do
33
+ context 'when passing the league' do
34
+ it 'should map that league to a sport' do
35
+ @client.standings(:mlb)
36
+ assert_requested :get, espn_url('sports/baseball/mlb/standings')
37
+ end
38
+ end
39
+
40
+ context 'when passing the sport and league' do
41
+ it 'should get standings for that sport and league' do
42
+ @client.standings(:football, :nfl)
43
+ assert_requested :get, espn_url('sports/football/nfl/standings')
44
+ end
37
45
  end
38
46
  end
39
47
 
48
+ context 'when passing values in the opts hash' do
49
+ context 'with a league param' do
50
+ it 'should map the league to a sport' do
51
+ @client.standings(league: 'mlb')
52
+ assert_requested :get, espn_url('sports/baseball/mlb/standings')
53
+ end
54
+ end
55
+
56
+ context 'with a sport and a league param' do
57
+ it 'should include the sport and league params in the request' do
58
+ @client.standings(sport: 'baseball', league: 'mlb')
59
+ assert_requested :get, espn_url('sports/baseball/mlb/standings')
60
+ end
61
+ end
62
+ end
40
63
  end
41
64
  end
@@ -3,71 +3,89 @@ require 'spec_helper'
3
3
  describe ESPN::Client::Teams do
4
4
  before do
5
5
  @client = ESPN::Client.new
6
+ stub_get 'teams.json'
6
7
  end
7
8
 
8
- describe '#teams' do
9
+ describe '#team' do
10
+ it 'should return a team' do
11
+ @client.team(1, :mlb).name.should eq('Phillies')
12
+ end
9
13
 
10
- it 'gets all teams' do
11
- stub_get('sports/teams')
12
- @client.teams
13
- assert_requested :get, espn_url('sports/teams')
14
+ it 'should get a team for the given league' do
15
+ @client.team(1, :mlb)
16
+ assert_requested :get, espn_url('sports/baseball/mlb/teams/1')
14
17
  end
15
18
 
16
- context 'with a sport param' do
17
- it 'should include the sport in the request' do
18
- stub_get('sports/baseball/teams')
19
- @client.teams(sport: 'baseball')
20
- assert_requested :get, espn_url('sports/baseball/teams')
21
- end
19
+ it 'should accept a league in the opts hash' do
20
+ @client.team(1, league: 'mlb')
21
+ assert_requested :get, espn_url('sports/baseball/mlb/teams/1')
22
+ end
22
23
 
23
- context 'with a team_id param' do
24
- it 'should include the sport and team_id params in the request' do
25
- stub_get('sports/baseball/teams/5')
26
- @client.teams(sport: 'baseball', team_id: 5)
27
- assert_requested :get, espn_url('sports/baseball/teams/5')
28
- end
24
+ context 'when league is not passed' do
25
+ it 'should raise an argument error' do
26
+ expect { @client.team(1) }.to raise_error(ArgumentError)
29
27
  end
30
28
  end
31
29
 
32
- context 'with a sport and a league param' do
33
- it 'should include the sport and league params in the request' do
34
- stub_get('sports/baseball/mlb/teams')
35
- @client.teams(sport: 'baseball', league: 'mlb')
36
- assert_requested :get, espn_url('sports/baseball/mlb/teams')
30
+ context 'when invalid league is passed' do
31
+ it 'should raise an argument error' do
32
+ expect { @client.team(1, :foobar) }.to raise_error(ArgumentError)
37
33
  end
34
+ end
35
+ end
38
36
 
39
- context 'with a team_id param' do
40
- it 'should include the sport, league and team_id params' do
41
- stub_get('sports/baseball/mlb/teams/1')
42
- @client.teams(sport: 'baseball', league: 'mlb', team_id: 1)
43
- assert_requested :get, espn_url('sports/baseball/mlb/teams/1')
44
- end
37
+ describe '#teams' do
38
+ it 'returns an array of teams' do
39
+ @client.teams(:mlb).first.name.should eq('Phillies')
40
+ end
41
+
42
+ it 'gets all teams for a given league' do
43
+ @client.teams(:nba)
44
+ assert_requested :get, espn_url('sports/basketball/nba/teams')
45
+ end
46
+
47
+ context 'when league is not passed' do
48
+ it 'should raise an argument error' do
49
+ expect { @client.teams }.to raise_error(ArgumentError)
45
50
  end
46
51
  end
47
52
 
48
- context 'with a team_id param' do
49
- it 'should include the team_id in the request' do
50
- stub_get('sports/teams/5')
51
- @client.teams(team_id: 5)
52
- assert_requested :get, espn_url('sports/teams/5')
53
+ context 'when invalid league is passed' do
54
+ it 'should raise an argument error' do
55
+ expect { @client.teams(:foobar) }.to raise_error(ArgumentError)
53
56
  end
54
57
  end
55
58
 
56
- context 'with a league param and no sport param' do
57
- it 'should not include either in the request' do
58
- stub_get('sports/teams')
59
- @client.teams(league: 'mlb')
60
- assert_requested :get, espn_url('sports/teams')
59
+ context 'when passing values as args' do
60
+ context 'when passing a league' do
61
+ it 'should map that league to a sport' do
62
+ @client.teams(:mlb)
63
+ assert_requested :get, espn_url('sports/baseball/mlb/teams')
64
+ end
61
65
  end
62
66
 
63
- context 'with a team_id param' do
64
- it 'should include the team_id in the request' do
65
- stub_get('sports/teams/2')
66
- @client.teams(league: 'mlb', team_id: 2)
67
- assert_requested :get, espn_url('sports/teams/2')
67
+ context 'when passing a sport and a league' do
68
+ it 'should get teams for that sport and league' do
69
+ @client.teams(:football, :nfl)
70
+ assert_requested :get, espn_url('sports/football/nfl/teams')
68
71
  end
69
72
  end
70
73
  end
71
74
 
75
+ context 'when passing values in the opts hash' do
76
+ context 'when passing a league' do
77
+ it 'should map that league to a sport' do
78
+ @client.teams(league: 'mlb')
79
+ assert_requested :get, espn_url('sports/baseball/mlb/teams')
80
+ end
81
+ end
82
+
83
+ context 'when passing a sport and a league' do
84
+ it 'should get teams for that sport and league' do
85
+ @client.teams(sport: 'football', league: 'nfl')
86
+ assert_requested :get, espn_url('sports/football/nfl/teams')
87
+ end
88
+ end
89
+ end
72
90
  end
73
91
  end
@@ -3,27 +3,23 @@ require 'spec_helper'
3
3
  describe ESPN::Client::Video do
4
4
  before do
5
5
  @client = ESPN::Client.new
6
+ stub_get 'videos.json'
6
7
  end
7
8
 
8
- it 'should get all clips' do
9
- stub_get('video/channels/clips')
10
- @client.video
11
- assert_requested :get, espn_url('video/channels/clips')
9
+ it 'should return an array of channels' do
10
+ results = @client.videos(1)
11
+ results.first.name.should eq('This is a video channel.')
12
12
  end
13
13
 
14
- context 'with a category_id param' do
15
- it 'should include the category_id in the request' do
16
- stub_get('video/channels/1/clips')
17
- @client.video(category_id: 1)
18
- assert_requested :get, espn_url('video/channels/1/clips')
19
- end
14
+ it 'should get all clips for a given channel' do
15
+ @client.videos(1)
16
+ assert_requested :get, espn_url('video/channels/1/clips')
20
17
  end
21
18
 
22
19
  context 'with a clip_id param' do
23
20
  it 'should include the clip_id in the request' do
24
- stub_get('video/channels/clips/2')
25
- @client.video(clip_id: 2)
26
- assert_requested :get, espn_url('video/channels/clips/2')
21
+ @client.videos(1, clip_id: 2)
22
+ assert_requested :get, espn_url('video/channels/1/clips/2')
27
23
  end
28
24
  end
29
25