npr 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
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
5
+
6
+ ### Version 0.1.1 (2012-12-16)
7
+ * First stable release.
@@ -61,6 +61,7 @@ module NPR
61
61
  response = connection.get do |request|
62
62
  request.url NPR::Configuration::API_QUERY_PATH
63
63
  request.params = @params.merge(params)
64
+ request.headers['Content-Type'] = "application/json"
64
65
  request.params['output'] = "json"
65
66
  request.params['apiKey'] = @apiKey
66
67
  end
@@ -77,7 +78,7 @@ module NPR
77
78
  def connection
78
79
  @connection ||= begin
79
80
  Faraday.new NPR::Configuration::API_ROOT do |conn|
80
- conn.response :json, :content_type => /\bjson$/
81
+ conn.response :json
81
82
  conn.adapter Faraday.default_adapter
82
83
  end
83
84
  end
@@ -16,14 +16,17 @@ module NPR
16
16
 
17
17
  def initialize(response)
18
18
  create_relations(response)
19
- @raw = response
20
- @version = response.body["version"]
21
19
 
22
- if response.body["list"]
23
- @list = NPR::Entity::List.new(response.body["list"])
20
+ @_response = response
21
+ @raw = response.body
22
+
23
+ @version = @raw["version"]
24
+
25
+ if list = @raw["list"]
26
+ @list = NPR::Entity::List.new(list)
24
27
  end
25
28
 
26
- Array.wrap(response.body["message"]).each do |message|
29
+ Array.wrap(@raw["message"]).each do |message|
27
30
  @messages.push NPR::API::Message.new(message)
28
31
  end
29
32
  end
@@ -5,13 +5,14 @@ module NPR
5
5
  module Entity
6
6
  class Byline < Base
7
7
  attr_accessor :id
8
- shallow_attribute "name"
8
+ has_many "links", :key => "link", :class_name => NPR::Entity::Link
9
+ has_one "name", :class_name => NPR::Entity::Name
9
10
 
10
11
  #-----------------
11
12
 
12
13
  def initialize(json)
13
14
  @id = json["id"].to_i
14
- extract_shallow_attributes(json)
15
+ create_relations(json)
15
16
  end
16
17
  end # Byline
17
18
  end # Entity
@@ -28,6 +28,18 @@ module NPR
28
28
  extract_shallow_attributes(json)
29
29
  create_relations(json)
30
30
  end
31
+
32
+ #--------------------
33
+
34
+ def primary?
35
+ @type == "primary"
36
+ end
37
+
38
+ #--------------------
39
+
40
+ def standard?
41
+ @type == "standard"
42
+ end
31
43
  end # Image
32
44
  end # Entity
33
45
  end # NPR
@@ -0,0 +1,25 @@
1
+ ##
2
+ # NPR::Entity::Namew
3
+ #
4
+ # Byline name
5
+ #
6
+ module NPR
7
+ module Entity
8
+ class Name < Base
9
+ attr_accessor :personId, :content
10
+
11
+ #-------------------
12
+
13
+ def initialize(json)
14
+ @personId = json["personId"].to_i
15
+ @content = json["$text"]
16
+ end
17
+
18
+ #-------------------
19
+
20
+ def to_s
21
+ @content
22
+ end
23
+ end # Name
24
+ end # Entity
25
+ end # NPR
@@ -143,10 +143,24 @@ module NPR
143
143
  # are found, then return the first image of any type.
144
144
  def primary_image
145
145
  @primary_image ||= begin
146
- primary = self.images.find { |i| i["type"] == "primary"}
146
+ primary = self.images.find(&:primary?)
147
147
  primary || self.images.first
148
148
  end
149
149
  end
150
+
151
+ #-------------------------
152
+ # Find links of the passed in type.
153
+ #
154
+ # Example:
155
+ #
156
+ # story.link_for("html") #=> NPR::Entity::Link
157
+ # story.link_for("nothing") #=> nil
158
+ #
159
+ # Returns an NPR::Entity::Link or nil
160
+ #
161
+ def link_for(type)
162
+ self.links.find { |link| link.type == type }
163
+ end
150
164
 
151
165
  #-------------------------
152
166
 
data/lib/npr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module NPR
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/npr.rb CHANGED
@@ -58,6 +58,7 @@ require "npr/entity/permissions"
58
58
  require "npr/entity/audio"
59
59
  require "npr/entity/book"
60
60
  require "npr/entity/book_edition"
61
+ require "npr/entity/name"
61
62
  require "npr/entity/byline"
62
63
  require "npr/entity/program"
63
64
  require "npr/entity/show"
@@ -4,10 +4,21 @@ describe NPR::Entity::Byline do
4
4
  json_fixture do
5
5
  <<-JSON
6
6
  {
7
- "id": "166481173",
7
+ "id": "166885535",
8
8
  "name": {
9
- "$text": "Annalee Newitz"
10
- }
9
+ "personId": "134991966",
10
+ "$text": "Julie Bierach"
11
+ },
12
+ "link": [
13
+ {
14
+ "type": "html",
15
+ "$text": "http://news.stlpublicradio.org/people/julie-bierach"
16
+ },
17
+ {
18
+ "type": "api",
19
+ "$text": "http://api.npr.org/query?id=134991966&apiKey=MDA1OTI3MjQ5MDEyODUwMTE2MzM1YzNmZA004"
20
+ }
21
+ ]
11
22
  }
