npr 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +13 -0
- data/{LICENSE → MIT-LICENSE} +1 -1
- data/README.md +115 -3
- data/Rakefile +6 -0
- data/gemfiles/Gemfile.rb-1.8.7 +6 -0
- data/lib/npr/api/client.rb +87 -0
- data/lib/npr/api/message.rb +38 -0
- data/lib/npr/api/query_builder.rb +222 -0
- data/lib/npr/api/response.rb +32 -0
- data/lib/npr/api.rb +7 -0
- data/lib/npr/concern/attr_typecast.rb +59 -0
- data/lib/npr/concern/relation.rb +103 -0
- data/lib/npr/concern/shallow_attributes.rb +71 -0
- data/lib/npr/concern.rb +9 -0
- data/lib/npr/configuration.rb +120 -0
- data/lib/npr/core_ext/array/wrap.rb +15 -0
- data/lib/npr/entity/audio.rb +28 -0
- data/lib/npr/entity/author.rb +22 -0
- data/lib/npr/entity/base.rb +19 -0
- data/lib/npr/entity/book.rb +21 -0
- data/lib/npr/entity/book_edition.rb +26 -0
- data/lib/npr/entity/byline.rb +18 -0
- data/lib/npr/entity/collection.rb +11 -0
- data/lib/npr/entity/crop.rb +20 -0
- data/lib/npr/entity/enlargement.rb +20 -0
- data/lib/npr/entity/formats.rb +19 -0
- data/lib/npr/entity/image.rb +33 -0
- data/lib/npr/entity/intro_text.rb +21 -0
- data/lib/npr/entity/link.rb +23 -0
- data/lib/npr/entity/list.rb +28 -0
- data/lib/npr/entity/list_text.rb +19 -0
- data/lib/npr/entity/member.rb +23 -0
- data/lib/npr/entity/member_byline.rb +17 -0
- data/lib/npr/entity/member_promo_art.rb +17 -0
- data/lib/npr/entity/mp3.rb +23 -0
- data/lib/npr/entity/organization.rb +18 -0
- data/lib/npr/entity/paragraph.rb +27 -0
- data/lib/npr/entity/permissions.rb +18 -0
- data/lib/npr/entity/program.rb +24 -0
- data/lib/npr/entity/promo_art.rb +17 -0
- data/lib/npr/entity/promo_art_book_edition.rb +18 -0
- data/lib/npr/entity/provider.rb +23 -0
- data/lib/npr/entity/pull_quote.rb +16 -0
- data/lib/npr/entity/related_link.rb +21 -0
- data/lib/npr/entity/show.rb +19 -0
- data/lib/npr/entity/story.rb +179 -0
- data/lib/npr/entity/text.rb +22 -0
- data/lib/npr/entity/title.rb +21 -0
- data/lib/npr/entity/transcript.rb +16 -0
- data/lib/npr/entity.rb +7 -0
- data/lib/npr/errors.rb +17 -0
- data/lib/npr/version.rb +1 -1
- data/lib/npr.rb +64 -3
- data/npr.gemspec +12 -2
- data/spec/fixtures/README.md +30 -0
- data/spec/fixtures/atom/01_story_full_media.atom +36 -0
- data/spec/fixtures/atom/02_story_multiple_images.atom +32 -0
- data/spec/fixtures/atom/03_no_results.atom +10 -0
- data/spec/fixtures/atom/04_invalid_id.atom +344 -0
- data/spec/fixtures/atom/05_no_api_key.atom +9 -0
- data/spec/fixtures/atom/06_story_multiple_ids.atom +59 -0
- data/spec/fixtures/fetch_formats.rb +65 -0
- data/spec/fixtures/html/01_story_full_media.html +54 -0
- data/spec/fixtures/html/02_story_multiple_images.html +55 -0
- data/spec/fixtures/html/03_no_results.html +38 -0
- data/spec/fixtures/html/04_invalid_id.html +82 -0
- data/spec/fixtures/html/05_no_api_key.html +8 -0
- data/spec/fixtures/html/06_story_multiple_ids.html +69 -0
- data/spec/fixtures/js/01_story_full_media.js +1 -0
- data/spec/fixtures/js/02_story_multiple_images.js +1 -0
- data/spec/fixtures/js/03_no_results.js +1 -0
- data/spec/fixtures/js/04_invalid_id.js +1 -0
- data/spec/fixtures/js/05_no_api_key.js +8 -0
- data/spec/fixtures/js/06_story_multiple_ids.js +1 -0
- data/spec/fixtures/json/01_story_full_media.json +1 -0
- data/spec/fixtures/json/02_story_multiple_images.json +1 -0
- data/spec/fixtures/json/03_no_results.json +1 -0
- data/spec/fixtures/json/04_invalid_id.json +1 -0
- data/spec/fixtures/json/05_no_api_key.json +1 -0
- data/spec/fixtures/json/06_story_multiple_ids.json +1 -0
- data/spec/fixtures/json/list.json +1 -0
- data/spec/fixtures/mediarss/01_story_full_media.rss +31 -0
- data/spec/fixtures/mediarss/02_story_multiple_images.rss +40 -0
- data/spec/fixtures/mediarss/03_no_results.rss +17 -0
- data/spec/fixtures/mediarss/04_invalid_id.rss +279 -0
- data/spec/fixtures/mediarss/05_no_api_key.rss +31 -0
- data/spec/fixtures/mediarss/06_story_multiple_ids.rss +65 -0
- data/spec/fixtures/nprml/01_story_full_media.xml +271 -0
- data/spec/fixtures/nprml/02_story_multiple_images.xml +165 -0
- data/spec/fixtures/nprml/03_no_results.xml +14 -0
- data/spec/fixtures/nprml/04_invalid_id.xml +1780 -0
- data/spec/fixtures/nprml/05_no_api_key.xml +9 -0
- data/spec/fixtures/nprml/06_story_multiple_ids.xml +435 -0
- data/spec/fixtures/nprml/list.xml +440 -0
- data/spec/fixtures/podcast/01_story_full_media.rss +30 -0
- data/spec/fixtures/podcast/02_story_multiple_images.rss +32 -0
- data/spec/fixtures/podcast/03_no_results.rss +19 -0
- data/spec/fixtures/podcast/04_invalid_id.rss +186 -0
- data/spec/fixtures/podcast/05_no_api_key.rss +31 -0
- data/spec/fixtures/podcast/06_story_multiple_ids.rss +43 -0
- data/spec/fixtures/rss/01_story_full_media.rss +25 -0
- data/spec/fixtures/rss/02_story_multiple_images.rss +25 -0
- data/spec/fixtures/rss/03_no_results.rss +17 -0
- data/spec/fixtures/rss/04_invalid_id.rss +137 -0
- data/spec/fixtures/rss/05_no_api_key.rss +31 -0
- data/spec/fixtures/rss/06_story_multiple_ids.rss +33 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/support/config_helper.rb +64 -0
- data/spec/support/fake_response.rb +54 -0
- data/spec/support/fixture_helper.rb +23 -0
- data/spec/unit/api/client_spec.rb +42 -0
- data/spec/unit/api/message_spec.rb +68 -0
- data/spec/unit/api/query_builder_spec.rb +195 -0
- data/spec/unit/api/response_spec.rb +45 -0
- data/spec/unit/configuration_spec.rb +63 -0
- data/spec/unit/entity/audio_spec.rb +75 -0
- data/spec/unit/entity/author_spec.rb +30 -0
- data/spec/unit/entity/base_spec.rb +87 -0
- data/spec/unit/entity/book_edition_spec.rb +57 -0
- data/spec/unit/entity/book_spec.rb +31 -0
- data/spec/unit/entity/byline_spec.rb +23 -0
- data/spec/unit/entity/collection_spec.rb +39 -0
- data/spec/unit/entity/crop_spec.rb +25 -0
- data/spec/unit/entity/englargement_spec.rb +21 -0
- data/spec/unit/entity/formats_spec.rb +41 -0
- data/spec/unit/entity/image_spec.rb +82 -0
- data/spec/unit/entity/intro_text_spec.rb +25 -0
- data/spec/unit/entity/link_spec.rb +25 -0
- data/spec/unit/entity/list_spec.rb +22 -0
- data/spec/unit/entity/list_text_spec.rb +31 -0
- data/spec/unit/entity/member_byline_spec.rb +21 -0
- data/spec/unit/entity/member_promo_art_spec.rb +21 -0
- data/spec/unit/entity/member_spec.rb +59 -0
- data/spec/unit/entity/mp3_spec.rb +25 -0
- data/spec/unit/entity/organization_spec.rb +29 -0
- data/spec/unit/entity/paragraph_spec.rb +25 -0
- data/spec/unit/entity/permissions_spec.rb +56 -0
- data/spec/unit/entity/program_spec.rb +27 -0
- data/spec/unit/entity/promo_art_book_edition_spec.rb +21 -0
- data/spec/unit/entity/promo_art_spec.rb +42 -0
- data/spec/unit/entity/pull_quote_spec.rb +29 -0
- data/spec/unit/entity/related_link_spec.rb +55 -0
- data/spec/unit/entity/show_spec.rb +41 -0
- data/spec/unit/entity/story_spec.rb +154 -0
- data/spec/unit/entity/text_spec.rb +54 -0
- data/spec/unit/entity/title_spec.rb +25 -0
- data/spec/unit/entity/transcript_spec.rb +22 -0
- metadata +340 -8
@@ -0,0 +1,17 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::MemberByline
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class MemberByline < Base
|
7
|
+
attr_accessor :refId, :num
|
8
|
+
|
9
|
+
#-------------------
|
10
|
+
|
11
|
+
def initialize(json)
|
12
|
+
@refId = json["refId"].to_i
|
13
|
+
@num = json["num"].to_i
|
14
|
+
end
|
15
|
+
end # MemberByline
|
16
|
+
end # Entity
|
17
|
+
end # NPR
|
@@ -0,0 +1,17 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::MemberPromoArt
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class MemberPromoArt < Base
|
7
|
+
attr_accessor :refId, :num
|
8
|
+
|
9
|
+
#-------------------
|
10
|
+
|
11
|
+
def initialize(json)
|
12
|
+
@refId = json["refId"].to_i
|
13
|
+
@num = json["num"].to_i
|
14
|
+
end
|
15
|
+
end # MemberPromoArt
|
16
|
+
end # Entity
|
17
|
+
end # NPR
|
@@ -0,0 +1,23 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::MP3
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class MP3 < Base
|
7
|
+
attr_accessor :type, :content
|
8
|
+
|
9
|
+
#-----------------
|
10
|
+
|
11
|
+
def initialize(json)
|
12
|
+
@type = json["type"]
|
13
|
+
@content = json["$text"]
|
14
|
+
end
|
15
|
+
|
16
|
+
#-----------------
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
@content
|
20
|
+
end
|
21
|
+
end # MP3
|
22
|
+
end # Entity
|
23
|
+
end # NPR
|
@@ -0,0 +1,18 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::Organization
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class Organization < Base
|
7
|
+
attr_accessor :orgId, :orgAbbr
|
8
|
+
shallow_attribute "name", "website"
|
9
|
+
|
10
|
+
def initialize(json)
|
11
|
+
extract_shallow_attributes(json)
|
12
|
+
|
13
|
+
@orgId = json["orgId"].to_i
|
14
|
+
@orgAbbr = json["orgAbbr"]
|
15
|
+
end
|
16
|
+
end # Organization
|
17
|
+
end # Entity
|
18
|
+
end # NPR
|
@@ -0,0 +1,27 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::Paragraph
|
3
|
+
#
|
4
|
+
# NPR splits every paragraph into separate
|
5
|
+
# database entries. This object will represent
|
6
|
+
# a single paragraph for a story.
|
7
|
+
#
|
8
|
+
module NPR
|
9
|
+
module Entity
|
10
|
+
class Paragraph < Base
|
11
|
+
attr_accessor :num, :content
|
12
|
+
|
13
|
+
#---------------------
|
14
|
+
|
15
|
+
def initialize(json)
|
16
|
+
@content = json["$text"]
|
17
|
+
@num = json["num"].to_i
|
18
|
+
end
|
19
|
+
|
20
|
+
#---------------------
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
@content
|
24
|
+
end
|
25
|
+
end # Paragraph
|
26
|
+
end # Entity
|
27
|
+
end # NPR
|
@@ -0,0 +1,18 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::Permissions
|
3
|
+
#
|
4
|
+
# Audio permissions
|
5
|
+
#
|
6
|
+
module NPR
|
7
|
+
module Entity
|
8
|
+
class Permissions < Base
|
9
|
+
attr_accessor :download, :stream, :embed
|
10
|
+
|
11
|
+
def initialize(json)
|
12
|
+
@download = json["download"]["allow"] == "true" if json["download"]
|
13
|
+
@stream = json["stream"]["allow"] == "true" if json["stream"]
|
14
|
+
@embed = json["embed"]["allow"] == "true" if json["embed"]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::Program
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class Program < Base
|
7
|
+
attr_accessor :id, :code, :content
|
8
|
+
|
9
|
+
#---------------------
|
10
|
+
|
11
|
+
def initialize(json)
|
12
|
+
@id = json["id"].to_i
|
13
|
+
@content = json["$text"]
|
14
|
+
@code = json["code"]
|
15
|
+
end
|
16
|
+
|
17
|
+
#---------------------
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
@content
|
21
|
+
end
|
22
|
+
end # Program
|
23
|
+
end # Entity
|
24
|
+
end # NPR
|
@@ -0,0 +1,17 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::PromoArt
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class PromoArt < Image
|
7
|
+
attr_accessor :bookEditionId
|
8
|
+
|
9
|
+
#--------------------
|
10
|
+
|
11
|
+
def initialize(json)
|
12
|
+
@bookEditionId = json["bookEditionId"].to_i
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end # PromoArt
|
16
|
+
end # Entity
|
17
|
+
end # NPR
|
@@ -0,0 +1,18 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::PromoArtBookEdition
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class PromoArtBookEdition < Base
|
7
|
+
attr_accessor :refId, :num
|
8
|
+
|
9
|
+
#-------------------
|
10
|
+
|
11
|
+
def initialize(json)
|
12
|
+
@refId = json["refId"].to_i
|
13
|
+
@num = json["num"].to_i
|
14
|
+
end
|
15
|
+
end # PromoArtBookEdition
|
16
|
+
end # Entity
|
17
|
+
end # NPR
|
18
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::Provider
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class Provider < Base
|
7
|
+
attr_accessor :content, :url
|
8
|
+
|
9
|
+
#----------------
|
10
|
+
|
11
|
+
def initialize(json)
|
12
|
+
@content = json["$text"]
|
13
|
+
@url = json["url"]
|
14
|
+
end
|
15
|
+
|
16
|
+
#----------------
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
@content
|
20
|
+
end
|
21
|
+
end # Provider
|
22
|
+
end # Entity
|
23
|
+
end # NPR
|
@@ -0,0 +1,16 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::PullQuote
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class PullQuote < Base
|
7
|
+
attr_accessor :id
|
8
|
+
shallow_attribute "text", "person", "date"
|
9
|
+
|
10
|
+
def initialize(json)
|
11
|
+
extract_shallow_attributes(json)
|
12
|
+
@id = json["id"].to_i
|
13
|
+
end
|
14
|
+
end # PullQuote
|
15
|
+
end # Entity
|
16
|
+
end # NPR
|
@@ -0,0 +1,21 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::RelatedLink
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class RelatedLink < Base
|
7
|
+
attr_accessor :id, :type
|
8
|
+
|
9
|
+
has_many "links", :key => "link", :class_name => NPR::Entity::Link
|
10
|
+
shallow_attribute "caption"
|
11
|
+
|
12
|
+
def initialize(json)
|
13
|
+
extract_shallow_attributes(json)
|
14
|
+
create_relations(json)
|
15
|
+
|
16
|
+
@id = json["id"].to_i
|
17
|
+
@type = json["type"]
|
18
|
+
end
|
19
|
+
end # RelatedLink
|
20
|
+
end # Entity
|
21
|
+
end # NPR
|
@@ -0,0 +1,19 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::Show
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class Show < Base
|
7
|
+
attr_accessor :program
|
8
|
+
shallow_attribute "showDate", "segNum"
|
9
|
+
|
10
|
+
def initialize(json)
|
11
|
+
extract_shallow_attributes(json)
|
12
|
+
|
13
|
+
if program = json["program"]
|
14
|
+
@program = NPR::Entity::Program.new(program)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end # Show
|
18
|
+
end # Entity
|
19
|
+
end # NPR
|
@@ -0,0 +1,179 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::Story
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class Story < Base
|
7
|
+
#-------------------------
|
8
|
+
|
9
|
+
class << self
|
10
|
+
#-------------------------
|
11
|
+
# Find a story based on ID
|
12
|
+
#
|
13
|
+
# This method is meant to be a quick, easy
|
14
|
+
# way to find a story just by its ID.
|
15
|
+
#
|
16
|
+
# If you need more control over what gets
|
17
|
+
# fetched and how, use NPR::Client directly,
|
18
|
+
# or the other chainable methods (+where+,
|
19
|
+
# +order+, etc.)
|
20
|
+
#
|
21
|
+
# It is not possible to pass options into this
|
22
|
+
# method. Therefore, you *must* globally
|
23
|
+
# configure at least the apiKey using
|
24
|
+
# +NPR.configure+.
|
25
|
+
#
|
26
|
+
# Example:
|
27
|
+
#
|
28
|
+
# NPR::Entity::Story.find(1000)
|
29
|
+
#
|
30
|
+
def find(id)
|
31
|
+
response = query_by_id(id)
|
32
|
+
|
33
|
+
if !response.messages.empty?
|
34
|
+
response.messages
|
35
|
+
else
|
36
|
+
response.list.stories.first
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
#-------------------------
|
41
|
+
# Same as above, but returns +nil+ if a single story isn't
|
42
|
+
# returned.
|
43
|
+
def find_by_id(id)
|
44
|
+
response = query_by_id(id)
|
45
|
+
|
46
|
+
if !response.messages.empty?
|
47
|
+
nil
|
48
|
+
else
|
49
|
+
response.list.stories.first
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
#-------------------------
|
54
|
+
# For each class builder method defined in NPR::API::QueryBuilder,
|
55
|
+
# define a method on this class which initializes a new
|
56
|
+
# QueryBuilder and proxies to its corresponding method on
|
57
|
+
# Query Builder class.
|
58
|
+
#
|
59
|
+
# We could use ActiveSupport's delegation for this, but
|
60
|
+
# this is simple enough.
|
61
|
+
NPR::API::QueryBuilder::CLASS_BUILDER_METHODS.each do |method|
|
62
|
+
define_method method do |args|
|
63
|
+
NPR::API::QueryBuilder.new(self).send(method, args)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
#-------------------------
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def query_by_id(id)
|
72
|
+
client = NPR::API::Client.new(
|
73
|
+
:apiKey => NPR.config.apiKey,
|
74
|
+
:output => "json")
|
75
|
+
|
76
|
+
client.query(:id => id)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
#-------------------------
|
81
|
+
# Association accessors
|
82
|
+
|
83
|
+
# Attributes that we are using as-is
|
84
|
+
# Use strings so that we don't have to
|
85
|
+
# convert between Strings and Symbols.
|
86
|
+
ATTR_AS_IS = [
|
87
|
+
"thumbnail",
|
88
|
+
"parent",
|
89
|
+
"container"
|
90
|
+
]
|
91
|
+
attr_accessor *ATTR_AS_IS
|
92
|
+
attr_accessor :id, :text, :textWithHtml
|
93
|
+
|
94
|
+
#------------------
|
95
|
+
|
96
|
+
has_many "images", :key => "image", :class_name => NPR::Entity::Image
|
97
|
+
has_many "bylines", :key => "byline", :class_name => NPR::Entity::Byline
|
98
|
+
has_many "audio", :key => "audio", :class_name => NPR::Entity::Audio
|
99
|
+
has_many "organizations", :key => "organization", :class_name => NPR::Entity::Organization
|
100
|
+
has_many "links", :key => "link", :class_name => NPR::Entity::Link
|
101
|
+
has_many "related_links", :key => "relatedLink", :class_name => NPR::Entity::RelatedLink
|
102
|
+
has_many "pull_quotes", :key => "pullQuote", :class_name => NPR::Entity::PullQuote
|
103
|
+
has_many "shows", :key => "show", :class_name => NPR::Entity::Show
|
104
|
+
has_many "members", :key => "member", :class_name => NPR::Entity::Member
|
105
|
+
has_many "list_texts", :key => "listText", :class_name => NPR::Entity::ListText
|
106
|
+
has_many "promo_arts", :key => "promoArt", :class_name => NPR::Entity::PromoArt
|
107
|
+
has_many "book_editions", :key => "bookEdition", :class_name => NPR::Entity::BookEdition
|
108
|
+
|
109
|
+
has_one "transcript", :class_name => NPR::Entity::Transcript
|
110
|
+
|
111
|
+
#------------------
|
112
|
+
|
113
|
+
shallow_attribute(
|
114
|
+
"title",
|
115
|
+
"partnerId",
|
116
|
+
"subtitle",
|
117
|
+
"shortTitle",
|
118
|
+
"teaser",
|
119
|
+
"miniTeaser",
|
120
|
+
"slug",
|
121
|
+
"storyDate",
|
122
|
+
"pubDate",
|
123
|
+
"lastModifiedDate",
|
124
|
+
"keywords",
|
125
|
+
"priorityKeywords",
|
126
|
+
"fullText"
|
127
|
+
)
|
128
|
+
|
129
|
+
#-------------------------
|
130
|
+
|
131
|
+
def initialize(json)
|
132
|
+
@_json = json
|
133
|
+
@id = @_json["id"].to_i
|
134
|
+
|
135
|
+
build_text
|
136
|
+
extract_shallow_attributes(@_json)
|
137
|
+
create_relations(@_json)
|
138
|
+
end
|
139
|
+
|
140
|
+
#-------------------------
|
141
|
+
# The primary image. Looks at the "type" attribute on
|
142
|
+
# an image and finds any with type "primary". If none
|
143
|
+
# are found, then return the first image of any type.
|
144
|
+
def primary_image
|
145
|
+
@primary_image ||= begin
|
146
|
+
primary = self.images.find { |i| i["type"] == "primary"}
|
147
|
+
primary || self.images.first
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
#-------------------------
|
152
|
+
|
153
|
+
private
|
154
|
+
|
155
|
+
#-------------------------
|
156
|
+
|
157
|
+
def build_text
|
158
|
+
if @_json["text"]
|
159
|
+
@text = NPR::Entity::Text.new(@_json["text"])
|
160
|
+
end
|
161
|
+
|
162
|
+
if @_json["textWithHtml"]
|
163
|
+
@textWithHtml = NPR::Entity::Text.new(@_json["textWithHtml"])
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end # Story
|
167
|
+
end # Entity
|
168
|
+
|
169
|
+
#-------------------------
|
170
|
+
# Alias
|
171
|
+
# This is a shorthand so that users of this library
|
172
|
+
# can work with just NPR::Story, but allowing us to
|
173
|
+
# keep everything organized properly.
|
174
|
+
#
|
175
|
+
# The other classes under Entity aren't part of the
|
176
|
+
# library's public API, so they don't need to be
|
177
|
+
# aliased.
|
178
|
+
Story = NPR::Entity::Story
|
179
|
+
end # NPR
|
@@ -0,0 +1,22 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::Text
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class Text < Base
|
7
|
+
has_many "paragraphs", :key => "paragraph", :class_name => NPR::Entity::Paragraph
|
8
|
+
|
9
|
+
#-----------------
|
10
|
+
|
11
|
+
def initialize(json)
|
12
|
+
create_relations(json)
|
13
|
+
end
|
14
|
+
|
15
|
+
#-----------------
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
@paragraphs.map(&:to_s).join("\n")
|
19
|
+
end
|
20
|
+
end # Text
|
21
|
+
end # Entity
|
22
|
+
end # NPR
|
@@ -0,0 +1,21 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::Title
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class Title < Base
|
7
|
+
attr_accessor :num, :content
|
8
|
+
|
9
|
+
def initialize(json)
|
10
|
+
@num = json["num"].to_i
|
11
|
+
@content = json["$text"]
|
12
|
+
end
|
13
|
+
|
14
|
+
#--------------------
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
@content
|
18
|
+
end
|
19
|
+
end # Title
|
20
|
+
end # Entity
|
21
|
+
end # NPR
|
@@ -0,0 +1,16 @@
|
|
1
|
+
##
|
2
|
+
# NPR::Entity::Transcript
|
3
|
+
#
|
4
|
+
module NPR
|
5
|
+
module Entity
|
6
|
+
class Transcript < Base
|
7
|
+
has_one "link", :class_name => NPR::Entity::Link
|
8
|
+
|
9
|
+
#-------------------
|
10
|
+
|
11
|
+
def initialize(json)
|
12
|
+
create_relations(json)
|
13
|
+
end
|
14
|
+
end # Transcript
|
15
|
+
end # Entity
|
16
|
+
end # NPR
|
data/lib/npr/entity.rb
ADDED
data/lib/npr/errors.rb
ADDED
data/lib/npr/version.rb
CHANGED
data/lib/npr.rb
CHANGED
@@ -1,5 +1,66 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
##
|
2
|
+
# NPR
|
3
|
+
#
|
3
4
|
module NPR
|
4
|
-
# Your code goes here...
|
5
5
|
end
|
6
|
+
|
7
|
+
#------------------
|
8
|
+
# Dependencies
|
9
|
+
require 'faraday'
|
10
|
+
require 'faraday_middleware'
|
11
|
+
require "npr/core_ext/array/wrap"
|
12
|
+
|
13
|
+
#------------------
|
14
|
+
# Internal
|
15
|
+
require "npr/version"
|
16
|
+
require "npr/configuration"
|
17
|
+
require "npr/errors"
|
18
|
+
require "npr/concern"
|
19
|
+
require "npr/concern/attr_typecast"
|
20
|
+
require "npr/concern/shallow_attributes"
|
21
|
+
require "npr/concern/relation"
|
22
|
+
|
23
|
+
#------------------
|
24
|
+
# API handlers
|
25
|
+
require "npr/api"
|
26
|
+
require "npr/api/query_builder"
|
27
|
+
require "npr/api/client"
|
28
|
+
require "npr/api/message"
|
29
|
+
require "npr/api/response"
|
30
|
+
|
31
|
+
#------------------
|
32
|
+
# NPR Entities
|
33
|
+
require "npr/entity"
|
34
|
+
require "npr/entity/base"
|
35
|
+
require "npr/entity/link"
|
36
|
+
require "npr/entity/paragraph"
|
37
|
+
require "npr/entity/organization"
|
38
|
+
require "npr/entity/transcript"
|
39
|
+
require "npr/entity/pull_quote"
|
40
|
+
require "npr/entity/related_link"
|
41
|
+
require "npr/entity/list_text"
|
42
|
+
require "npr/entity/author"
|
43
|
+
require "npr/entity/member_promo_art"
|
44
|
+
require "npr/entity/title"
|
45
|
+
require "npr/entity/intro_text"
|
46
|
+
require "npr/entity/member_byline"
|
47
|
+
require "npr/entity/promo_art_book_edition"
|
48
|
+
require "npr/entity/member"
|
49
|
+
require "npr/entity/collection"
|
50
|
+
require "npr/entity/provider"
|
51
|
+
require "npr/entity/enlargement"
|
52
|
+
require "npr/entity/crop"
|
53
|
+
require "npr/entity/image"
|
54
|
+
require "npr/entity/promo_art"
|
55
|
+
require "npr/entity/mp3"
|
56
|
+
require "npr/entity/formats"
|
57
|
+
require "npr/entity/permissions"
|
58
|
+
require "npr/entity/audio"
|
59
|
+
require "npr/entity/book"
|
60
|
+
require "npr/entity/book_edition"
|
61
|
+
require "npr/entity/byline"
|
62
|
+
require "npr/entity/program"
|
63
|
+
require "npr/entity/show"
|
64
|
+
require "npr/entity/text"
|
65
|
+
require "npr/entity/story"
|
66
|
+
require "npr/entity/list"
|
data/npr.gemspec
CHANGED
@@ -4,8 +4,8 @@ require File.expand_path('../lib/npr/version', __FILE__)
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Bryan Ricker"]
|
6
6
|
gem.email = ["bricker@scpr.org"]
|
7
|
-
gem.description = %q{
|
8
|
-
gem.summary = %q{A
|
7
|
+
gem.description = %q{[NPR](http://npr.org) is a news organization. This gem helps you pull its content with a nice Ruby DSL. <http://www.npr.org/api/index>}
|
8
|
+
gem.summary = %q{A Ruby client for the NPR API}
|
9
9
|
gem.homepage = "http://github.com/bricker88/npr"
|
10
10
|
|
11
11
|
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
@@ -14,4 +14,14 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.name = "npr"
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = NPR::VERSION
|
17
|
+
|
18
|
+
gem.licenses = ['MIT']
|
19
|
+
|
20
|
+
gem.add_dependency 'faraday', ['>= 0.8.0']
|
21
|
+
gem.add_dependency 'faraday_middleware', ['>= 0.9.0']
|
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']
|
17
27
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
## Fixture Index
|
2
|
+
|
3
|
+
* list - 3002 - All Topics
|
4
|
+
* 01 - 167019577 - Story with 1 Audio, 1 Image, 1 Byline
|
5
|
+
* 02 - 166884441 - Multiple Images
|
6
|
+
* 03 - 167016221 - No Results (404-ish)
|
7
|
+
* 04 - 00 - Invalid ID
|
8
|
+
* 05 - Missing API Key
|
9
|
+
* 06 - 167055503,166956822 - More than one ID returned (on purpose)
|
10
|
+
|
11
|
+
(awesome story: 166480907)
|
12
|
+
|
13
|
+
## Adding more fixtures
|
14
|
+
|
15
|
+
`fetch_fixtures.rb` provides an easy way to pull a story
|
16
|
+
from the NPR API in all of the possible formats. This
|
17
|
+
allows us to more easily test edge cases. To use, run
|
18
|
+
from this directory (`spec/fixtures`):
|
19
|
+
|
20
|
+
ruby fetch_formats.rb --key "YOUR_API_KEY" --id 166946294 --filename 04_story_multiple_bylines
|
21
|
+
|
22
|
+
The arguments are:
|
23
|
+
|
24
|
+
```
|
25
|
+
--key : Your API Key.
|
26
|
+
--id : The ID of the story that you want to pull
|
27
|
+
--filename : The filename that the responses will be
|
28
|
+
written to (in their respective format).
|
29
|
+
(No extension)
|
30
|
+
```
|