post_haste 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +43 -0
- data/Rakefile +15 -0
- data/lib/post_haste/article.rb +72 -0
- data/lib/post_haste/feed.rb +9 -0
- data/lib/post_haste/version.rb +3 -0
- data/lib/post_haste.rb +3 -0
- data/post_haste.gemspec +26 -0
- data/test/post_haste/test_article.rb +45 -0
- data/test/test_helper.rb +11 -0
- metadata +105 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Derek Willis
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# PostHaste
|
2
|
+
|
3
|
+
A proof-of-concept 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.
|
4
|
+
|
5
|
+
Tested under Ruby 1.9.2, but other versions should work.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'post_haste'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install post_haste
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Post Haste currently can accept a URL of a Washington Post article or blog post, and converts that URL into a Ruby object with a number of methods that describe it, including its title, byline, published and updated datetimes, and more:
|
24
|
+
|
25
|
+
url = "http://www.washingtonpost.com/blogs/the-fix/post/republicans-on-the-2012-gop-field-blah/2012/03/15/gIQAT7CSFS_blog.html"
|
26
|
+
|
27
|
+
@article = Article.create_from_url(url)
|
28
|
+
|
29
|
+
@article.title
|
30
|
+
|
31
|
+
=> "Republicans on the 2012 GOP field: Blah."
|
32
|
+
|
33
|
+
@article.display_datetime.to_s
|
34
|
+
|
35
|
+
=> "2012-03-16T06:30:00-04:00"
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'bundler'
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
include Rake::DSL
|
5
|
+
Bundler::GemHelper.install_tasks
|
6
|
+
|
7
|
+
require 'rake/testtask'
|
8
|
+
Rake::TestTask.new(:test) do |test|
|
9
|
+
test.libs << 'lib' << 'test'
|
10
|
+
test.pattern = 'test/**/test_*.rb'
|
11
|
+
test.verbose = true
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => :test
|
15
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'date'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
# Represents a single Washington Post story or blog post.
|
7
|
+
module PostHaste
|
8
|
+
class Article
|
9
|
+
|
10
|
+
attr_reader :uuid, :type, :title, :blurb, :has_correction, :correction, :has_clarification, :clarification, :permalink, :short_url, :email_url,
|
11
|
+
:comments_url, :graphic_url, :video_url, :byline, :organization, :credits, :created_datetime, :published_datetime, :display_datetime, :updated_datetime,
|
12
|
+
:section, :tags
|
13
|
+
|
14
|
+
def initialize(params={})
|
15
|
+
params.each_pair do |k,v|
|
16
|
+
instance_variable_set("@#{k}", v)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.create_from_url(url)
|
21
|
+
json_url = get_json(url)
|
22
|
+
result = parse_json(json_url)
|
23
|
+
create(result)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Given a Washington Post story or blog url, can turn that url into a JSON API endpoint
|
27
|
+
def self.get_json(url)
|
28
|
+
return url.gsub('_story','_json') if url.include?("_story")
|
29
|
+
return url.gsub('_blog','_json') if url.include?("_blog")
|
30
|
+
end
|
31
|
+
|
32
|
+
# parses a Washington Post story or blog JSON response
|
33
|
+
def self.parse_json(url)
|
34
|
+
JSON.parse(open(url).read)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Post CMS produces unix timestamps, but with extra zeroes
|
38
|
+
def self.parse_datetime(seconds)
|
39
|
+
seconds = seconds.to_s[0..9]
|
40
|
+
Time.at(seconds.to_i).to_datetime
|
41
|
+
end
|
42
|
+
|
43
|
+
# creates an Article object from a JSON response
|
44
|
+
def self.create(params={})
|
45
|
+
self.new :type => params['contentConfig']['type'],
|
46
|
+
:uuid => params['contentConfig']['uuid'],
|
47
|
+
:title => params['contentConfig']['title'],
|
48
|
+
:blurb => params['contentConfig']['blurb'],
|
49
|
+
:has_correction => params['contentConfig']['hasCorrection'],
|
50
|
+
:correction => params['contentConfig']['correction'],
|
51
|
+
:has_clarification => params['contentConfig']['hasClarification'],
|
52
|
+
:clarification => params['contentConfig']['clarification'],
|
53
|
+
:permalink => params['contentConfig']['permaLinkURL'],
|
54
|
+
:short_url => params['contentConfig']['shortURL'],
|
55
|
+
:email_url => params['contentConfig']['emailURL'],
|
56
|
+
:comments_url => params['contentConfig']['commentsURL'],
|
57
|
+
:graphic_url => params['contentConfig']['graphicURL'],
|
58
|
+
:video_url => params['contentConfig']['videoURL'],
|
59
|
+
:byline => params['contentConfig']['credits'].first['name'],
|
60
|
+
:organization => params['contentConfig']['credits'].first['organization'],
|
61
|
+
:credits => params['contentConfig']['credits'].first['credit'],
|
62
|
+
:created_datetime => parse_datetime(params['contentConfig']['dateConfig']['dateCreated']),
|
63
|
+
:published_datetime => parse_datetime(params['contentConfig']['dateConfig']['datePublished']),
|
64
|
+
:display_datetime => parse_datetime(params['contentConfig']['dateConfig']['displayDate']),
|
65
|
+
:updated_datetime => parse_datetime(params['contentConfig']['dateConfig']['dateUpdated']),
|
66
|
+
:section => params['metaConfig']['section'],
|
67
|
+
:tags => params['metaConfig']['tags']
|
68
|
+
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/post_haste.rb
ADDED
data/post_haste.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/post_haste/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Derek Willis"]
|
6
|
+
gem.email = ["dwillis@gmail.com"]
|
7
|
+
gem.description = %q{A Ruby gem for accessing Washington Post articles and blog posts.}
|
8
|
+
gem.summary = %q{Because it was there.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "post_haste"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = PostHaste::VERSION
|
17
|
+
|
18
|
+
gem.required_rubygems_version = ">= 1.3.6"
|
19
|
+
gem.rubyforge_project = "post_haste"
|
20
|
+
gem.add_runtime_dependency "json"
|
21
|
+
|
22
|
+
gem.add_development_dependency "rake", "0.8.7"
|
23
|
+
gem.add_development_dependency "bundler", ">= 1.1.0"
|
24
|
+
gem.add_development_dependency "shoulda"
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestPostHaste::TestArticle < Test::Unit::TestCase
|
4
|
+
include PostHaste
|
5
|
+
|
6
|
+
context "Article.create from article" do
|
7
|
+
setup do
|
8
|
+
url = "http://www.washingtonpost.com/politics/joe-biden-digging-back-into-his-roots-to-move-obama-forward/2012/03/14/gIQARwYBDS_story.html"
|
9
|
+
json_url = Article.get_json(url)
|
10
|
+
@result = Article.parse_json(json_url)
|
11
|
+
@article = Article.create(@result)
|
12
|
+
end
|
13
|
+
|
14
|
+
should "return an object of the Article type" do
|
15
|
+
assert_kind_of(Article, @article)
|
16
|
+
assert_equal(@article.type, 'article')
|
17
|
+
end
|
18
|
+
|
19
|
+
%w(uuid type title blurb).each do |attr|
|
20
|
+
should "assign the value of the @#{attr} attribute from the '#{attr}' key in the hash" do
|
21
|
+
assert_equal(@result['contentConfig'][attr], @article.send(attr))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "Article.create from blog post" do
|
27
|
+
setup do
|
28
|
+
url = "http://www.washingtonpost.com/blogs/the-fix/post/republicans-on-the-2012-gop-field-blah/2012/03/15/gIQAT7CSFS_blog.html"
|
29
|
+
json_url = Article.get_json(url)
|
30
|
+
@result = Article.parse_json(json_url)
|
31
|
+
@article = Article.create(@result)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "return an object of the Article type" do
|
35
|
+
assert_kind_of(Article, @article)
|
36
|
+
assert_equal(@article.type, 'BlogStory')
|
37
|
+
end
|
38
|
+
|
39
|
+
%w(uuid type title blurb).each do |attr|
|
40
|
+
should "assign the value of the @#{attr} attribute from the '#{attr}' key in the hash" do
|
41
|
+
assert_equal(@result['contentConfig'][attr], @article.send(attr))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: post_haste
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Derek Willis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-16 00:00:00.000000000 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: json
|
17
|
+
requirement: &2152614200 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2152614200
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: &2152613660 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - =
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.8.7
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2152613660
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bundler
|
39
|
+
requirement: &2152613160 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.1.0
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *2152613160
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: shoulda
|
50
|
+
requirement: &2152612780 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *2152612780
|
59
|
+
description: A Ruby gem for accessing Washington Post articles and blog posts.
|
60
|
+
email:
|
61
|
+
- dwillis@gmail.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- .gitignore
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- lib/post_haste.rb
|
72
|
+
- lib/post_haste/article.rb
|
73
|
+
- lib/post_haste/feed.rb
|
74
|
+
- lib/post_haste/version.rb
|
75
|
+
- post_haste.gemspec
|
76
|
+
- test/post_haste/test_article.rb
|
77
|
+
- test/test_helper.rb
|
78
|
+
has_rdoc: true
|
79
|
+
homepage: ''
|
80
|
+
licenses: []
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.3.6
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project: post_haste
|
99
|
+
rubygems_version: 1.6.2
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: Because it was there.
|
103
|
+
test_files:
|
104
|
+
- test/post_haste/test_article.rb
|
105
|
+
- test/test_helper.rb
|