espn_rb 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,16 +1,16 @@
1
1
  # EspnRb
2
2
 
3
- A ruby wrapper for the ESPN api. It allows you to interact, in a semantically pleasing way, with the ESPN api. Currently it only allows access to the publicly available Headline API which can be found [here](http://developer.espn.com/docs/headlines). I am working to bring more of ESPN's features to espn_rb. While I do that I'll try to keep this document updated to its current functionality. That said, I hope you enjoy.
3
+ A ruby wrapper for the ESPN api. It allows you to interact, in a semantically pleasing way, with the ESPN api. Currently it only allows access to the publicly available Headline API which can be found [here](http://developer.espn.com/docs/headlines). I am working to bring more of ESPN's features to espn_rb. While I do that I'll try to keep this document updated to its current functionality. That said, I hope you enjoy.
4
4
 
5
- ## Installation
5
+ ![ESPN api logo](http://a.espncdn.com/i/apis/attribution/espn-api-black_200.png "See more branding options at developer.espn.com/branding") in mind. ^_^
6
6
 
7
- ### As of Version 0.0.3
7
+ ## Installation
8
8
 
9
9
  Add this line to your application's Gemfile:
10
-
11
- gem 'espn_rb'
12
-
13
- In order to use espn_rb you need to first get an API key from ESPN [ESPN api request](http://developer.espn.com/member/register). Once you've gotten that squared away you can use the public requests straight away.
10
+
11
+ gem "espn_rb"
12
+
13
+ In order to use espn_rb you need to first get an API key from ESPN [ESPN api request](http://developer.espn.com/member/register). Once you've gotten that squared away you can use the public requests straight away.
14
14
 
15
15
 
16
16
  ## Set your API key
@@ -18,7 +18,7 @@ In order to use espn_rb you need to first get an API key from ESPN [ESPN api req
18
18
  The easiest way to set your api key for use with espn_rb is to export it as an environment variable. Do that like so:
19
19
 
20
20
  ```sh
21
- $ export espn_api_key=YOUR_SUPER_SECRET_API_KEY
21
+ $ export espn_api_key=YOUR_SUPER_SECRET_API_KEY
22
22
  ```
23
23
 
24
24
  If you want to pass it in to your objects you may do so explicitly like so:
@@ -27,7 +27,7 @@ If you want to pass it in to your objects you may do so explicitly like so:
27
27
  espn = EspnRb.headlines(:api_key => YOUR_SUPER_SECRET_API_KEY)
28
28
  ```
29
29
 
30
- For the sake of simplicity all my examples will assume that you have exported the API key as an environment variable.
30
+ For the sake of simplicity all my examples will assume that you have exported the API key as an environment variable.
31
31
 
32
32
  ## Espn Headines
33
33
 
@@ -40,22 +40,44 @@ require 'espn_rb'
40
40
  espn = Espn.headlines
41
41
 
42
42
  espn.all
43
+ #=> HeadlineResponse
43
44
  ```
44
45
 
45
46
  Which will return an HeadlineResponse object.
46
47
 
47
- #### Get ESPN response as JSON
48
+ #### Get ESPN response as a hash
48
49
 
49
50
  To get the response straight from the horses' mouth:
50
51
 
51
52
  ```ruby
52
53
  # from above
53
-
54
+
54
55
  espn.all.response
55
- #=> ESPN's response string as JSON
56
+ #=> ESPN's response string as a hash
56
57
  ```
57
58
 
58
- The raw response from ESPN will give you the top ten stories meeting your criteria.
59
+ The raw response from ESPN will give you the top ten stories meeting your criteria.
60
+
61
+ ### HeadlineResponse
62
+
63
+ Now includes Enumerable which allows you to treat the HeadlineResponse as an iterable object.
64
+
65
+ ```ruby
66
+ espn = EspnRb.headlines
67
+ espn.all.map(&:title)
68
+
69
+ #=> ["Celtics-Clippers Preview",
70
+ # "Warriors 97, Clippers 93",
71
+ # "Hawks 106, Kings 99",
72
+ # "Warriors-Clippers, Box",
73
+ # "Warriors 97, Clippers 93",
74
+ # "Hawks 106, Kings 99",
75
+ # "Bucks-Nets Preview",
76
+ # "Hawks-Kings, Box",
77
+ # "Grizzlies 94, Nuggets 91",
78
+ # "Grizzlies-Nuggets, Box"]
79
+
80
+ ```
59
81
 
60
82
  #### Collections
61
83
 
@@ -66,9 +88,9 @@ Since the above response is a basic collection and each headline share many comm
66
88
 
67
89
  # Available methods are [headlines descriptions sources bylines types]
68
90
 
69
- espn.all.response.titles
91
+ espn.all.response.titles
70
92
  #=> ["array", "of", "ESPN", "titles"]
71
-
93
+
72
94
  espn.all.response.descriptions
73
95
  #=> ["array", "of", "ESPN", "descriptions"]
74
96
 
@@ -84,26 +106,49 @@ espn = EspnRb.headlines
84
106
 
85
107
  espn.nba(:news) #=> HeadlineResponse
86
108
  espn.nba(:top) #=> HeadlineResponse
87
- espn.nba({:for_date => "2012-03-09"}) #=> HeadlineResponse
109
+ espn.nba(:for_date => "2012-03-09") #=> HeadlineResponse # Will include all stories for that date
110
+ espn.nba(:for_athlete => "1234") #=> HeadlineResponse # Will include all stories for that athleteId
88
111
 
89
112
  ```
90
113
 
91
- #### HeadlineItem
114
+ ### HeadlineItem
92
115
 
93
- The HeadlineResponse Object holdes in it the headlines split into HeadlineItems. Here is where you can get Specific information about each story. Here are some of the options.
116
+ The HeadlineResponse Object holdes in it the headlines split into HeadlineItems. Here is where you can get Specific information about each story. Some of the options are:
94
117
 
95
118
  ```ruby
96
119
  espn = EspnRb.headlines
97
- headline_response = espn.nba[2] #=> HeadlineItem
120
+ headline_item = espn.nba[2] #=> HeadlineItem
98
121
 
99
- headline_response.web_url #=> "http://sports.espn.go.com/espn/wire?section=nba&id=7664408&ex_cid=espnapi_public"
100
- headline_response.id #=> 7664408
101
- headline_response.title #=> "Mavericks-Kings Preview"
122
+ headline_item.web_url #=> "http://sports.espn.go.com/espn/wire?section=nba&id=7664408&ex_cid=espnapi_public"
123
+ headline_item.id #=> 7664408
124
+ headline_item.title #=> "Mavericks-Kings Preview"
125
+ headline_item.athletes #=> ["Johnny B", "Freddie Flintstone", "Etc"]
126
+ headline_item.leagues #=> ["46"]
127
+ headline_item.athlete_ids #=> ["123", "132", "123"]
102
128
 
103
129
  # More to come in future versions.
104
130
  headline_response.headline #=> JSON hash from original response.
105
131
  ```
106
132
 
133
+ HeadlineItem will now also respond to #images which will return an HeadlineResponse::HeadlineItem::Images class which contains the images associated with the HeadlineItem in a class that is also enumerable which lets you access the images with nice little methods like:
134
+
135
+ ```ruby
136
+ espn = EspnRb.headlines
137
+ headline_item = espn.nfl[2]
138
+
139
+ headline_item.images => HeadlineResponse::HeadlineItem::Images
140
+
141
+ # or to actually use the images
142
+
143
+ images = headline_item.images
144
+
145
+ images.first.landscape #=> true
146
+ images.first.url #=> http://path-to-img.com/blah-blah-blah
147
+
148
+ # or list all the urls
149
+
150
+ images.map(&:url) #=> ["list", "of", "image", "urls"]
151
+ ```
107
152
  #### HELP
108
153
 
109
154
  ```ruby
@@ -2,6 +2,7 @@ require "espn_rb/version"
2
2
  require "espn_rb/headline.rb"
3
3
  require 'espn_rb/headline_response.rb'
4
4
  require 'espn_rb/headline_item.rb'
5
+ require 'espn_rb/headline_image.rb'
5
6
  require 'espn_rb/utilities.rb'
6
7
 
7
8
  require 'json'
@@ -33,7 +33,7 @@ module EspnRb
33
33
  end
34
34
 
35
35
  # This will define singleton methods for all resources defined
36
- # in EspnRb::Headline.api_resources
36
+ # in EspnRb::Headline#api_resources
37
37
  #
38
38
  # @return [HeadlineResponse] which contains the espn response object and assocated methods.
39
39
  def create_headline_methods
@@ -86,7 +86,7 @@ module EspnRb
86
86
  api_methods[:for_athlete][:url].gsub(":athleteId", opt[:for_athlete])
87
87
  end
88
88
 
89
- # Takes EspnRb::Headline.api_methods[:for_date][:url] and subs out the options passed by user
89
+ # Takes EspnRb::Headline#api_methods[:for_date][:url] and subs out the options passed by user
90
90
  # @example internal call
91
91
  # for_date({:for_date => "2012-01-01"}) => '/news/dates/20120101'
92
92
  def for_date(opt)
@@ -0,0 +1,68 @@
1
+ class HeadlineResponse
2
+ class HeadlineItem
3
+ class Images
4
+ include Enumerable
5
+ attr_reader :images
6
+
7
+ def initialize(images)
8
+ @images = images.map {|img| Image.new img }
9
+ end
10
+
11
+ def each &block
12
+ @images.each &block
13
+ end
14
+
15
+ def [](int)
16
+ @images[int]
17
+ end
18
+ end
19
+
20
+ class Image
21
+ attr_reader :image
22
+
23
+ def initialize(image)
24
+ @image = image
25
+ end
26
+
27
+ # returns true if the image is landscape oriented.
28
+ def landscape?
29
+ @image["width"] > @image["height"]
30
+ end
31
+
32
+ # returns true if the image is portrait oriented.
33
+ def portrait?
34
+ !landscape?
35
+ end
36
+
37
+ # returns caption of image
38
+ def caption
39
+ @image["caption"]
40
+ end
41
+
42
+ # returns name of image
43
+ def name
44
+ @image["name"]
45
+ end
46
+
47
+ # returns url of image
48
+ def url
49
+ @image["url"]
50
+ end
51
+
52
+ # returns credit (attribution) of image
53
+ def credit
54
+ @image["credit"]
55
+ end
56
+
57
+ # returns width of image
58
+ def width
59
+ @image["width"]
60
+ end
61
+
62
+ # returns height of image
63
+ def height
64
+ @image["height"]
65
+ end
66
+ end
67
+ end
68
+ end
@@ -7,7 +7,7 @@ class HeadlineResponse
7
7
  end
8
8
 
9
9
  # @example
10
- # espn = EspnRb::Headline
10
+ # espn = EspnRb.headlines
11
11
  # espn.nba.first.web_url => "http://some-valid-nba-article"
12
12
  # @return [String] web_url for Item
13
13
  def web_url(mobile=false)
@@ -15,24 +15,55 @@ class HeadlineResponse
15
15
  end
16
16
 
17
17
  # @example
18
- # espn = EspnRb::Headline
18
+ # espn = EspnRb.headlines
19
19
  # espn.ncaa_football.first.title => "Gators Win!!"
20
20
  # @return [String] title for Item
21
21
  def title
22
22
  @headline["headline"]
23
23
  end
24
24
 
25
+ # @example
26
+ # espn = EspnRb.headlines
27
+ # espn.ncaa_football.first.description => "Some string about what happened in the article."
28
+ # @return [String] description for Item
29
+ def description
30
+ @headline["description"]
31
+ end
32
+
33
+ # @example
34
+ # espn = EspnRb.headlines
35
+ # espn.ncaa_football.first.source => "Page 2"
36
+ # @return [String] source for Item
37
+ def source
38
+ @headline["source"]
39
+ end
40
+
41
+ # @example
42
+ # espn = EspnRb.headlines
43
+ # espn.ncaa_football.first.last_modified => "2012-03-16T04:07:56Z"
44
+ # @return [String] title for Item
45
+ def last_modified
46
+ @headline["lastModified"]
47
+ end
48
+
49
+ # @example
50
+ # espn = EspnRb.headlines
51
+ # espn.ncaa_football.first.last_modified => "2012-03-16T04:07:56Z"
52
+ # @return [Images] Images class containing the collection of images associated with the item.
53
+ def images
54
+ Images.new @headline["images"]
55
+ end
25
56
 
26
57
  # @example
27
- # espn = EspnRb::Headline
28
- # espn.ncaa_football.first.id => 1234
58
+ # espn = EspnRb.headlines
59
+ # espn.ncaa_football.first.id => '1234'
29
60
  # @return [Integer] id for Item
30
61
  def id
31
62
  @headline["id"]
32
63
  end
33
64
 
34
65
  # @example
35
- # espn = EspnRb::Headline
66
+ # espn = EspnRb.headlines
36
67
  # espn.nba.first.api_url => "http://some-valid-nba-article"
37
68
  # @return [String] api_url for Item
38
69
  def api_url
@@ -47,7 +78,7 @@ class HeadlineResponse
47
78
  # Provides access to categories sub-hash. If available
48
79
  #
49
80
  # @example
50
- # espn = EspnRb::Headline
81
+ # espn = EspnRb.headlines
51
82
  # espn.nba.first.athletes
52
83
  # #=> ["Johnny B", "Freddie Flintstone", "Etc"]
53
84
  def athletes
@@ -57,7 +88,7 @@ class HeadlineResponse
57
88
  # Provides access to categories sub-hash. If available
58
89
  #
59
90
  # @example
60
- # espn = EspnRb::Headline
91
+ # espn = EspnRb.headlines
61
92
  # espn.nba.first.leagues
62
93
  # #=> ["46"]
63
94
  def leagues
@@ -67,7 +98,7 @@ class HeadlineResponse
67
98
  # Provides access to categories sub-hash. If available
68
99
  #
69
100
  # @example
70
- # espn = EspnRb::Headline
101
+ # espn = EspnRb.headlines
71
102
  # espn.nba.first.athlete_ids
72
103
  # #=> ["123", "132", "123"]
73
104
  def athlete_ids
@@ -3,7 +3,7 @@ class HeadlineResponse
3
3
 
4
4
  attr_reader :response,:responses
5
5
 
6
- # Sets response from EspnRb::Headline.get_results. Splits response object into composite parts
6
+ # Sets response from EspnRb::Headline#get_results. Splits response object into composite parts
7
7
  # so that we can include enumerable
8
8
  def initialize(response)
9
9
  @response = @response || response
@@ -12,13 +12,7 @@ class HeadlineResponse
12
12
 
13
13
  # define each so that Enumerable methods work properly.
14
14
  def each &block
15
- @responses.each do |response|
16
- if block_given?
17
- block.call response
18
- else
19
- yield response
20
- end
21
- end
15
+ @responses.each &block
22
16
  end
23
17
 
24
18
  # Allows the user to specify which HeadlineItem they'd like to work with via it's index in the @responses array
@@ -28,13 +22,22 @@ class HeadlineResponse
28
22
  @responses[int]
29
23
  end
30
24
 
25
+ # Returns the all the links associated with this HeadlineResponse
26
+ # @option options [String] :type Either web, or mobile.
27
+ # @return [Array] array of links.
28
+ def links(type=nil)
29
+ type = type.nil? ? "web" : type
30
+ @response["headlines"].map{|r| r["links"][type]["href"] }
31
+ end
32
+
33
+
31
34
  # Defines a few collection methods to allow the user to view all of the @response like attributes
32
35
  # @note available methods are headlines, descriptions, sources, bylines, types
33
36
  #
34
37
  # @example Valid method call
35
- # EspnRb::Headline.nba.titles #=> ['title1', 'title2', 'title3', 'title4', 'etc']
38
+ # EspnRb.headlines.nba.titles #=> ['title1', 'title2', 'title3', 'title4', 'etc']
36
39
  # @example Invalid method call
37
- # EspnRb::Headline.nba.not_an_available_method #=> nil
40
+ # EspnRb.headlines.nba.not_an_available_method #=> nil
38
41
  # @return [Array] array of like items or nil
39
42
  def method_missing(sym, *args)
40
43
  sym.to_s == "titles" ? sym = :headlines : sym
@@ -1,4 +1,4 @@
1
1
  module EspnRb
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  API_VERSION = "v1"
4
4
  end
@@ -59,6 +59,10 @@ describe EspnRb::Headline do
59
59
  it { @espn.all[1].title.should == 'Oilers-Sharks Preview' }
60
60
  it { @espn.all[1].api_url.should == 'http://api.espn.com/v1/sports/news/7651009' }
61
61
  it { @espn.all[1].id.should == 7651009 }
62
+ it { @espn.all[1].title.should == "Oilers-Sharks Preview" }
63
+ it { @espn.all[1].description.should == "The San Jose Sharks are trying to reverse what's becoming a free fall through the Western Conference standings."}
64
+ it { @espn.all[1].source.should == "Associated Press"}
65
+ it { @espn.all[1].last_modified.should == "2012-03-06T06:55:01Z"}
62
66
  end
63
67
 
64
68
  context "HeadlineItem should return an array of requested entries from categories sub-hash" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: espn_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-11 00:00:00.000000000 Z
12
+ date: 2012-03-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A Ruby wrapper for ESPN's api.
15
15
  email:
@@ -29,6 +29,7 @@ files:
29
29
  - lib/espn_rb/api_definitions/headline_methods.yaml
30
30
  - lib/espn_rb/api_definitions/headline_resources.yaml
31
31
  - lib/espn_rb/headline.rb
32
+ - lib/espn_rb/headline_image.rb
32
33
  - lib/espn_rb/headline_item.rb
33
34
  - lib/espn_rb/headline_response.rb
34
35
  - lib/espn_rb/utilities.rb