nytimes-events 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 +22 -0
- data/README.md +60 -0
- data/Rakefile +2 -0
- data/lib/nytimes-events/base.rb +29 -0
- data/lib/nytimes-events/list.rb +70 -0
- data/lib/nytimes-events/version.rb +5 -0
- data/lib/nytimes-events.rb +12 -0
- data/nytimes-events.gemspec +21 -0
- data/spec/base_spec.rb +13 -0
- data/spec/list_spec.rb +65 -0
- data/spec/spec-helper.rb +3 -0
- metadata +109 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Dylan Drop
|
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,60 @@
|
|
1
|
+
# Nytimes::Events
|
2
|
+
|
3
|
+
A Ruby wrapper for the NYTimes Event Listings API.
|
4
|
+
|
5
|
+
Currently v0.0.1. As of right now, you can supply any legal parameters and receive the response back from NYTimes as a JSON hash. Next to come: faceted search, spatial search, and more robust error handling. Possible other features include an option for XML response. Also, if there are any other features that people may want, feel free to contribute or let me know.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'nytimes-events'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install nytimes-events
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
A simple usage example, as of right now:
|
24
|
+
|
25
|
+
listing = Nytimes::Event::List.new(MY_API_KEY)
|
26
|
+
listing.find('query' => 'food')
|
27
|
+
|
28
|
+
`search` takes a hash of parameters which directly correspond to the parameters found on the NYTimes' Event Listings API page. You must initialize any object with your API key which can be obtained from the NYTimes API website.
|
29
|
+
|
30
|
+
Query parameters can be found here: http://developer.nytimes.com/docs/read/events_api
|
31
|
+
|
32
|
+
You will receive back a JSON hash of events.
|
33
|
+
|
34
|
+
**IMPORTANT**
|
35
|
+
|
36
|
+
`nytimes-event` is based off of the linked-list data structure. That is, `Nytimes::Event::List` actually retrieves `batch_size` number of events, and you can retrieve the next `batch_size` events or the previous `batch_size` events.
|
37
|
+
|
38
|
+
`batch_size` by default is 20.
|
39
|
+
|
40
|
+
For example:
|
41
|
+
|
42
|
+
listing = Nytimes::Event::List.new(MY_API_KEY)
|
43
|
+
listing.batch_size = 10
|
44
|
+
listing.find('query' => 'food')
|
45
|
+
#=> next retrieves the next 10 events
|
46
|
+
listing.next
|
47
|
+
#=> previous retrieves the previous 10 events
|
48
|
+
listing.prev
|
49
|
+
|
50
|
+
Also, you can reset to head (such that the results are those which would appear if you hadn't called next or previous at all) by calling `to_head`:
|
51
|
+
|
52
|
+
listing.to_head
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Nytimes
|
2
|
+
module Events
|
3
|
+
class Base
|
4
|
+
API_SERVER = 'api.nytimes.com'
|
5
|
+
API_VERSION = 'v2'
|
6
|
+
API_NAME = 'events'
|
7
|
+
API_OBJECTS = 'listings'
|
8
|
+
RESPONSE_TYPE = 'json'
|
9
|
+
API_URL = "http://#{API_SERVER}/svc/#{API_NAME}/#{API_VERSION}/listings.#{RESPONSE_TYPE}"
|
10
|
+
|
11
|
+
def initialize(api_key)
|
12
|
+
@api_key = api_key
|
13
|
+
end
|
14
|
+
|
15
|
+
def api_key=(key)
|
16
|
+
@api_key = key
|
17
|
+
end
|
18
|
+
|
19
|
+
def api_key
|
20
|
+
@api_key
|
21
|
+
end
|
22
|
+
|
23
|
+
def api_url(params)
|
24
|
+
prmstr = params.collect { |k, v| "#{k.to_s}=#{v.to_s.gsub(' ','+')}" }.join('&') + "&api-key=" + api_key
|
25
|
+
API_URL + "?" + prmstr
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Nytimes
|
2
|
+
module Events
|
3
|
+
class List < Base
|
4
|
+
attr_reader :batch_size
|
5
|
+
|
6
|
+
def initialize(api_key, batch_size = 20)
|
7
|
+
super(api_key)
|
8
|
+
@batch_size = batch_size
|
9
|
+
@current_offset = 0
|
10
|
+
@results = 0
|
11
|
+
@previous_params = Hash.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def find(params = {})
|
15
|
+
to_head if previous_params_differ_from(params)
|
16
|
+
|
17
|
+
@previous_params = params
|
18
|
+
response = RestClient.get(api_url_for(params))
|
19
|
+
|
20
|
+
case response_code_for response
|
21
|
+
when 403
|
22
|
+
raise RestClient::Exception, "Access forbidden by NYTimes API. Perhaps the API key isn't working?"
|
23
|
+
when 200
|
24
|
+
json = JSON.parse(response)
|
25
|
+
set_results_returned(json)
|
26
|
+
json
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def batch_size=(amount)
|
31
|
+
@batch_size = amount
|
32
|
+
end
|
33
|
+
|
34
|
+
def next_page
|
35
|
+
@current_offset += @batch_size unless @current_offset + @batch_size > @results
|
36
|
+
find(@previous_params)
|
37
|
+
end
|
38
|
+
alias :next :next_page
|
39
|
+
|
40
|
+
def prev_page
|
41
|
+
@current_offset -= @batch_size unless @batch_size > @current_offset
|
42
|
+
find(@previous_params)
|
43
|
+
end
|
44
|
+
alias :previous :prev_page
|
45
|
+
|
46
|
+
def to_head
|
47
|
+
@current_offset = 0
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def previous_params_differ_from(params)
|
53
|
+
params = params.delete_if {|key, value| key == 'offset' || key == 'limit' }
|
54
|
+
params != @previous_params
|
55
|
+
end
|
56
|
+
|
57
|
+
def response_code_for response
|
58
|
+
response.net_http_res.instance_of?(Fixnum) ? response.net_http_res : response.code
|
59
|
+
end
|
60
|
+
|
61
|
+
def api_url_for params
|
62
|
+
api_url(params.merge({'limit' => @batch_size, 'offset' => @current_offset}))
|
63
|
+
end
|
64
|
+
|
65
|
+
def set_results_returned incoming_json
|
66
|
+
@results = incoming_json['num_results'].to_i
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/nytimes-events/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dylan"]
|
6
|
+
gem.email = ["dylandrop@gmail.com"]
|
7
|
+
gem.description = %q{NYTimes Events Ruby API}
|
8
|
+
gem.summary = %q{A Ruby wrapper for the NYTimes Events API.}
|
9
|
+
gem.homepage = "https://github.com/dylandrop/nytimes-events"
|
10
|
+
|
11
|
+
gem.add_development_dependency "rspec"
|
12
|
+
gem.add_dependency "rest-client", ">0"
|
13
|
+
gem.add_dependency "json", ">0"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($\)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.name = "nytimes-events"
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
gem.version = Nytimes::Events::VERSION
|
21
|
+
end
|
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec-helper'
|
2
|
+
|
3
|
+
describe Nytimes::Events::Base do
|
4
|
+
it "should form the correct url from a basic query string" do
|
5
|
+
base = Nytimes::Events::Base.new("XXXX")
|
6
|
+
base.api_url('query' => 'food').should == "http://api.nytimes.com/svc/events/v2/listings.json?query=food&api-key=XXXX"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should substitute spaces with plusses" do
|
10
|
+
base = Nytimes::Events::Base.new("XXXX")
|
11
|
+
base.api_url('query' => 'foo bar').should == "http://api.nytimes.com/svc/events/v2/listings.json?query=foo+bar&api-key=XXXX"
|
12
|
+
end
|
13
|
+
end
|
data/spec/list_spec.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec-helper'
|
2
|
+
|
3
|
+
describe Nytimes::Events::List do
|
4
|
+
it "raises an exception on 403" do
|
5
|
+
listing = Nytimes::Events::List.new("XXXX")
|
6
|
+
RestClient.stub(:get).and_return(RestClient::Response.create("{}", 403, {}))
|
7
|
+
expect {listing.find('foo' => 'bar')}.to raise_error(RestClient::Exception)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "parses the body of the response on 200" do
|
11
|
+
listing = Nytimes::Events::List.new("XXXX")
|
12
|
+
RestClient.stub(:get).and_return(RestClient::Response.create("{\"thing1\":\"thing2\"}", 200, {}))
|
13
|
+
listing.find('foo' => 'bar').should == {'thing1' => 'thing2'}
|
14
|
+
end
|
15
|
+
|
16
|
+
it "allows you to set and retrieve the batch size" do
|
17
|
+
listing = Nytimes::Events::List.new("XXXX")
|
18
|
+
listing.batch_size = 40
|
19
|
+
listing.batch_size.should == 40
|
20
|
+
end
|
21
|
+
|
22
|
+
it "fetches the next 'batch' number of items when next_page is requested" do
|
23
|
+
listing = Nytimes::Events::List.new("XXXX")
|
24
|
+
RestClient.stub(:get).and_return(RestClient::Response.create("{\"num_results\":\"23\"}", 200, {}))
|
25
|
+
listing.find('foo' => 'bar')
|
26
|
+
listing.next_page
|
27
|
+
listing.instance_variable_get(:@current_offset).should == listing.instance_variable_get(:@batch_size)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "fetches the last 'batch' number of items when next_page is requested" do
|
31
|
+
listing = Nytimes::Events::List.new("XXXX")
|
32
|
+
RestClient.stub(:get).and_return(RestClient::Response.create("{\"thing1\":\"thing2\"}", 200, {}))
|
33
|
+
listing.find('foo' => 'bar')
|
34
|
+
listing.instance_variable_set(:@current_offset, 20)
|
35
|
+
listing.prev_page
|
36
|
+
listing.instance_variable_get(:@current_offset).should == 0
|
37
|
+
end
|
38
|
+
|
39
|
+
it "doesn't fetch the last items if the current offset would be negative" do
|
40
|
+
listing = Nytimes::Events::List.new("XXXX")
|
41
|
+
RestClient.stub(:get).and_return(RestClient::Response.create("{\"thing1\":\"thing2\"}", 200, {}))
|
42
|
+
listing.find('foo' => 'bar')
|
43
|
+
listing.instance_variable_set(:@current_offset, 0)
|
44
|
+
listing.prev_page
|
45
|
+
listing.instance_variable_get(:@current_offset).should == 0
|
46
|
+
end
|
47
|
+
|
48
|
+
it "doesn't fetch the next items if the new current offset would be greater than the number of results" do
|
49
|
+
listing = Nytimes::Events::List.new("XXXX")
|
50
|
+
RestClient.stub(:get).and_return(RestClient::Response.create("{\"num_results\":\"6\"}", 200, {}))
|
51
|
+
listing.find('foo' => 'bar')
|
52
|
+
listing.instance_variable_set(:@current_offset, 0)
|
53
|
+
listing.next_page
|
54
|
+
listing.instance_variable_get(:@current_offset).should == 0
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should reset to head if new query is performed" do
|
58
|
+
listing = Nytimes::Events::List.new("XXXX")
|
59
|
+
RestClient.stub(:get).and_return(RestClient::Response.create("{\"num_results\":\"6\"}", 200, {}))
|
60
|
+
listing.find('foo' => 'bar')
|
61
|
+
listing.instance_variable_set(:@current_offset, 40)
|
62
|
+
listing.find('foo' => 'qwerty')
|
63
|
+
listing.instance_variable_get(:@current_offset).should == 0
|
64
|
+
end
|
65
|
+
end
|
data/spec/spec-helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nytimes-events
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dylan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
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: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rest-client
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>'
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
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: json
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>'
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
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: NYTimes Events Ruby API
|
63
|
+
email:
|
64
|
+
- dylandrop@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/nytimes-events.rb
|
75
|
+
- lib/nytimes-events/base.rb
|
76
|
+
- lib/nytimes-events/list.rb
|
77
|
+
- lib/nytimes-events/version.rb
|
78
|
+
- nytimes-events.gemspec
|
79
|
+
- spec/base_spec.rb
|
80
|
+
- spec/list_spec.rb
|
81
|
+
- spec/spec-helper.rb
|
82
|
+
homepage: https://github.com/dylandrop/nytimes-events
|
83
|
+
licenses: []
|
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.24
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: A Ruby wrapper for the NYTimes Events API.
|
106
|
+
test_files:
|
107
|
+
- spec/base_spec.rb
|
108
|
+
- spec/list_spec.rb
|
109
|
+
- spec/spec-helper.rb
|