12
23
  JSON
13
24
  end
@@ -17,7 +28,13 @@ describe NPR::Entity::Byline do
17
28
  end
18
29
 
19
30
  it "sets up attributes" do
20
- @byline.id.should eq 166481173
21
- @byline.name.should match /Annalee/
31
+ @byline.id.should be_a Fixnum
32
+ @byline.id.should_not eq 0
33
+ end
34
+
35
+ it "creates relations" do
36
+ @byline.links.size.should eq 2
37
+ @byline.links.first.should be_a NPR::Entity::Link
38
+ @byline.name.should be_a NPR::Entity::Name
22
39
  end
23
40
  end
@@ -79,4 +79,9 @@ describe NPR::Entity::Image do
79
79
  @image.producer.should eq ""
80
80
  @image.copyright.should eq ""
81
81
  end
82
+
83
+ it "responds to type methods" do
84
+ @image.primary?.should eq false
85
+ @image.standard?.should eq true
86
+ end
82
87
  end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe NPR::Entity::Name do
4
+ json_fixture do
5
+ <<-JSON
6
+ {
7
+ "personId": "134991966",
8
+ "$text": "Julie Bierach"
9
+ }
10
+ JSON
11
+ end
12
+
13
+ before :each do
14
+ @name = NPR::Entity::Name.new(@fixture)
15
+ end
16
+
17
+ it "sets attributes" do
18
+ @name.personId.should eq 134991966
19
+ @name.content.should match /Julie/
20
+ end
21
+
22
+ it "uses @content for to_s" do
23
+ @name.to_s.should eq @name.content
24
+ end
25
+ end
@@ -150,5 +150,46 @@ describe NPR::Entity::Story do
150
150
  query = NPR::Entity::Story.offset(args)
151
151
  query.builder[:offset].should eq args
152
152
  end
153
- end
153
+ end
154
+
155
+ #--------------------
156
+
157
+ describe "#primary_image" do
158
+ before :each do
159
+ @story = mock_response "json/02_story_multiple_images.json" do
160
+ NPR::Story.find(999)
161
+ end
162
+ end
163
+
164
+ it "finds the first image with type 'primary'" do
165
+ @story.primary_image.type.should eq "primary"
166
+ end
167
+
168
+ it "falls back to just the first image if no primary image is available" do
169
+ # Remove the type "primary" image from the json response
170
+ @story.images.delete_if { |i| i.type == "primary" }
171
+ @story.primary_image.type.should eq "standard"
172
+ end
173
+ end
174
+
175
+ #--------------------
176
+
177
+ describe "#link_for" do
178
+ before :each do
179
+ @story = mock_response "json/02_story_multiple_images.json" do
180
+ NPR::Story.find(999)
181
+ end
182
+ end
183
+
184
+ it "finds the link for the passed-in type if it exists" do
185
+ @story.link_for("html").should be_a NPR::Entity::Link
186
+ end
187
+
188
+ it "is nil if the type isn't present" do
189
+ @story.link_for("nothing").should eq nil
190
+ end
191
+ end
192
+
193
+ #--------------------
194
+
154
195
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: npr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
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-12-17 00:00:00.000000000 Z
12
+ date: 2012-12-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -117,6 +117,7 @@ extra_rdoc_files: []
117
117
  files:
118
118
  - .gitignore
119
119
  - .travis.yml
120
+ - CHANGELOG.md
120
121
  - Gemfile
121
122
  - MIT-LICENSE
122
123
  - README.md
@@ -154,6 +155,7 @@ files:
154
155
  - lib/npr/entity/member_byline.rb
155
156
  - lib/npr/entity/member_promo_art.rb
156
157
  - lib/npr/entity/mp3.rb
158
+ - lib/npr/entity/name.rb
157
159
  - lib/npr/entity/organization.rb
158
160
  - lib/npr/entity/paragraph.rb
159
161
  - lib/npr/entity/permissions.rb
@@ -252,6 +254,7 @@ files:
252
254
  - spec/unit/entity/member_promo_art_spec.rb
253
255
  - spec/unit/entity/member_spec.rb
254
256
  - spec/unit/entity/mp3_spec.rb
257
+ - spec/unit/entity/name_spec.rb
255
258
  - spec/unit/entity/organization_spec.rb
256
259
  - spec/unit/entity/paragraph_spec.rb
257
260
  - spec/unit/entity/permissions_spec.rb
@@ -372,6 +375,7 @@ test_files:
372
375
  - spec/unit/entity/member_promo_art_spec.rb
373
376
  - spec/unit/entity/member_spec.rb
374
377
  - spec/unit/entity/mp3_spec.rb
378
+ - spec/unit/entity/name_spec.rb
375
379
  - spec/unit/entity/organization_spec.rb
376
380
  - spec/unit/entity/paragraph_spec.rb
377
381
  - spec/unit/entity/permissions_spec.rb