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.
8 9 10 11 12 |
# File 'lib/post_haste/article.rb', line 8 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.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def bio_page @bio_page end |
#byline ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def @byline end |
#comments ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def comments @comments end |
#comments_url ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def comments_url @comments_url end |
#created_datetime ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def created_datetime @created_datetime end |
#display_datetime ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def display_datetime @display_datetime end |
#email ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def email @email end |
#keywords ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def keywords @keywords end |
#mobile_headline ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def mobile_headline @mobile_headline end |
#permalink ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def permalink @permalink end |
#published_datetime ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def published_datetime @published_datetime end |
#section ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def section @section end |
#short_url ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def short_url @short_url end |
#summary ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def summary @summary end |
#tags ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def @tags end |
#title ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def title @title end |
#type ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def type @type end |
#updated_datetime ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def updated_datetime @updated_datetime end |
#uuid ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 def uuid @uuid end |
#web_headline ⇒ Object (readonly)
Represents a single Washington Post story or blog post.
5 6 7 |
# File 'lib/post_haste/article.rb', line 5 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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/post_haste/article.rb', line 50 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
25 26 27 28 29 |
# File 'lib/post_haste/article.rb', line 25 def self.create_from_url(url, comment_limit=15) url.gsub('/blogs/','/news/') if url.include?('/blogs/') 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
33 34 35 |
# File 'lib/post_haste/article.rb', line 33 def self.get_json(url) "http://apps-origin.washingtonpost.com/f/story-builder/api/url/?url=#{url}" end |
.latest_comments_url(url, limit) ⇒ Object
14 15 16 17 |
# File 'lib/post_haste/article.rb', line 14 def self.latest_comments_url(url, limit) escaped_uri = URI.escape(url) "https://comments-api.ext.nile.works/v1/search?q=((childrenof%3A+#{escaped_uri}+source%3Awashpost.com+(((state%3AUntouched++AND+user.state%3AModeratorApproved)+OR+(state%3ACommunityFlagged%2CModeratorApproved%2CModeratorDeleted+AND+-user.state%3AModeratorBanned%2CModeratorDeleted)+)++AND+(+-markers%3Aignore+)+)+++))+itemsPerPage%3A+15+sortOrder%3AreverseChronological+safeHTML%3Aaggressive+children%3A+2+childrenSortOrder%3Achronological+childrenItemsPerPage%3A3++(((state%3AUntouched++AND+user.state%3AModeratorApproved)+OR+(state%3ACommunityFlagged%2CModeratorApproved+AND+-user.state%3AModeratorBanned%2CModeratorDeleted)+)++AND+(+-markers%3Aignore+)+)+&appkey=prod.washpost.com" end |
.parse_datetime(seconds) ⇒ Object
Post CMS produces unix timestamps, but with extra zeroes
43 44 45 46 |
# File 'lib/post_haste/article.rb', line 43 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
38 39 40 |
# File 'lib/post_haste/article.rb', line 38 def self.parse_json(url) HTTParty.get(url).parsed_response end |
.parse_latest_comments(article, comments_url) ⇒ Object
19 20 21 22 |
# File 'lib/post_haste/article.rb', line 19 def self.parse_latest_comments(article, comments_url) results = HTTParty.get(comments_url).parsed_response Comment.create_comments_from_objects(article, results['entries']) end |