post_haste 0.5 → 0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +6 -2
- data/lib/post_haste.rb +2 -2
- data/lib/post_haste/article.rb +21 -8
- data/lib/post_haste/comment.rb +27 -0
- data/lib/post_haste/version.rb +1 -1
- metadata +4 -3
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# PostHaste
|
2
2
|
|
3
|
-
A Ruby library that wraps the JSON endpoints provided for Washington Post articles and blog posts. Potentially suitable for building custom feeds of Washington Post content, in the event that you don't want to actually visit washingtonpost.com. It handles articles and blog posts from the Post's CMS, as well as WordPress-powered blogs, which have slightly different output.
|
3
|
+
A Ruby library that wraps the JSON endpoints provided for Washington Post articles and blog posts. Potentially suitable for building custom feeds of Washington Post content, in the event that you don't want to actually visit washingtonpost.com. It handles articles and blog posts from the Post's CMS (along with the most recent 15 comments from those), as well as WordPress-powered blogs, which have slightly different output.
|
4
4
|
|
5
5
|
Tested under Ruby 1.9.2 & 1.9.3.
|
6
6
|
|
@@ -24,7 +24,7 @@ Post Haste currently can accept a URL of a Washington Post article or blog post,
|
|
24
24
|
|
25
25
|
url = "http://www.washingtonpost.com/blogs/the-fix/post/republicans-on-the-2012-gop-field-blah/2012/03/15/gIQAT7CSFS_blog.html"
|
26
26
|
|
27
|
-
@article = Article.create_from_url(url)
|
27
|
+
@article = Article.create_from_url(url, 25) # 25 is comment limit, default is 15.
|
28
28
|
|
29
29
|
@article.title
|
30
30
|
|
@@ -33,6 +33,10 @@ Post Haste currently can accept a URL of a Washington Post article or blog post,
|
|
33
33
|
@article.display_datetime.to_s
|
34
34
|
|
35
35
|
=> "2012-03-16T06:30:00-04:00"
|
36
|
+
|
37
|
+
@article.comments.first.author
|
38
|
+
|
39
|
+
=> "Horatio_Swaggbottom"
|
36
40
|
|
37
41
|
## Contributing
|
38
42
|
|
data/lib/post_haste.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
%w(article).each do |f|
|
2
|
-
require File.join(File.dirname(__FILE__), '
|
1
|
+
%w(article.rb comment.rb).each do |f|
|
2
|
+
require File.join(File.dirname(__FILE__), 'post_haste/', f)
|
3
3
|
end
|
data/lib/post_haste/article.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'open-uri'
|
2
|
+
require 'uri'
|
2
3
|
require 'json'
|
3
4
|
|
4
5
|
module PostHaste
|
@@ -7,7 +8,7 @@ module PostHaste
|
|
7
8
|
|
8
9
|
attr_reader :uuid, :type, :title, :blurb, :has_correction, :correction, :has_clarification, :clarification, :permalink, :short_url, :email_url,
|
9
10
|
:comments_url, :graphic_url, :video_url, :byline, :organization, :credits, :created_datetime, :published_datetime, :display_datetime, :updated_datetime,
|
10
|
-
:section, :tags
|
11
|
+
:section, :tags, :comments
|
11
12
|
|
12
13
|
def initialize(params={})
|
13
14
|
params.each_pair do |k,v|
|
@@ -15,10 +16,21 @@ module PostHaste
|
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
18
|
-
def self.
|
19
|
+
def self.latest_comments_url(url, limit)
|
20
|
+
escaped_uri = URI.escape(url)
|
21
|
+
"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"
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.parse_latest_comments(article, comments_url)
|
25
|
+
results = JSON.parse(open(comments_url).read)
|
26
|
+
Comment.create_comments_from_objects(article, results['entries'])
|
27
|
+
end
|
28
|
+
|
29
|
+
# comment limit defaults to 15, but can be set higher or lower
|
30
|
+
def self.create_from_url(url, comment_limit=nil)
|
19
31
|
json_url, source = get_json(url)
|
20
32
|
result = parse_json(json_url)
|
21
|
-
create_from_source(source, result)
|
33
|
+
create_from_source(source, result, comment_limit)
|
22
34
|
end
|
23
35
|
|
24
36
|
# Given a Washington Post story or blog url, can turn that url into a JSON API endpoint
|
@@ -44,16 +56,17 @@ module PostHaste
|
|
44
56
|
Time.at(seconds.to_i).to_datetime
|
45
57
|
end
|
46
58
|
|
47
|
-
def self.create_from_source(source, result)
|
59
|
+
def self.create_from_source(source, result, comment_limit)
|
48
60
|
if source == 'cms'
|
49
|
-
create(result)
|
61
|
+
create(result, comment_limit)
|
50
62
|
elsif source == 'wordpress'
|
51
63
|
create_from_wordpress(result)
|
52
64
|
end
|
53
65
|
end
|
54
66
|
|
55
67
|
# creates an Article object from a JSON response
|
56
|
-
|
68
|
+
# with 15 latest comments, can be configured.
|
69
|
+
def self.create(params={}, limit=15)
|
57
70
|
self.new :type => params['contentConfig']['type'],
|
58
71
|
:uuid => params['contentConfig']['uuid'],
|
59
72
|
:title => params['contentConfig']['title'],
|
@@ -76,8 +89,8 @@ module PostHaste
|
|
76
89
|
:display_datetime => parse_datetime(params['contentConfig']['dateConfig']['displayDate']),
|
77
90
|
:updated_datetime => parse_datetime(params['contentConfig']['dateConfig']['dateUpdated']),
|
78
91
|
:section => params['metaConfig']['section'],
|
79
|
-
:tags => params['metaConfig']['tags']
|
80
|
-
|
92
|
+
:tags => params['metaConfig']['tags'],
|
93
|
+
:comments => parse_latest_comments(params['contentConfig']['permaLinkURL'], latest_comments_url(params['contentConfig']['permaLinkURL'], limit=15))
|
81
94
|
|
82
95
|
end
|
83
96
|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'uri'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module PostHaste
|
6
|
+
class Comment
|
7
|
+
# Represents a comment on a Washington Post story
|
8
|
+
|
9
|
+
attr_reader :id, :article_url, :author, :content, :status, :published
|
10
|
+
|
11
|
+
def initialize(params={})
|
12
|
+
params.each_pair do |k,v|
|
13
|
+
instance_variable_set("@#{k}", v)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.create_comments_from_objects(article_url, comments)
|
18
|
+
results = []
|
19
|
+
comments.each do |comment|
|
20
|
+
c = Comment.new({:id => comment['object']['id'], :article_url => article_url, :author => comment['actor']['title'], :content => comment['object']['content'], :status => comment['object']['status'], :published => DateTime.parse(comment['object']['published'])})
|
21
|
+
results << c
|
22
|
+
end
|
23
|
+
results
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/lib/post_haste/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: post_haste
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.6'
|
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: 2013-
|
12
|
+
date: 2013-02-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- Rakefile
|
91
91
|
- lib/post_haste.rb
|
92
92
|
- lib/post_haste/article.rb
|
93
|
+
- lib/post_haste/comment.rb
|
93
94
|
- lib/post_haste/version.rb
|
94
95
|
- post_haste.gemspec
|
95
96
|
- test/post_haste/test_article.rb
|
@@ -108,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
108
109
|
version: '0'
|
109
110
|
segments:
|
110
111
|
- 0
|
111
|
-
hash:
|
112
|
+
hash: 1471246428543457843
|
112
113
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
114
|
none: false
|
114
115
|
requirements:
|