npr 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -4
- data/CHANGELOG.md +27 -1
- data/README.md +5 -10
- data/lib/npr/api/client.rb +6 -6
- data/lib/npr/api/message.rb +5 -5
- data/lib/npr/api/query_builder.rb +22 -23
- data/lib/npr/api/response.rb +7 -7
- data/lib/npr/concern/attr_typecast.rb +11 -13
- data/lib/npr/concern/links_association.rb +26 -0
- data/lib/npr/concern/relation.rb +17 -17
- data/lib/npr/concern/shallow_attributes.rb +12 -12
- data/lib/npr/configuration.rb +27 -27
- data/lib/npr/entity/audio.rb +3 -4
- data/lib/npr/entity/author.rb +3 -3
- data/lib/npr/entity/base.rb +1 -1
- data/lib/npr/entity/book.rb +2 -2
- data/lib/npr/entity/book_edition.rb +5 -5
- data/lib/npr/entity/byline.rb +4 -3
- data/lib/npr/entity/crop.rb +2 -2
- data/lib/npr/entity/enlargement.rb +2 -2
- data/lib/npr/entity/external_asset.rb +27 -0
- data/lib/npr/entity/formats.rb +2 -2
- data/lib/npr/entity/image.rb +4 -4
- data/lib/npr/entity/intro_text.rb +4 -4
- data/lib/npr/entity/link.rb +3 -3
- data/lib/npr/entity/list.rb +2 -4
- data/lib/npr/entity/list_text.rb +2 -2
- data/lib/npr/entity/member.rb +1 -1
- data/lib/npr/entity/member_byline.rb +2 -2
- data/lib/npr/entity/member_promo_art.rb +2 -2
- data/lib/npr/entity/mp3.rb +4 -4
- data/lib/npr/entity/name.rb +4 -4
- data/lib/npr/entity/organization.rb +2 -2
- data/lib/npr/entity/paragraph.rb +2 -2
- data/lib/npr/entity/parent.rb +23 -0
- data/lib/npr/entity/permissions.rb +14 -1
- data/lib/npr/entity/program.rb +7 -5
- data/lib/npr/entity/promo_art.rb +2 -2
- data/lib/npr/entity/promo_art_book_edition.rb +2 -3
- data/lib/npr/entity/provider.rb +4 -4
- data/lib/npr/entity/pull_quote.rb +1 -1
- data/lib/npr/entity/related_link.rb +4 -4
- data/lib/npr/entity/show.rb +3 -3
- data/lib/npr/entity/story.rb +42 -72
- data/lib/npr/entity/text.rb +7 -7
- data/lib/npr/entity/title.rb +4 -4
- data/lib/npr/entity/transcript.rb +2 -2
- data/lib/npr/errors.rb +1 -1
- data/lib/npr/version.rb +1 -1
- data/lib/npr.rb +3 -0
- data/spec/fixtures/fetch_formats.rb +4 -4
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/api/client_spec.rb +4 -4
- data/spec/unit/api/message_spec.rb +9 -9
- data/spec/unit/api/query_builder_spec.rb +39 -39
- data/spec/unit/api/response_spec.rb +8 -8
- data/spec/unit/configuration_spec.rb +14 -14
- data/spec/unit/entity/audio_spec.rb +6 -6
- data/spec/unit/entity/author_spec.rb +3 -3
- data/spec/unit/entity/base_spec.rb +19 -19
- data/spec/unit/entity/book_edition_spec.rb +4 -4
- data/spec/unit/entity/book_spec.rb +2 -2
- data/spec/unit/entity/byline_spec.rb +3 -3
- data/spec/unit/entity/collection_spec.rb +1 -1
- data/spec/unit/entity/crop_spec.rb +2 -2
- data/spec/unit/entity/englargement_spec.rb +2 -2
- data/spec/unit/entity/external_assset_spec.rb +39 -0
- data/spec/unit/entity/formats_spec.rb +4 -4
- data/spec/unit/entity/image_spec.rb +7 -7
- data/spec/unit/entity/intro_text_spec.rb +3 -3
- data/spec/unit/entity/link_spec.rb +3 -3
- data/spec/unit/entity/list_spec.rb +3 -3
- data/spec/unit/entity/list_text_spec.rb +3 -3
- data/spec/unit/entity/member_byline_spec.rb +2 -2
- data/spec/unit/entity/member_promo_art_spec.rb +2 -2
- data/spec/unit/entity/member_spec.rb +4 -4
- data/spec/unit/entity/mp3_spec.rb +4 -4
- data/spec/unit/entity/name_spec.rb +3 -3
- data/spec/unit/entity/organization_spec.rb +2 -2
- data/spec/unit/entity/paragraph_spec.rb +3 -3
- data/spec/unit/entity/parent_spec.rb +39 -0
- data/spec/unit/entity/permissions_spec.rb +16 -6
- data/spec/unit/entity/program_spec.rb +7 -3
- data/spec/unit/entity/promo_art_book_edition_spec.rb +2 -2
- data/spec/unit/entity/promo_art_spec.rb +2 -2
- data/spec/unit/entity/pull_quote_spec.rb +2 -2
- data/spec/unit/entity/related_link_spec.rb +7 -7
- data/spec/unit/entity/show_spec.rb +3 -3
- data/spec/unit/entity/story_spec.rb +35 -36
- data/spec/unit/entity/text_spec.rb +4 -4
- data/spec/unit/entity/title_spec.rb +3 -3
- data/spec/unit/entity/transcript_spec.rb +2 -2
- metadata +10 -8
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,20 +1,46 @@
|
|
1
|
+
### Version 1.2.0 (2013-08-05)
|
2
|
+
##### Additions
|
3
|
+
* Added support for the `parent` node.
|
4
|
+
* Added the `#link_for` method for any node which has (multiple) nested links.
|
5
|
+
* Added `#download?`, `#stream?`, and `#embed?` methods to Permission, to return
|
6
|
+
booleans of whether that permission is allowed.
|
7
|
+
* Added support for the `externalAsset` node, as `Story#external_assets (array)`
|
8
|
+
|
9
|
+
##### Changes
|
10
|
+
* None
|
11
|
+
|
12
|
+
##### Deprecations
|
13
|
+
* None
|
14
|
+
|
15
|
+
|
16
|
+
|
1
17
|
### Version 1.1.0 (2013-06-05)
|
18
|
+
|
19
|
+
##### Additions
|
2
20
|
* Allow passing `:url` option to Client, and `:path` option to `Client#query`
|
3
21
|
* Allow passing `:apiKey` and `:output` to `Client#query`
|
22
|
+
* Add an Image#crop method, to find a specific crop by its type.
|
23
|
+
|
24
|
+
##### Changes
|
4
25
|
* Handle an HTTP Error from the API better. If the API response is not a
|
5
26
|
success (as defined by `Faraday::Response#success?`), then an
|
6
27
|
`NPR::APIError` will be raised. An error was being raised before, but
|
7
28
|
it was due to a nil error (when response.body was nil), which could be confusing.
|
8
|
-
|
29
|
+
|
30
|
+
##### Deprecations
|
31
|
+
* None
|
32
|
+
|
9
33
|
|
10
34
|
|
11
35
|
### Version 1.0.0
|
12
36
|
* Stable release
|
13
37
|
|
14
38
|
|
39
|
+
|
15
40
|
### Version 0.1.2 (2012-12-30)
|
16
41
|
* Critical bug fix having to do with JSON parsing.
|
17
42
|
|
18
43
|
|
44
|
+
|
19
45
|
### Version 0.1.1 (2012-12-16)
|
20
46
|
* First stable release.
|
data/README.md
CHANGED
@@ -3,7 +3,8 @@
|
|
3
3
|
[![Build Status](https://travis-ci.org/bricker/npr.png)](https://travis-ci.org/bricker/npr)
|
4
4
|
|
5
5
|
A simple Ruby client for the
|
6
|
-
[NPR API](http://www.npr.org/api/index).
|
6
|
+
[NPR API](http://www.npr.org/api/index). Alternative documentation for the NPR
|
7
|
+
API can be found at <http://dev.npr.org>.
|
7
8
|
|
8
9
|
|
9
10
|
## Support
|
@@ -11,7 +12,7 @@ A simple Ruby client for the
|
|
11
12
|
This gem is tested with these versions (but may
|
12
13
|
work with others):
|
13
14
|
|
14
|
-
* Ruby 1.9.2, 1.9.3,
|
15
|
+
* Ruby 1.9.2, 1.9.3, 2.0.0
|
15
16
|
* NPR API version 0.94
|
16
17
|
|
17
18
|
|
@@ -105,17 +106,11 @@ for all of the options.
|
|
105
106
|
* Search functionality
|
106
107
|
* Add "select" method to QueryBuilder for the "fields" input
|
107
108
|
* Abstract attributes so they're not tied directly to the API response.
|
108
|
-
|
109
|
-
* Example: "apiKey" => :api_key
|
109
|
+
* Make all attributes snake_case
|
110
110
|
* Support for "correction" node
|
111
111
|
* Support for "container" node
|
112
|
-
* Support for "parent" node
|
113
112
|
* Support for "thumbnail" node
|
114
|
-
* Support a way to return raw
|
115
|
-
* HTML / Javascript (for views)
|
116
|
-
* JSON
|
117
|
-
* ATOM
|
118
|
-
* RSS
|
113
|
+
* Support a way to return the raw NPRML if requested.
|
119
114
|
|
120
115
|
|
121
116
|
## Contributing
|
data/lib/npr/api/client.rb
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
#
|
10
10
|
# You can also set params via +client.params+
|
11
11
|
#
|
12
|
-
# Params should be passed in exactly as they will be sent
|
12
|
+
# Params should be passed in exactly as they will be sent
|
13
13
|
# to the API. Example: "apiKey", not "api_key"
|
14
14
|
# See the API documentation for what those params are.
|
15
15
|
#
|
@@ -56,7 +56,7 @@ module NPR
|
|
56
56
|
#
|
57
57
|
def query(params={})
|
58
58
|
path = params.delete(:path) || NPR::Configuration::API_QUERY_PATH
|
59
|
-
|
59
|
+
|
60
60
|
response = connection.get do |request|
|
61
61
|
request.url path
|
62
62
|
request.headers['Content-Type'] = "application/json"
|
@@ -65,7 +65,7 @@ module NPR
|
|
65
65
|
request.params['output'] ||= "json" # Only JSON is supported.
|
66
66
|
request.params['apiKey'] ||= @apiKey
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
if response.success?
|
70
70
|
NPR::API::Response.new(response)
|
71
71
|
else
|
@@ -74,11 +74,11 @@ module NPR
|
|
74
74
|
end
|
75
75
|
|
76
76
|
#-----------------
|
77
|
-
|
77
|
+
|
78
78
|
private
|
79
|
-
|
79
|
+
|
80
80
|
#-----------------
|
81
|
-
|
81
|
+
|
82
82
|
def connection
|
83
83
|
@connection ||= begin
|
84
84
|
Faraday.new @url do |conn|
|
data/lib/npr/api/message.rb
CHANGED
@@ -8,26 +8,26 @@ module NPR
|
|
8
8
|
module API
|
9
9
|
class Message
|
10
10
|
include NPR::Concern::ShallowAttributes
|
11
|
-
|
11
|
+
|
12
12
|
attr_accessor :id, :level
|
13
13
|
shallow_attribute "text", "timestamp"
|
14
14
|
|
15
15
|
#------------------
|
16
|
-
|
16
|
+
|
17
17
|
def initialize(json)
|
18
18
|
@_json = json
|
19
19
|
@id = @_json["id"]
|
20
20
|
@level = @_json["level"]
|
21
|
-
|
21
|
+
|
22
22
|
extract_shallow_attributes(@_json)
|
23
23
|
end
|
24
24
|
|
25
25
|
#------------------
|
26
|
-
|
26
|
+
|
27
27
|
def error?
|
28
28
|
self.level == "error"
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
#------------------
|
32
32
|
|
33
33
|
def warning?
|
@@ -11,16 +11,16 @@ module NPR
|
|
11
11
|
:offset,
|
12
12
|
:set
|
13
13
|
]
|
14
|
-
|
14
|
+
|
15
15
|
attr_reader :_klass, :builder
|
16
16
|
|
17
17
|
#-----------------------
|
18
|
-
|
18
|
+
|
19
19
|
def initialize(klass)
|
20
20
|
@_klass = klass
|
21
21
|
@builder = {}
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
#-----------------------
|
25
25
|
# Build the params hash.
|
26
26
|
# This is automatically called by #to_a, so it probably
|
@@ -31,13 +31,13 @@ module NPR
|
|
31
31
|
else
|
32
32
|
conditions = @builder[:conditions]
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
params = conditions || {}
|
36
36
|
params[:sort] = @builder[:order] if @builder[:order]
|
37
37
|
params[:numResults] = @builder[:limit] if @builder[:limit]
|
38
38
|
params[:startNum] = @builder[:offset] if @builder[:offset]
|
39
39
|
params.merge!(@builder[:extra]) if @builder[:extra]
|
40
|
-
|
40
|
+
|
41
41
|
params
|
42
42
|
end
|
43
43
|
|
@@ -68,19 +68,19 @@ module NPR
|
|
68
68
|
def to_a
|
69
69
|
response = self.query
|
70
70
|
stories = []
|
71
|
-
|
71
|
+
|
72
72
|
if response.list
|
73
73
|
stories = Array.wrap(response.list.stories)
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
stories
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
#-----------------------
|
80
80
|
# Fire the query and return the full Response object
|
81
|
-
#
|
81
|
+
#
|
82
82
|
# Example:
|
83
|
-
#
|
83
|
+
#
|
84
84
|
# query = NPR::API::QueryBuilder.new(NPR::Story)
|
85
85
|
# query.where(:id => [100, 150]).query
|
86
86
|
# #=> NPR::API::Response
|
@@ -110,7 +110,7 @@ module NPR
|
|
110
110
|
@builder[:extra] = (@builder[:extra] || {}).merge(params)
|
111
111
|
self
|
112
112
|
end
|
113
|
-
|
113
|
+
|
114
114
|
#-----------------------
|
115
115
|
# Merge in the passed-in conditions to what
|
116
116
|
# already exists.
|
@@ -132,7 +132,7 @@ module NPR
|
|
132
132
|
# last_week = Time.new(2012, 10, 21)
|
133
133
|
# yesterday = Time.new(2012, 10, 25)
|
134
134
|
# query.where(date: (last_week..yesterday))
|
135
|
-
#
|
135
|
+
#
|
136
136
|
def where(conditions)
|
137
137
|
@builder[:conditions] = (@builder[:conditions] || {}).merge(conditions)
|
138
138
|
self
|
@@ -168,9 +168,9 @@ module NPR
|
|
168
168
|
@builder[:limit] = limit
|
169
169
|
self
|
170
170
|
end
|
171
|
-
|
171
|
+
|
172
172
|
#-----------------------
|
173
|
-
# Offset the number of results
|
173
|
+
# Offset the number of results
|
174
174
|
# Useful for pagination
|
175
175
|
#
|
176
176
|
# Accepts an Integer.
|
@@ -183,35 +183,34 @@ module NPR
|
|
183
183
|
@builder[:offset] = offset
|
184
184
|
self
|
185
185
|
end
|
186
|
-
|
186
|
+
|
187
187
|
#-----------------------
|
188
|
-
|
188
|
+
|
189
189
|
private
|
190
|
-
|
190
|
+
|
191
191
|
def client
|
192
192
|
@client ||= NPR::API::Client.new(:apiKey => NPR.config.apiKey)
|
193
193
|
end
|
194
|
-
|
194
|
+
|
195
195
|
#-----------------------
|
196
|
-
|
196
|
+
|
197
197
|
def parse_conditions(conditions)
|
198
198
|
parse_conditions!(conditions.dup)
|
199
199
|
end
|
200
200
|
|
201
201
|
#-----------------------
|
202
|
-
|
202
|
+
|
203
203
|
def parse_conditions!(conditions)
|
204
|
-
|
205
204
|
if conditions[:id].is_a? Array
|
206
205
|
conditions[:id] = conditions[:id].join(",")
|
207
206
|
end
|
208
|
-
|
207
|
+
|
209
208
|
if conditions[:date].is_a? Range
|
210
209
|
conditions[:startDate] = conditions[:date].first
|
211
210
|
conditions[:endDate] = conditions[:date].last
|
212
211
|
conditions.delete(:date)
|
213
212
|
end
|
214
|
-
|
213
|
+
|
215
214
|
conditions
|
216
215
|
end
|
217
216
|
end # QueryBuilder
|
data/lib/npr/api/response.rb
CHANGED
@@ -8,24 +8,24 @@ module NPR
|
|
8
8
|
module API
|
9
9
|
class Response
|
10
10
|
include NPR::Concern::Relation
|
11
|
-
|
11
|
+
|
12
12
|
has_many "messages", :key => "message", :class_name => NPR::API::Message
|
13
13
|
attr_reader :raw, :version, :messages, :list
|
14
14
|
|
15
15
|
#--------------------------
|
16
|
-
|
16
|
+
|
17
17
|
def initialize(response)
|
18
18
|
create_relations(response)
|
19
|
-
|
19
|
+
|
20
20
|
@_response = response
|
21
21
|
@raw = response.body
|
22
|
-
|
23
|
-
@version
|
24
|
-
|
22
|
+
|
23
|
+
@version = @raw["version"]
|
24
|
+
|
25
25
|
if list = @raw["list"]
|
26
26
|
@list = NPR::Entity::List.new(list)
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
Array.wrap(@raw["message"]).each do |message|
|
30
30
|
@messages.push NPR::API::Message.new(message)
|
31
31
|
end
|
@@ -9,7 +9,7 @@ module NPR
|
|
9
9
|
ATTR_TYPES = {
|
10
10
|
"id" => :string_to_i,
|
11
11
|
"partnerId" => :string_to_i,
|
12
|
-
"storyDate" => :string_time_parse,
|
12
|
+
"storyDate" => :string_time_parse,
|
13
13
|
"pubDate" => :string_time_parse,
|
14
14
|
"lastModifiedDate" => :string_time_parse,
|
15
15
|
"showDate" => :string_time_parse,
|
@@ -19,41 +19,39 @@ module NPR
|
|
19
19
|
"timestamp" => :string_time_at,
|
20
20
|
"duration" => :string_to_i
|
21
21
|
}
|
22
|
-
|
22
|
+
|
23
23
|
module Concern
|
24
24
|
module AttrTypecast
|
25
|
-
|
25
|
+
|
26
26
|
private
|
27
|
-
|
27
|
+
|
28
28
|
#------------------
|
29
29
|
# Typecasting methods
|
30
30
|
def string_to_i(value)
|
31
31
|
value.to_i
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
#------------------
|
35
|
-
|
35
|
+
|
36
36
|
def string_time_parse(value)
|
37
37
|
!value.empty? ? Time.parse(value) : nil
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
#------------------
|
41
|
-
|
41
|
+
|
42
42
|
def string_time_at(value)
|
43
43
|
Time.at(value.to_f)
|
44
44
|
end
|
45
|
-
|
46
|
-
|
47
|
-
#------------------
|
45
|
+
|
48
46
|
#------------------
|
49
|
-
|
47
|
+
|
50
48
|
def attr_typecast(key, value)
|
51
49
|
if method = NPR::ATTR_TYPES[key]
|
52
50
|
send method, value
|
53
51
|
else
|
54
52
|
value
|
55
53
|
end
|
56
|
-
end
|
54
|
+
end
|
57
55
|
end # AttrTypecast
|
58
56
|
end # Concern
|
59
57
|
end # NPR
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module NPR
|
2
|
+
module Concern
|
3
|
+
module LinksAssociation
|
4
|
+
def self.included(base)
|
5
|
+
base.has_many "links", :key => "link", :class_name => NPR::Entity::Link
|
6
|
+
end
|
7
|
+
|
8
|
+
#-------------------------
|
9
|
+
# Find links of the passed in type.
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
#
|
13
|
+
# story.link_for("html") #=> http://npr.org/...
|
14
|
+
# story.link_for("nothing") #=> nil
|
15
|
+
#
|
16
|
+
# Returns an the content of that link if found,
|
17
|
+
# or nil if not found.
|
18
|
+
#
|
19
|
+
def link_for(type)
|
20
|
+
if link = self.links.find { |link| link.type == type }
|
21
|
+
link.to_s
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end # LinkAssociation
|
25
|
+
end # Concern
|
26
|
+
end # NPR
|
data/lib/npr/concern/relation.rb
CHANGED
@@ -9,13 +9,13 @@ module NPR
|
|
9
9
|
end
|
10
10
|
|
11
11
|
#-----------------
|
12
|
-
|
12
|
+
|
13
13
|
module ClassMethods
|
14
14
|
#-----------------
|
15
15
|
# Define a relationship
|
16
16
|
# Similar to ActiveRecord's +has_many+
|
17
17
|
#
|
18
|
-
# Arguments:
|
18
|
+
# Arguments:
|
19
19
|
#
|
20
20
|
# * name (String) - the name of the relation
|
21
21
|
# * options (Hash)
|
@@ -28,7 +28,7 @@ module NPR
|
|
28
28
|
#
|
29
29
|
def has_many(name, options)
|
30
30
|
relation = build_relation(name, options)
|
31
|
-
|
31
|
+
|
32
32
|
# Define getter and setter for this attribute
|
33
33
|
# Forces the relation into an empty array when
|
34
34
|
# it's first accessed.
|
@@ -36,7 +36,7 @@ module NPR
|
|
36
36
|
define_method name do
|
37
37
|
instance_variable_get("@#{name}") || instance_variable_set("@#{name}", [])
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
_has_many_relations.push relation
|
41
41
|
end
|
42
42
|
|
@@ -47,25 +47,25 @@ module NPR
|
|
47
47
|
attr_accessor name
|
48
48
|
_has_one_relations.push relation
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
#-----------------
|
52
|
-
|
52
|
+
|
53
53
|
def _has_many_relations
|
54
54
|
@_has_many_relations ||= []
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
def _has_one_relations
|
58
58
|
@_has_one_relations ||= []
|
59
59
|
end
|
60
60
|
|
61
61
|
#-----------------
|
62
|
-
|
62
|
+
|
63
63
|
private
|
64
64
|
attr_writer :_has_many_relations, :_has_one_relations
|
65
|
-
|
65
|
+
|
66
66
|
def build_relation(name, options)
|
67
|
-
relation = {
|
68
|
-
:name => name,
|
67
|
+
relation = {
|
68
|
+
:name => name,
|
69
69
|
:class_name => options[:class_name],
|
70
70
|
:key => options[:key] || name
|
71
71
|
}
|
@@ -76,22 +76,22 @@ module NPR
|
|
76
76
|
#-----------------
|
77
77
|
|
78
78
|
private
|
79
|
-
|
79
|
+
|
80
80
|
#-----------------
|
81
|
-
|
81
|
+
|
82
82
|
def create_relations(json)
|
83
83
|
self.class._has_many_relations.each do |relation|
|
84
84
|
collection = []
|
85
|
-
|
85
|
+
|
86
86
|
if node = json[relation[:key]]
|
87
|
-
node.each do |obj|
|
87
|
+
node.each do |obj|
|
88
88
|
collection.push relation[:class_name].new(obj)
|
89
89
|
end
|
90
90
|
end
|
91
|
-
|
91
|
+
|
92
92
|
send "#{relation[:name]}=", collection
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
self.class._has_one_relations.each do |relation|
|
96
96
|
if node = json[relation[:key]]
|
97
97
|
send "#{relation[:name]}=", relation[:class_name].new(node)
|
@@ -7,15 +7,15 @@
|
|
7
7
|
#
|
8
8
|
# Example:
|
9
9
|
#
|
10
|
-
# { "link": { "$text": "
|
11
|
-
#
|
12
|
-
# shallow_attribute "link"
|
10
|
+
# { "link": { "$text": "http://npr.org" } }
|
13
11
|
#
|
12
|
+
# shallow_attribute "link"
|
13
|
+
# @story.link # => "http://npr.org"
|
14
14
|
module NPR
|
15
15
|
module Concern
|
16
16
|
module ShallowAttributes
|
17
17
|
include AttrTypecast
|
18
|
-
|
18
|
+
|
19
19
|
def self.included(base)
|
20
20
|
base.extend ClassMethods
|
21
21
|
end
|
@@ -27,23 +27,23 @@ module NPR
|
|
27
27
|
_shallow_attributes.push *attrs
|
28
28
|
attr_accessor *attrs
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
#-----------------
|
32
|
-
|
32
|
+
|
33
33
|
def _shallow_attributes
|
34
34
|
@shallow_attributes ||= []
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
#-----------------
|
38
|
-
|
38
|
+
|
39
39
|
private
|
40
40
|
attr_writer :_shallow_attributes
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
#-----------------
|
44
|
-
|
44
|
+
|
45
45
|
private
|
46
|
-
|
46
|
+
|
47
47
|
#-----------------
|
48
48
|
# Extract the defined shallow_attributes from the JSON
|
49
49
|
# and set the corresponding attribute.
|
@@ -68,4 +68,4 @@ module NPR
|
|
68
68
|
end
|
69
69
|
end # ShallowAttribute
|
70
70
|
end # Concern
|
71
|
-
end # NPR
|
71
|
+
end # NPR
|
data/lib/npr/configuration.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
##
|
2
2
|
# NPR::Configuration
|
3
3
|
#
|
4
|
-
module NPR
|
4
|
+
module NPR
|
5
5
|
#-------------------
|
6
6
|
# Pass a block to configure NPR client globally.
|
7
7
|
#
|
@@ -24,11 +24,11 @@ module NPR
|
|
24
24
|
end
|
25
25
|
|
26
26
|
#-------------------
|
27
|
-
|
27
|
+
|
28
28
|
def self.config
|
29
29
|
@config ||= NPR::Configuration.new
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
#-------------------
|
33
33
|
# NPR::Configuration
|
34
34
|
#
|
@@ -38,52 +38,52 @@ module NPR
|
|
38
38
|
#
|
39
39
|
class Configuration
|
40
40
|
API_ROOT = "http://api.npr.org"
|
41
|
-
|
41
|
+
|
42
42
|
API_QUERY_PATH = "/query"
|
43
43
|
API_LIST_PATH = "/list"
|
44
|
-
|
44
|
+
|
45
45
|
# List all the parameters that the NPR API
|
46
|
-
# can accept. Some are left out that don't
|
46
|
+
# can accept. Some are left out that don't
|
47
47
|
# make sense to globally configure (such as
|
48
48
|
# +:id+, +:startNum+, and +:callback+)
|
49
49
|
API_OPTIONS = [
|
50
50
|
# List options
|
51
|
-
:storyCountAll,
|
52
|
-
:storyCountMonth,
|
51
|
+
:storyCountAll,
|
52
|
+
:storyCountMonth,
|
53
53
|
:storyCountToday,
|
54
|
-
:childrenOf,
|
54
|
+
:childrenOf,
|
55
55
|
:hideChildren,
|
56
|
-
|
56
|
+
|
57
57
|
# Story options
|
58
|
-
:apiKey,
|
59
|
-
:orgId,
|
60
|
-
:meta,
|
61
|
-
:output,
|
58
|
+
:apiKey,
|
59
|
+
:orgId,
|
60
|
+
:meta,
|
61
|
+
:output,
|
62
62
|
:fields,
|
63
|
-
:sort,
|
64
|
-
:numResults,
|
65
|
-
:action,
|
63
|
+
:sort,
|
64
|
+
:numResults,
|
65
|
+
:action,
|
66
66
|
:requiredAssets,
|
67
|
-
:date,
|
68
|
-
:startDate,
|
69
|
-
:endDate,
|
67
|
+
:date,
|
68
|
+
:startDate,
|
69
|
+
:endDate,
|
70
70
|
:dateType,
|
71
|
-
:searchTerm,
|
72
|
-
:searchType,
|
71
|
+
:searchTerm,
|
72
|
+
:searchType,
|
73
73
|
:title
|
74
74
|
]
|
75
|
-
|
75
|
+
|
76
76
|
attr_accessor *API_OPTIONS
|
77
77
|
|
78
78
|
#-------------------
|
79
|
-
# You can pass in a hash of options to
|
79
|
+
# You can pass in a hash of options to
|
80
80
|
# Configuration.new
|
81
81
|
def initialize(options={})
|
82
82
|
options.each do |key, value|
|
83
83
|
send "#{key}=", value
|
84
84
|
end
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
#-------------------
|
88
88
|
# Convenience method
|
89
89
|
def merge(hash)
|
@@ -96,12 +96,12 @@ module NPR
|
|
96
96
|
# Why don't we inherit from OrderedOptions?
|
97
97
|
#
|
98
98
|
# Since the API options are out of our control
|
99
|
-
# (without having to map every API option to
|
99
|
+
# (without having to map every API option to
|
100
100
|
# internal methods), it is possible that one
|
101
101
|
# of the config options will conflict with
|
102
102
|
# something in Ruby. For example, the "sort"
|
103
103
|
# option that the NPR API allows would mean
|
104
|
-
# we'd have to overwrite Ruby's Hash#sort
|
104
|
+
# we'd have to overwrite Ruby's Hash#sort
|
105
105
|
# method.
|
106
106
|
#
|
107
107
|
# We *could* just map out config options to
|