npr 0.1.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -4,10 +4,5 @@ rvm:
4
4
  - 1.9.2
5
5
  - ruby-head
6
6
 
7
- matrix:
8
- include:
9
- - rvm: 1.8.7
10
- gemfile: gemfiles/Gemfile.rb-1.8.7
11
-
12
7
  notifications:
13
8
  email: false
data/CHANGELOG.md CHANGED
@@ -1,7 +1,20 @@
1
- ### Version 0.2.0 (unreleased)
2
- * Deeper byline implementation
3
- * Add `primary?` and `standard?` methods to Image to check type
4
- * Add `Story#link_for` for finding link of a certain type
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
  [![Build Status](https://travis-ci.org/bricker/npr.png)](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.8.7, 1.9.2, 1.9.3, ruby-head
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
 
@@ -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
- if @apiKey.nil?
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 NPR::Configuration::API_QUERY_PATH
63
- request.params = @params.merge(params)
61
+ request.url path
64
62
  request.headers['Content-Type'] = "application/json"
65
- request.params['output'] = "json"
66
- request.params['apiKey'] = @apiKey
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
- NPR::API::Response.new(response)
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 NPR::Configuration::API_ROOT do |conn|
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 list = response.list
72
+ if response.list
77
73
  stories = Array.wrap(response.list.stories)
78
74
  end
79
75
 
@@ -19,7 +19,7 @@ module NPR
19
19
 
20
20
  @_response = response
21
21
  @raw = response.body
22
-
22
+
23
23
  @version = @raw["version"]
24
24
 
25
25
  if list = @raw["list"]
@@ -17,4 +17,3 @@ module NPR
17
17
  end # Crop
18
18
  end # Entity
19
19
  end # NPR
20
-
@@ -16,5 +16,3 @@ module NPR
16
16
  end # Enlargement
17
17
  end # Entity
18
18
  end # NPR
19
-
20
-
@@ -16,4 +16,4 @@ module NPR
16
16
  end
17
17
  end # Formats
18
18
  end # Entity
19
- end # NPR
19
+ end # NPR
@@ -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
@@ -14,7 +14,7 @@ module NPR
14
14
  #--------------------
15
15
 
16
16
  def to_s
17
- @content
17
+ @content.to_s
18
18
  end
19
19
  end # IntroText
20
20
  end # Entity
@@ -16,7 +16,7 @@ module NPR
16
16
  #---------------------
17
17
 
18
18
  def to_s
19
- @content
19
+ @content.to_s
20
20
  end
21
21
  end # Link
22
22
  end # Entity
@@ -16,7 +16,7 @@ module NPR
16
16
  #-----------------
17
17
 
18
18
  def to_s
19
- @content
19
+ @content.to_s
20
20
  end
21
21
  end # MP3
22
22
  end # Entity
@@ -18,7 +18,7 @@ module NPR
18
18
  #-------------------
19
19
 
20
20
  def to_s
21
- @content
21
+ @content.to_s
22
22
  end
23
23
  end # Name
24
24
  end # Entity
@@ -20,7 +20,7 @@ module NPR
20
20
  #---------------------
21
21
 
22
22
  def to_s
23
- @content
23
+ @content.to_s
24
24
  end
25
25
  end # Paragraph
26
26
  end # Entity
@@ -17,7 +17,7 @@ module NPR
17
17
  #---------------------
18
18
 
19
19
  def to_s
20
- @content
20
+ @content.to_s
21
21
  end
22
22
  end # Program
23
23
  end # Entity
@@ -16,7 +16,7 @@ module NPR
16
16
  #----------------
17
17
 
18
18
  def to_s
19
- @content
19
+ @content.to_s
20
20
  end
21
21
  end # Provider
22
22
  end # Entity
@@ -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. Looks at the "type" attribute on
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") #=> NPR::Entity::Link
160
+ # story.link_for("html") #=> http://npr.org/...
157
161
  # story.link_for("nothing") #=> nil
158
162
  #
159
- # Returns an NPR::Entity::Link or nil
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
  #-------------------------
@@ -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
@@ -14,7 +14,7 @@ module NPR
14
14
  #--------------------
15
15
 
16
16
  def to_s
17
- @content
17
+ @content.to_s
18
18
  end
19
19
  end # Title
20
20
  end # Entity
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
@@ -1,3 +1,3 @@
1
1
  module NPR
2
- VERSION = "0.1.2"
2
+ VERSION = "1.1.0"
3
3
  end
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 |gem|
5
- gem.authors = ["Bryan Ricker"]
6
- gem.email = ["bricker@scpr.org"]
7
- gem.description = %q{NPR (npr.org) is a news organization. This gem helps you pull NPR content with a nice Ruby DSL.}
8
- gem.summary = %q{A Ruby client for the NPR API}
9
- gem.homepage = "http://github.com/bricker/npr"
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
- gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
- gem.files = `git ls-files`.split("\n")
13
- gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
- gem.name = "npr"
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
- gem.licenses = ['MIT']
18
+ s.licenses = ['MIT']
19
19
 
20
- gem.add_dependency 'faraday', ['>= 0.8.0']
21
- gem.add_dependency 'faraday_middleware', ['>= 0.9.0']
20
+ s.add_dependency 'faraday', '>= 0.8.0'
21
+ s.add_dependency 'faraday_middleware', '>= 0.9.0'
22
22
 
23
- gem.add_development_dependency 'bundler', ['>= 1.0.0']
24
- gem.add_development_dependency 'rake', ['>= 0']
25
- gem.add_development_dependency 'rspec', ['>= 0']
26
- gem.add_development_dependency 'fakeweb', ['>= 0']
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