espn 0.0.2 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,16 +2,24 @@ module ESPN
2
2
  class Client
3
3
  module Sports
4
4
 
5
- # Get all sports supported by the ESPN Developer API
5
+ # Public: Get sports and leagues supported in the ESPN API.
6
6
  #
7
- # @param [Hash] options
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).
8
10
  #
9
- # @return [Hashie::Mash]
10
- #
11
- # @example
12
- # ESPN.sports()
13
- def sports(options={})
14
- get("/sports", options)
11
+ # Returns an Array of Hashie::Mash.
12
+ def sports(opts={})
13
+ url = 'sports'
14
+
15
+ unless opts[:sport].to_s.empty?
16
+ url += "/#{opts[:sport]}"
17
+ unless opts[:league].to_s.empty?
18
+ url += "/#{opts[:league]}"
19
+ end
20
+ end
21
+
22
+ get(url, opts)
15
23
  end
16
24
 
17
25
  end
@@ -0,0 +1,29 @@
1
+ module ESPN
2
+ class Client
3
+ module Standings
4
+
5
+ # Public: Get the latest league and divisional standings from ESPN.
6
+ #
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
+ #
11
+ # Returns an Array of Hashie::Mash.
12
+ def standings(opts={})
13
+ url = 'sports'
14
+
15
+ unless opts[:sport].to_s.empty?
16
+ url += "/#{opts[:sport]}"
17
+ unless opts[:league].to_s.empty?
18
+ url += "/#{opts[:league]}"
19
+ end
20
+ end
21
+
22
+ url += '/standings'
23
+
24
+ get(url, opts)
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -2,16 +2,31 @@ module ESPN
2
2
  class Client
3
3
  module Teams
4
4
 
5
- # Get all sports supported by the ESPN Developer API
5
+ # Public: Get sport team stats and information from the ESPN API.
6
6
  #
7
- # @param [Hash] options
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
+ # - :team_id - The id of the team (default: nil).
8
11
  #
