nytimes-article-search 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/nytimes-article-search +3 -0
- data/lib/nytimes-article/version.rb +3 -0
- data/lib/nytimes-article.rb +68 -0
- data/nytimes-article-search.gemspec +24 -0
- data/spec/nytimes_article_spec.rb +36 -0
- data/spec/spec_helper.rb +11 -0
- metadata +108 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kazuhiro Yamada
|
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,29 @@
|
|
1
|
+
# NytimesArticle
|
2
|
+
|
3
|
+
A thin Ruby client for The New York Times Article Search API
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'nytimes-article-search'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install nytimes-article-search
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'json'
|
4
|
+
require 'date'
|
5
|
+
require "nytimes-article/version"
|
6
|
+
|
7
|
+
module NytimesArticle
|
8
|
+
class << self
|
9
|
+
API_SERVER = 'api.nytimes.com'
|
10
|
+
API_VERSION = 'v1'
|
11
|
+
API_NAME = 'search'
|
12
|
+
API_BASE = "/svc/#{API_NAME}/#{API_VERSION}/article"
|
13
|
+
|
14
|
+
@@api_key = nil
|
15
|
+
@@debug = false
|
16
|
+
@@copyright = nil
|
17
|
+
@@semantic_api_key = nil
|
18
|
+
|
19
|
+
##
|
20
|
+
# Set the API key used for operations. This needs to be called before any requests against the API. To obtain an API key, go to http://developer.nytimes.com/
|
21
|
+
def api_key=(key)
|
22
|
+
@@api_key = key
|
23
|
+
end
|
24
|
+
|
25
|
+
def api_key
|
26
|
+
@@api_key
|
27
|
+
end
|
28
|
+
|
29
|
+
def semantic_api_key=(key)
|
30
|
+
@@semantic_api_key = key
|
31
|
+
end
|
32
|
+
|
33
|
+
def api_key
|
34
|
+
@@semantic_api_key
|
35
|
+
end
|
36
|
+
|
37
|
+
def datetime_parser(datetime)
|
38
|
+
datetime ? DateTime.parse(datetime) : nil
|
39
|
+
end
|
40
|
+
|
41
|
+
##
|
42
|
+
# Builds a request URI to call the API server
|
43
|
+
def build_request_url(params)
|
44
|
+
URI::HTTP.build :host => API_SERVER, :path => API_BASE, :query => params.map {|k,v| "#{k}=#{v}"}.join('&')
|
45
|
+
end
|
46
|
+
|
47
|
+
def search(params={})
|
48
|
+
begin
|
49
|
+
if @@api_key.nil?
|
50
|
+
raise "You must initialize the API key before you run any API queries"
|
51
|
+
end
|
52
|
+
full_params = params.merge 'api-key' => @@api_key, 'format' => 'json'
|
53
|
+
uri = build_request_url(full_params)
|
54
|
+
reply = uri.read
|
55
|
+
parsed_reply = JSON.parse reply
|
56
|
+
if parsed_reply.nil?
|
57
|
+
raise "Empty reply returned from API"
|
58
|
+
end
|
59
|
+
parsed_reply
|
60
|
+
rescue OpenURI::HTTPError => e
|
61
|
+
if e.message =~ /^404/
|
62
|
+
return nil
|
63
|
+
end
|
64
|
+
raise "Error connecting to URL #{uri} #{e}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'nytimes-article/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nytimes-article-search"
|
8
|
+
spec.version = NytimesArticle::VERSION
|
9
|
+
spec.authors = ["Kazuhiro Yamada"]
|
10
|
+
spec.email = ["yamadakazu45@gmail.com"]
|
11
|
+
spec.description = %q{A thin Ruby client for The New York Times Article Search API}
|
12
|
+
spec.summary = %q{A thin Ruby client for The New York Times Article Search API}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tempfile'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
describe NytimesArticle do
|
6
|
+
|
7
|
+
describe ".search" do
|
8
|
+
context "search by no query" do
|
9
|
+
before(:each) do
|
10
|
+
@articles = NytimesArticle.search
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return 0 articles" do
|
14
|
+
@articles["total"].should == 0
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "search by query" do
|
19
|
+
before(:each) do
|
20
|
+
@articles = NytimesArticle.search(
|
21
|
+
:query => 'obama',
|
22
|
+
:begin_date => '20130401',
|
23
|
+
:end_date => '20130402'
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return articles" do
|
28
|
+
@articles["total"].should == 26
|
29
|
+
article = @articles['results'][0]
|
30
|
+
article['date'] = '20130402'
|
31
|
+
article['title'] = "U.S. Sees North Korea Blustering, Not Acting"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'nytimes-article' # and any other gems you need
|
5
|
+
|
6
|
+
API_KEY = ENV['NYTIMES_ARTICLE_SEARCH_API_KEY']
|
7
|
+
NytimesArticle.api_key = API_KEY
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
# some (optional) config here
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nytimes-article-search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kazuhiro Yamada
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A thin Ruby client for The New York Times Article Search API
|
63
|
+
email:
|
64
|
+
- yamadakazu45@gmail.com
|
65
|
+
executables:
|
66
|
+
- nytimes-article-search
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- bin/nytimes-article-search
|
76
|
+
- lib/nytimes-article.rb
|
77
|
+
- lib/nytimes-article/version.rb
|
78
|
+
- nytimes-article-search.gemspec
|
79
|
+
- spec/nytimes_article_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
homepage: ''
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.8.23
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: A thin Ruby client for The New York Times Article Search API
|
106
|
+
test_files:
|
107
|
+
- spec/nytimes_article_spec.rb
|
108
|
+
- spec/spec_helper.rb
|