Class: PostHaste::Article
- Inherits:
-
Object
- Object
- PostHaste::Article
- Defined in:
- lib/post_haste/article.rb
Instance Attribute Summary collapse
-
#bio_page ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#byline ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#comments ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#comments_url ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#created_datetime ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#display_datetime ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#email ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#keywords ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#mobile_headline ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#permalink ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#published_datetime ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#section ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#short_url ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#summary ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#tags ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#title ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#type ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#updated_datetime ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#uuid ⇒ Object
readonly
Represents a single Washington Post story or blog post.
-
#web_headline ⇒ Object
readonly
Represents a single Washington Post story or blog post.
Class Method Summary collapse
-
.create(result = {}, limit = 25) ⇒ Object
creates an Article object from a JSON response with 25 latest comments, can be configured.
-
.create_from_url(url, comment_limit = 15) ⇒ Object
comment limit defaults to 15, but can be set higher or lower.
-
.get_json(url) ⇒ Object
Given a Washington Post story or blog url, can turn that url into a JSON API endpoint returns the url and the source used in Article creation.
- .latest_comments_url(url, limit) ⇒ Object
-
.parse_datetime(seconds) ⇒ Object
Post CMS produces unix timestamps, but with extra zeroes.
-
.parse_json(url) ⇒ Object
parses a Washington Post story or blog JSON response.
- .parse_latest_comments(article, comments_url) ⇒ Object
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Article
constructor
A new instance of Article.
Constructor Details
#initialize(params = {}) ⇒ Article
Returns a new instance of Article.
12 13 14 15 16 |
# File 'lib/post_haste/article.rb', line 12 def initialize(params={}) params.each_pair do |k,v| instance_variable_set("@#{k}", v) end end |
Instance Attribute Details
#bio_page ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def bio_page @bio_page end |
#byline ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def @byline end |
#comments ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def comments @comments end |
#comments_url ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def comments_url @comments_url end |
#created_datetime ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def created_datetime @created_datetime end |
#display_datetime ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def display_datetime @display_datetime end |
#email ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def email @email end |
#keywords ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def keywords @keywords end |
#mobile_headline ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def mobile_headline @mobile_headline end |
#permalink ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def permalink @permalink end |
#published_datetime ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def published_datetime @published_datetime end |
#section ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def section @section end |
#short_url ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def short_url @short_url end |
#summary ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def summary @summary end |
#tags ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def @tags end |
#title ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def title @title end |
#type ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def type @type end |
#updated_datetime ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def updated_datetime @updated_datetime end |
#uuid ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def uuid @uuid end |
#web_headline ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
9 10 11 |
# File 'lib/post_haste/article.rb', line 9 def web_headline @web_headline end |
Class Method Details
.create(result = {}, limit = 25) ⇒ Object
creates an Article object from a JSON response with 25 latest comments, can be configured.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/post_haste/article.rb', line 53 def self.create(result={}, limit=25) self.new type: result['type'], uuid: result['uuid'], title: result['title'], mobile_headline: result['mobile_headline'], web_headline: result['web_headline'], summary: result['summary'], permalink: result['_id'], short_url: result['short_url'], comments_url: latest_comments_url(result['_id'], limit), keywords: result['clavis_keywords'], byline: result['creator'].map{|c| c['name']}, email: result['creator'].map{|c| c['email']}, bio_page: result['creator'].map{|c| c['bio_page']}, created_datetime: Time.at(result['created_date'].to_i), published_datetime: Time.at(result['published_date'].to_i), display_datetime: Time.at(result['display_date'].to_i), section: result['kicker']['name'], tags: result['tags'], comments: parse_latest_comments(result['_id'], latest_comments_url(result['_id'], limit=limit)) end |
.create_from_url(url, comment_limit = 15) ⇒ Object
comment limit defaults to 15, but can be set higher or lower
29 30 31 32 |
# File 'lib/post_haste/article.rb', line 29 def self.create_from_url(url, comment_limit=15) result = parse_json(get_json(url)) create(result, comment_limit) end |
.get_json(url) ⇒ Object
Given a Washington Post story or blog url, can turn that url into a JSON API endpoint returns the url and the source used in Article creation
36 37 38 |
# File 'lib/post_haste/article.rb', line 36 def self.get_json(url) "http://apps-origin.washingtonpost.com/f/story-builder/api/url?url=#{url}" end |
.latest_comments_url(url, limit) ⇒ Object
18 19 20 21 |
# File 'lib/post_haste/article.rb', line 18 def self.latest_comments_url(url, limit) escaped_uri = URI.escape(url) "http://echoapi.wpdigital.net/api/v1/search?q=((childrenof%3A+#{escaped_uri}+source%3Awashpost.com+(((state%3AUntouched+user.state%3AModeratorApproved)+OR+(state%3ACommunityFlagged%2CModeratorApproved%2CModeratorDeleted+-user.state%3AModeratorBanned%2CModeratorDeleted)+)+)+++))+itemsPerPage%3A+#{limit}+sortOrder%3A+reverseChronological+safeHTML%3Aaggressive+childrenSortOrder%3Achronological+childrenItemsPerPage%3A10+children%3A+1++(((state%3AUntouched+user.state%3AModeratorApproved)+OR+(state%3ACommunityFlagged%2CModeratorApproved+-user.state%3AModeratorBanned%2CModeratorDeleted)+)+)++&appkey=prod.washpost.com" end |
.parse_datetime(seconds) ⇒ Object
Post CMS produces unix timestamps, but with extra zeroes
46 47 48 49 |
# File 'lib/post_haste/article.rb', line 46 def self.parse_datetime(seconds) seconds = seconds.to_s[0..9] Time.at(seconds.to_i).to_datetime end |
.parse_json(url) ⇒ Object
parses a Washington Post story or blog JSON response
41 42 43 |
# File 'lib/post_haste/article.rb', line 41 def self.parse_json(url) JSON.parse(open(url).read) end |
.parse_latest_comments(article, comments_url) ⇒ Object
23 24 25 26 |
# File 'lib/post_haste/article.rb', line 23 def self.parse_latest_comments(article, comments_url) results = JSON.parse(open(comments_url).read) Comment.create_comments_from_objects(article, results['entries']) end |