9
- # @return [Hashie::Mash]
10
- #
11
- # @example
12
- # ESPN.sports()
13
- def teams(options={})
14
- get("/sports/teams", options)
12
+ # Returns an Array of Hashie::Mash.
13
+ def teams(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 += '/teams'
24
+
25
+ unless opts[:team_id].to_s.empty?
26
+ url += "/#{opts[:team_id]}"
27
+ end
28
+
29
+ get(url, opts)
15
30
  end
16
31
 
17
32
  end
@@ -0,0 +1,30 @@
1
+ module ESPN
2
+ class Client
3
+ module Video
4
+
5
+ # Public: Get video clip and channel information from ESPN.
6
+ #
7
+ # opts - Hash options used to refine the selection (default: {}).
8
+ # - :category_id - The id of the category (default: nil).
9
+ # - :clip_id - The id of the clip (default: nil).
10
+ #
11
+ # Returns an Array of Hashie::Mash.
12
+ def video(opts={})
13
+ url = 'video/channels'
14
+
15
+ unless opts[:category_id].to_s.empty?
16
+ url += "/#{opts[:category_id]}"
17
+ end
18
+
19
+ url += '/clips'
20
+
21
+ unless opts[:clip_id].to_s.empty?
22
+ url += "/#{opts[:clip_id]}"
23
+ end
24
+
25
+ get(url, opts)
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -2,35 +2,67 @@ require 'faraday'
2
2
  require 'espn/version'
3
3
 
4
4
  module ESPN
5
+
6
+ # Public: All methods useful for configuration. This module should be extended
7
+ # in the ESPN module, so ESPN can be configured.
8
+ #
9
+ # Examples
10
+ #
11
+ # module ESPN
12
+ # extend Configuration
13
+ # end
5
14
  module Configuration
6
- VALID_OPTIONS_KEYS = [
7
- :adapter,
8
- :api_version,
9
- :proxy,
10
- :api_key,
11
- :timeout,
12
- :open_timeout,
13
- :user_agent].freeze
14
15
 
16
+ # Public: Array of all configuration options for an ESPN::Client.
17
+ VALID_OPTIONS_KEYS = [:adapter, :api_version, :proxy, :api_key, :timeout,
18
+ :open_timeout, :user_agent].freeze
19
+
20
+ # Public: The default adapter used for requests.
15
21
  DEFAULT_ADAPTER = Faraday.default_adapter
22
+
23
+ # Public: The default API Version.
16
24
  DEFAULT_API_VERSION = 1
25
+
26
+ # Public: The default user agent.
17
27
  DEFAULT_USER_AGENT = "ESPN Ruby Gem #{ESPN::VERSION}".freeze
28
+
29
+ # Public: The default timeout for HTTP Requests.
18
30
  DEFAULT_TIMEOUT = 10
19
31
 
32
+ # Public: Create an attr_accessor for each VALID_OPTIONS_KEYS when this
33
+ # module is extended into another.
20
34
  attr_accessor(*VALID_OPTIONS_KEYS)
21
35
 
36
+ # Internal: Hook when this module is extended in another, we call #reset.
37
+ #
38
+ # Returns nothing.
22
39
  def self.extended(base)
23
40
  base.reset
24
41
  end
25
42
 
43
+ # Public: The ability to configure ESPN default options.
44
+ #
45
+ # Examples
46
+ #
47
+ # ESPN.configure do |c|
48
+ # c.api_key = 'abc123'
49
+ # end
50
+ #
51
+ # Yields ESPN::Configuration.
26
52
  def configure
27
53
  yield self
28
54
  end
29
55
 
56
+ # Public: Get all valid options with their defaults.
57
+ #
58
+ # Returns a Hash.
30
59
  def options
31
60
  VALID_OPTIONS_KEYS.inject({}){|o,k| o.merge!(k => send(k)) }
32
61
  end
33
62
 
63
+ # Public: Reset all valid options to their defaults.
64
+ #
65
+ # Returns nothing.
34
66
  def reset
35
67
  self.adapter = DEFAULT_ADAPTER
36
68
  self.api_version = DEFAULT_API_VERSION
@@ -9,6 +9,14 @@ module ESPN
9
9
  private
10
10
 
11
11
  def request(method, path, options)
12
+
13
+ # TODO: Decide if I want to delete these or not. There is probably
14
+ # a better way to do this, if so, by filtering them out.
15
+ %w( sport league method section team_id headline_id category_id clip_id
16
+ athlete_id event_id note_id podcast_id recording_id ).each do |k|
17
+ options.delete(k.to_sym)
18
+ end
19
+
12
20
  response = connection.send(method) do |request|
13
21
  request.url(path, options)
14
22
  request.options[:timeout] = timeout
@@ -1,3 +1,6 @@
1
1
  module ESPN
2
- VERSION = "0.0.2" unless defined?(ESPN::VERSION)
2
+
3
+ # Public: The version of the ESPN gem.
4
+ VERSION = '0.1.1'.freeze
5
+
3
6
  end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe ESPN::Client::Athletes do
4
+ before do
5
+ @client = ESPN::Client.new
6
+ end
7
+
8
+ describe '#athletes' do
9
+
10
+ it 'gets all athletes' do
11
+ stub_get('sports/athletes')
12
+ @client.athletes
13
+ assert_requested :get, espn_url('sports/athletes')
14
+ end
15
+
16
+ context 'with a sport param' do
17
+ it 'should include the sport in the request' do
18
+ stub_get('sports/baseball/athletes')
19
+ @client.athletes(sport: 'baseball')
20
+ assert_requested :get, espn_url('sports/baseball/athletes')
21
+ end
22
+
23
+ context 'with a athlete_id param' do
24
+ it 'should include the sport and athlete_id params in the request' do
25
+ stub_get('sports/baseball/athletes/5')
26
+ @client.athletes(sport: 'baseball', athlete_id: 5)
27
+ assert_requested :get, espn_url('sports/baseball/athletes/5')
28
+ end
29
+ end
30
+ end
31
+
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/athletes')
35
+ @client.athletes(sport: 'baseball', league: 'mlb')
36
+ assert_requested :get, espn_url('sports/baseball/mlb/athletes')
37
+ end
38
+
39
+ context 'with a athlete_id param' do
40
+ it 'should include the sport, league and athlete_id params' do
41
+ stub_get('sports/baseball/mlb/athletes/1')
42
+ @client.athletes(sport: 'baseball', league: 'mlb', athlete_id: 1)
43
+ assert_requested :get, espn_url('sports/baseball/mlb/athletes/1')
44
+ end
45
+ end
46
+ end
47
+
48
+ context 'with a athlete_id param' do
49
+ it 'should include the athlete_id in the request' do
50
+ stub_get('sports/athletes/5')
51
+ @client.athletes(athlete_id: 5)
52
+ assert_requested :get, espn_url('sports/athletes/5')
53
+ end
54
+ end
55
+
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/athletes')
59
+ @client.athletes(league: 'mlb')
60
+ assert_requested :get, espn_url('sports/athletes')
61
+ end
62
+
63
+ context 'with a athlete_id param' do
64
+ it 'should include the athlete_id in the request' do
65
+ stub_get('sports/athletes/2')
66
+ @client.athletes(league: 'mlb', athlete_id: 2)
67
+ assert_requested :get, espn_url('sports/athletes/2')
68
+ end
69
+ end
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe ESPN::Client::Audio do
4
+ before do
5
+ @client = ESPN::Client.new
6
+ end
7
+
8
+ it 'should get all podcasts' do
9
+ stub_get('audio/podcasts')
10
+ @client.audio
11
+ assert_requested :get, espn_url('audio/podcasts')
12
+ end
13
+
14
+ context 'with a method param' do
15
+ context 'if method is podcast_recordings' do
16
+ it 'should include podcasts/recordings in the request' do
17
+ stub_get('audio/podcasts/recordings')
18
+ @client.audio(method: 'podcast_recordings')
19
+ assert_requested :get, espn_url('audio/podcasts/recordings')
20
+ end
21
+ end
22
+
23
+ it 'should include the method in the request' do
24
+ stub_get('audio/recordings')
25
+ @client.audio(method: 'recordings')
26
+ assert_requested :get, espn_url('audio/recordings')
27
+ end
28
+ end
29
+
30
+ context 'with a podcast_id param' do
31
+ it 'should include the podcast_id in the param' do
32
+ stub_get('audio/podcasts/1')
33
+ @client.audio(podcast_id: 1)
34
+ assert_requested :get, espn_url('audio/podcasts/1')
35
+ end
36
+ end
37
+
38
+ context 'with a recording_id param' do
39
+ it 'should include the recording_id in the param' do
40
+ stub_get('audio/podcasts/2')
41
+ @client.audio(recording_id: 2)
42
+ assert_requested :get, espn_url('audio/podcasts/2')
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe ESPN::Client::Headlines do
4
+ before do
5
+ @client = ESPN::Client.new
6
+ end
7
+
8
+ describe '#headlines' do
9
+ it 'should get headlines for sports by default' do
10
+ stub_get('sports/news')
11
+ @client.headlines
12
+ assert_requested :get, espn_url('sports/news')
13
+ end
14
+
15
+ context 'with a section param' do
16
+ it 'should get headlines for that section' do
17
+ stub_get('cities/news')
18
+ @client.headlines(section: 'cities')
19
+ assert_requested :get, espn_url('cities/news')
20
+ end
21
+ end
22
+
23
+ context 'with a sport param' do
24
+ it 'should include the sport in the request' do
25
+ stub_get('sports/baseball/news')
26
+ @client.headlines(sport: 'baseball')
27
+ assert_requested :get, espn_url('sports/baseball/news')
28
+ end
29
+ end
30
+
31
+ context 'with a sport and league param' do
32
+ it 'should include both params in the request' do
33
+ stub_get('sports/baseball/mlb/news')
34
+ @client.headlines(sport: 'baseball', league: 'mlb')
35
+ assert_requested :get, espn_url('sports/baseball/mlb/news')
36
+ end
37
+ end
38
+
39
+ context 'with a league param but no sport param' do
40
+ it 'should not include the league in the request' do
41
+ stub_get('sports/news')
42
+ @client.headlines(league: 'mlb')
43
+ assert_requested :get, espn_url('sports/news')
44
+ end
45
+ end
46
+
47
+ context 'with a method param' do
48
+ it 'should include the method in the request' do
49
+ stub_get('sports/news/headlines/top')
50
+ @client.headlines(method: 'top')
51
+ assert_requested :get, espn_url('sports/news/headlines/top')
52
+ end
53
+ end
54
+
55
+ context 'with a headline_id param' do
56
+ it 'should include the headline_id in the request' do
57
+ stub_get('sports/news/5')
58
+ @client.headlines(headline_id: 5)
59
+ assert_requested :get, espn_url('sports/news/5')
60
+ end
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe ESPN::Client::Medals do
4
+ before do
5
+ @client = ESPN::Client.new
6
+ end
7
+
8
+ it 'should get all medals' do
9
+ stub_get('sports/olympics/medals')
10
+ @client.medals
11
+ assert_requested :get, espn_url('sports/olympics/medals')
12
+ end
13
+
14
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe ESPN::Client::Notes do
4
+ before do
5
+ @client = ESPN::Client.new
6
+ end
7
+
8
+ describe '#notes' do
9
+
10
+ it 'gets all notes' do
11
+ stub_get('sports/news/notes')
12
+ @client.notes
13
+ assert_requested :get, espn_url('sports/news/notes')
14
+ end
15
+
16
+ context 'with a sport param' do
17
+ it 'should include the sport in the request' do
18
+ stub_get('sports/baseball/news/notes')
19
+ @client.notes(sport: 'baseball')
20
+ assert_requested :get, espn_url('sports/baseball/news/notes')
21
+ end
22
+
23
+ context 'with a note_id param' do
24
+ it 'should include the sport and note_id params in the request' do
25
+ stub_get('sports/baseball/news/notes/5')
26
+ @client.notes(sport: 'baseball', note_id: 5)
27
+ assert_requested :get, espn_url('sports/baseball/news/notes/5')
28
+ end
29
+ end
30
+ end
31
+
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/news/notes')
35
+ @client.notes(sport: 'baseball', league: 'mlb')
36
+ assert_requested :get, espn_url('sports/baseball/mlb/news/notes')
37
+ end
38
+
39
+ context 'with a note_id param' do
40
+ it 'should include the sport, league and note_id params' do
41
+ stub_get('sports/baseball/mlb/news/notes/1')
42
+ @client.notes(sport: 'baseball', league: 'mlb', note_id: 1)
43
+ assert_requested :get, espn_url('sports/baseball/mlb/news/notes/1')
44
+ end
45
+ end
46
+ end
47
+
48
+ context 'with a note_id param' do
49
+ it 'should include the note_id in the request' do
50
+ stub_get('sports/news/notes/5')
51
+ @client.notes(note_id: 5)
52
+ assert_requested :get, espn_url('sports/news/notes/5')
53
+ end
54
+ end
55
+
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/news/notes')
59
+ @client.notes(league: 'mlb')
60
+ assert_requested :get, espn_url('sports/news/notes')
61
+ end
62
+
63
+ context 'with a note_id param' do
64
+ it 'should include the note_id in the request' do
65
+ stub_get('sports/news/notes/2')
66
+ @client.notes(league: 'mlb', note_id: 2)
67
+ assert_requested :get, espn_url('sports/news/notes/2')
68
+ end
69
+ end
70
+ end
71
+
72
+ end
73
+ end