npr 0.1.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +0 -5
- data/CHANGELOG.md +17 -4
- data/README.md +1 -16
- data/lib/npr/api/client.rb +14 -10
- data/lib/npr/api/query_builder.rb +1 -5
- data/lib/npr/api/response.rb +1 -1
- data/lib/npr/entity/crop.rb +0 -1
- data/lib/npr/entity/enlargement.rb +0 -2
- data/lib/npr/entity/formats.rb +1 -1
- data/lib/npr/entity/image.rb +30 -8
- data/lib/npr/entity/intro_text.rb +1 -1
- data/lib/npr/entity/link.rb +1 -1
- data/lib/npr/entity/mp3.rb +1 -1
- data/lib/npr/entity/name.rb +1 -1
- data/lib/npr/entity/paragraph.rb +1 -1
- data/lib/npr/entity/program.rb +1 -1
- data/lib/npr/entity/provider.rb +1 -1
- data/lib/npr/entity/story.rb +17 -10
- data/lib/npr/entity/text.rb +19 -0
- data/lib/npr/entity/title.rb +1 -1
- data/lib/npr/errors.rb +6 -6
- data/lib/npr/version.rb +1 -1
- data/npr.gemspec +19 -19
- data/spec/fixtures/json/01_story_full_media.json +520 -1
- data/spec/fixtures/json/02_story_multiple_images.json +561 -1
- data/spec/fixtures/json/03_no_results.json +36 -1
- data/spec/fixtures/json/04_invalid_id.json +3791 -1
- data/spec/fixtures/json/05_no_api_key.json +15 -1
- data/spec/fixtures/json/06_story_multiple_ids.json +1190 -1
- data/spec/support/fake_response.rb +6 -5
- data/spec/unit/api/client_spec.rb +35 -12
- data/spec/unit/entity/image_spec.rb +10 -0
- data/spec/unit/entity/story_spec.rb +1 -1
- data/spec/unit/entity/text_spec.rb +8 -0
- metadata +9 -4
- data/gemfiles/Gemfile.rb-1.8.7 +0 -6
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,20 @@
|
|
1
|
-
### Version
|
2
|
-
*
|
3
|
-
*
|
4
|
-
*
|
1
|
+
### Version 1.1.0 (2013-06-05)
|
2
|
+
* Allow passing `:url` option to Client, and `:path` option to `Client#query`
|
3
|
+
* Allow passing `:apiKey` and `:output` to `Client#query`
|
4
|
+
* Handle an HTTP Error from the API better. If the API response is not a
|
5
|
+
success (as defined by `Faraday::Response#success?`), then an
|
6
|
+
`NPR::APIError` will be raised. An error was being raised before, but
|
7
|
+
it was due to a nil error (when response.body was nil), which could be confusing.
|
8
|
+
* Add an Image#crop method, to find a specific crop by its type.
|
9
|
+
|
10
|
+
|
11
|
+
### Version 1.0.0
|
12
|
+
* Stable release
|
13
|
+
|
14
|
+
|
15
|
+
### Version 0.1.2 (2012-12-30)
|
16
|
+
* Critical bug fix having to do with JSON parsing.
|
17
|
+
|
5
18
|
|
6
19
|
### Version 0.1.1 (2012-12-16)
|
7
20
|
* First stable release.
|
data/README.md
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
[](https://travis-ci.org/bricker/npr)
|
4
4
|
|
5
|
-
**NOTE** This gem is a WIP
|
6
|
-
|
7
5
|
A simple Ruby client for the
|
8
6
|
[NPR API](http://www.npr.org/api/index).
|
9
7
|
|
@@ -13,7 +11,7 @@ A simple Ruby client for the
|
|
13
11
|
This gem is tested with these versions (but may
|
14
12
|
work with others):
|
15
13
|
|
16
|
-
* Ruby 1.
|
14
|
+
* Ruby 1.9.2, 1.9.3, ruby-head
|
17
15
|
* NPR API version 0.94
|
18
16
|
|
19
17
|
|
@@ -22,19 +20,6 @@ work with others):
|
|
22
20
|
* `faraday >= 0.8.0` (HTTP requests)
|
23
21
|
* `faraday_middleware >= 0.9.0` (response processing)
|
24
22
|
|
25
|
-
**NOTE** If you are running **Ruby < 1.9**, you will have to install
|
26
|
-
the [json gem](http://rubygems.org/gems/json):
|
27
|
-
|
28
|
-
In your Gemfile:
|
29
|
-
|
30
|
-
gem 'json', '~> 1.7.5'
|
31
|
-
|
32
|
-
From the command line:
|
33
|
-
|
34
|
-
$ gem install json
|
35
|
-
|
36
|
-
Ruby 1.9+ comes with JSON support built-in!
|
37
|
-
|
38
23
|
|
39
24
|
## Installation
|
40
25
|
|
data/lib/npr/api/client.rb
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
module NPR
|
17
17
|
module API
|
18
18
|
class Client
|
19
|
-
attr_accessor :params
|
19
|
+
attr_accessor :params, :url
|
20
20
|
|
21
21
|
#-----------------
|
22
22
|
# Argument is a hash of params to send to the API.
|
@@ -32,6 +32,7 @@ module NPR
|
|
32
32
|
#
|
33
33
|
def initialize(params={})
|
34
34
|
@params = NPR.config.merge(params)
|
35
|
+
@url = @params.delete(:url) || NPR::Configuration::API_ROOT
|
35
36
|
@apiKey = @params.delete(:apiKey)
|
36
37
|
end
|
37
38
|
|
@@ -54,19 +55,22 @@ module NPR
|
|
54
55
|
# TODO: Support more formats
|
55
56
|
#
|
56
57
|
def query(params={})
|
57
|
-
|
58
|
-
raise NPR::NotConfiguredError, "apiKey must be set to perform a query"
|
59
|
-
end
|
58
|
+
path = params.delete(:path) || NPR::Configuration::API_QUERY_PATH
|
60
59
|
|
61
60
|
response = connection.get do |request|
|
62
|
-
request.url
|
63
|
-
request.params = @params.merge(params)
|
61
|
+
request.url path
|
64
62
|
request.headers['Content-Type'] = "application/json"
|
65
|
-
|
66
|
-
request.params
|
63
|
+
|
64
|
+
request.params = @params.merge(params)
|
65
|
+
request.params['output'] ||= "json" # Only JSON is supported.
|
66
|
+
request.params['apiKey'] ||= @apiKey
|
67
67
|
end
|
68
68
|
|
69
|
-
|
69
|
+
if response.success?
|
70
|
+
NPR::API::Response.new(response)
|
71
|
+
else
|
72
|
+
raise NPR::APIError, "The API call failed. (Status: #{response.status})"
|
73
|
+
end
|
70
74
|
end
|
71
75
|
|
72
76
|
#-----------------
|
@@ -77,7 +81,7 @@ module NPR
|
|
77
81
|
|
78
82
|
def connection
|
79
83
|
@connection ||= begin
|
80
|
-
Faraday.new
|
84
|
+
Faraday.new @url do |conn|
|
81
85
|
conn.response :json
|
82
86
|
conn.adapter Faraday.default_adapter
|
83
87
|
end
|
@@ -44,10 +44,6 @@ module NPR
|
|
44
44
|
#-----------------------
|
45
45
|
# Fire the query and return an Array of stories (or empty [])
|
46
46
|
#
|
47
|
-
# Named +to_a+ to keep in line with ActiveRecord::Relation's
|
48
|
-
# naming convention. It is also aliased as +query+, which
|
49
|
-
# probably makes a little more sense.
|
50
|
-
#
|
51
47
|
# Returns an Array. If the query returned any stories, then
|
52
48
|
# it will be an array of those stories. If no stories were
|
53
49
|
# returned, then it will be an empty array.
|
@@ -73,7 +69,7 @@ module NPR
|
|
73
69
|
response = self.query
|
74
70
|
stories = []
|
75
71
|
|
76
|
-
if
|
72
|
+
if response.list
|
77
73
|
stories = Array.wrap(response.list.stories)
|
78
74
|
end
|
79
75
|
|
data/lib/npr/api/response.rb
CHANGED
data/lib/npr/entity/crop.rb
CHANGED
data/lib/npr/entity/formats.rb
CHANGED
data/lib/npr/entity/image.rb
CHANGED
@@ -9,8 +9,8 @@ module NPR
|
|
9
9
|
has_many "crops", :key => "crop", :class_name => NPR::Entity::Crop
|
10
10
|
has_one "enlargement", :class_name => NPR::Entity::Enlargement
|
11
11
|
has_one "provider", :class_name => NPR::Entity::Provider
|
12
|
-
|
13
|
-
|
12
|
+
|
13
|
+
|
14
14
|
# NOTE that the "link" attribute here is not cast into a Link
|
15
15
|
# object, and the "url" parameter is ignored. Instead, just
|
16
16
|
# calling +image.link+ will return the URL parameter.
|
@@ -28,15 +28,37 @@ module NPR
|
|
28
28
|
extract_shallow_attributes(json)
|
29
29
|
create_relations(json)
|
30
30
|
end
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
|
32
|
+
|
33
|
+
# Find a crop by its type.
|
34
|
+
#
|
35
|
+
# Arguments:
|
36
|
+
#
|
37
|
+
# * (String) type - the type of crop you're looking for
|
38
|
+
#
|
39
|
+
#
|
40
|
+
# Examples:
|
41
|
+
#
|
42
|
+
# image.crop("enlargement") #=> NPR::Entity::Crop
|
43
|
+
# image.crop("missing") #=> nil
|
44
|
+
#
|
45
|
+
# Returns: An NPR::Entity::Crop, or nil if none found.
|
46
|
+
def crop(type)
|
47
|
+
self.crops.find { |c| c.type == type }
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
# Determine if this image is the primary image (as defined by NPR).
|
52
|
+
#
|
53
|
+
# Returns: Boolean
|
34
54
|
def primary?
|
35
55
|
@type == "primary"
|
36
56
|
end
|
37
|
-
|
38
|
-
|
39
|
-
|
57
|
+
|
58
|
+
|
59
|
+
# Determine if this image is the standard image (as defined by NPR).
|
60
|
+
#
|
61
|
+
# Returns: Boolean
|
40
62
|
def standard?
|
41
63
|
@type == "standard"
|
42
64
|
end
|
data/lib/npr/entity/link.rb
CHANGED
data/lib/npr/entity/mp3.rb
CHANGED
data/lib/npr/entity/name.rb
CHANGED
data/lib/npr/entity/paragraph.rb
CHANGED
data/lib/npr/entity/program.rb
CHANGED
data/lib/npr/entity/provider.rb
CHANGED
data/lib/npr/entity/story.rb
CHANGED
@@ -4,8 +4,6 @@
|
|
4
4
|
module NPR
|
5
5
|
module Entity
|
6
6
|
class Story < Base
|
7
|
-
#-------------------------
|
8
|
-
|
9
7
|
class << self
|
10
8
|
#-------------------------
|
11
9
|
# Find a story based on ID
|
@@ -69,12 +67,15 @@ module NPR
|
|
69
67
|
private
|
70
68
|
|
71
69
|
def query_by_id(id)
|
72
|
-
client = NPR::API::Client.new(
|
73
|
-
:apiKey => NPR.config.apiKey,
|
74
|
-
:output => "json")
|
75
|
-
|
76
70
|
client.query(:id => id)
|
77
71
|
end
|
72
|
+
|
73
|
+
def client
|
74
|
+
@client ||= NPR::API::Client.new(
|
75
|
+
:apiKey => NPR.config.apiKey,
|
76
|
+
:output => "json"
|
77
|
+
)
|
78
|
+
end
|
78
79
|
end
|
79
80
|
|
80
81
|
#-------------------------
|
@@ -138,9 +139,12 @@ module NPR
|
|
138
139
|
end
|
139
140
|
|
140
141
|
#-------------------------
|
141
|
-
# The primary image.
|
142
|
+
# The primary image.
|
143
|
+
#
|
144
|
+
# Looks at the "type" attribute on
|
142
145
|
# an image and finds any with type "primary". If none
|
143
146
|
# are found, then return the first image of any type.
|
147
|
+
#
|
144
148
|
def primary_image
|
145
149
|
@primary_image ||= begin
|
146
150
|
primary = self.images.find(&:primary?)
|
@@ -153,13 +157,16 @@ module NPR
|
|
153
157
|
#
|
154
158
|
# Example:
|
155
159
|
#
|
156
|
-
# story.link_for("html") #=>
|
160
|
+
# story.link_for("html") #=> http://npr.org/...
|
157
161
|
# story.link_for("nothing") #=> nil
|
158
162
|
#
|
159
|
-
# Returns an
|
163
|
+
# Returns an the content of that link if found,
|
164
|
+
# or nil if not found.
|
160
165
|
#
|
161
166
|
def link_for(type)
|
162
|
-
self.links.find { |link| link.type == type }
|
167
|
+
if link = self.links.find { |link| link.type == type }
|
168
|
+
link.to_s
|
169
|
+
end
|
163
170
|
end
|
164
171
|
|
165
172
|
#-------------------------
|
data/lib/npr/entity/text.rb
CHANGED
@@ -17,6 +17,25 @@ module NPR
|
|
17
17
|
def to_s
|
18
18
|
@paragraphs.map(&:to_s).join("\n")
|
19
19
|
end
|
20
|
+
|
21
|
+
#-----------------
|
22
|
+
# Turn this text node into an embeddable
|
23
|
+
# block of HTML.
|
24
|
+
#
|
25
|
+
# This is useful if the fullText attribute
|
26
|
+
# is missing on an NPR Story. It will not
|
27
|
+
# be as robust as a full HTML body from
|
28
|
+
# NPR, but it will work in most cases.
|
29
|
+
#
|
30
|
+
def to_html
|
31
|
+
result = ""
|
32
|
+
|
33
|
+
@paragraphs.each do |paragraph|
|
34
|
+
result << "<p>" + paragraph.to_s + "</p>\n"
|
35
|
+
end
|
36
|
+
|
37
|
+
result
|
38
|
+
end
|
20
39
|
end # Text
|
21
40
|
end # Entity
|
22
41
|
end # NPR
|
data/lib/npr/entity/title.rb
CHANGED
data/lib/npr/errors.rb
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
module NPR
|
5
5
|
class ClientError < StandardError
|
6
6
|
end
|
7
|
-
|
8
|
-
#-------------------
|
9
|
-
|
7
|
+
|
10
8
|
class ServerError < StandardError
|
11
9
|
end
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
|
11
|
+
|
12
|
+
class APIError < ServerError
|
13
|
+
end
|
14
|
+
|
15
15
|
class NotConfiguredError < ClientError
|
16
16
|
end
|
17
17
|
end # NPR
|
data/lib/npr/version.rb
CHANGED
data/npr.gemspec
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require File.expand_path('../lib/npr/version', __FILE__)
|
3
3
|
|
4
|
-
Gem::Specification.new do |
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "npr"
|
6
|
+
s.version = NPR::VERSION
|
7
|
+
s.authors = ["Bryan Ricker"]
|
8
|
+
s.email = ["bricker@scpr.org"]
|
9
|
+
s.description = %q{NPR (npr.org) is a news organization. This gem helps you pull NPR content with a nice Ruby DSL.}
|
10
|
+
s.summary = %q{A Ruby client for the NPR API}
|
11
|
+
s.homepage = "http://github.com/bricker/npr"
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
gem.require_paths = ["lib"]
|
16
|
-
gem.version = NPR::VERSION
|
13
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.require_paths = ["lib"]
|
17
17
|
|
18
|
-
|
18
|
+
s.licenses = ['MIT']
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
s.add_dependency 'faraday', '>= 0.8.0'
|
21
|
+
s.add_dependency 'faraday_middleware', '>= 0.9.0'
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
s.add_development_dependency 'bundler', '>= 1.0.0'
|
24
|
+
s.add_development_dependency 'rake'
|
25
|
+
s.add_development_dependency 'rspec'
|
26
|
+
s.add_development_dependency 'fakeweb'
|
27
27
|
end
|