mccandlish 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTJhZjNhNmI0NWZlYzM0NDc4YzI2NWJkNjFkMmNkODExMzExYjBiMQ==
5
+ data.tar.gz: !binary |-
6
+ YWY4YzIwMjdmNDA5MzkzNmY1MjczNTAwY2UwN2Y2ZWY1YmIxMTgzZg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ODlmYzI4ZWVkZmNhYmIxOWQ5MTE3OWM2NWMxZTM0OTk5NzkxYzA4NmJkMTA4
10
+ YTMwZTVkNTY3MDAzMjNhYTU4YTA4NzkyZjE4ZjJkNmY4MTVlMDQwY2M0YmVi
11
+ NTcxZTdlY2RkOTk0N2Y2NGI2NjM4YTI5ZmZlZjliMDUzNjNjOWU=
12
+ data.tar.gz: !binary |-
13
+ MmNiMWE2MWRkZWM0MWI0YjY1YTQ2MjlmMTJkMGY3MzljOGY1NjlhMWM5OGU2
14
+ OTdlY2E4MDY3MGNlNTIyNmJiNWQxNTFiMWI5MmI1NDk4ZGIwZDk5Zjg4MjE3
15
+ OWJhZWM4OWMyY2ZhNGRmZmFjYTM3M2I5MjQ2YWMzYjE2YzkxM2I=
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ Gemfile.lock
6
+ pkg
7
+ spec/reports
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mccandlish.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 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,45 @@
1
+ # McCandlish
2
+
3
+ A thin Ruby wrapper for NYT Article Search API Version 2. The name comes from McCandlish Phillips, ["a tenacious reporter and a lyrical stylist"](http://www.nytimes.com/2013/04/10/business/media/mccandlish-phillips-times-reporter-dies-at-85.html?pagewanted=all) for The New York Times from 1952 to 1973.
4
+
5
+ ## THIS LIBRARY IS IN ALPHA STATE AND WILL CHANGE FREQUENTLY. DO NOT USE IT FOR ANYTHING, REALLY ##
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'mccandlish'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mccandlish
20
+
21
+ ## Usage
22
+
23
+ You'll need an API key from [The New York Times](http://developer.nytimes.com/). Reading the [Article Search V2 API docs](http://developer.nytimes.com/docs/read/article_search_api_v2) wouldn't hurt, either. But once you have a key, you can get started like so:
24
+
25
+ ```
26
+ require 'mccandlish'
27
+ # create a client
28
+ c = Mccandlish::Client.new('YOUR-API-KEY')
29
+ # retrieve API results for articles and blog posts mentioning Afghanistan from the Foreign Desk on May 26, 2013:
30
+ results = c.desk("Foreign").date("2013-05-26").query("Afghanistan").result
31
+ results.hits
32
+ => 4
33
+ article = results.articles.first
34
+ => #<Mccandlish::Article:0x007fca04897b20 @id="51a176ad46fdbf6c1db79139", @web_url="http://www.nytimes.com/2013/05/26/world/asia/in-afghan-transition-us-forces-take-a-step-back.html", @snippet="As the United States military moves into a support role in Afghanistan, a week spent with a brigade accompanying Afghan forces offered a direct look at the evolving training mission, for better or for worse.", ... @type_of_material="News", @word_count=1243>
35
+ article.headline
36
+ => "In Afghan Transition, U.S. Forces Take a Step Back"
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler'
3
+ require "bundler/gem_tasks"
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rake/testtask'
7
+ Rake::TestTask.new(:test) do |test|
8
+ test.libs << 'lib' << 'spec'
9
+ test.pattern = 'spec/*.rb'
10
+ test.verbose = true
11
+ end
12
+
13
+ task :default => :test
data/lib/mccandlish.rb ADDED
@@ -0,0 +1,7 @@
1
+ %w(article.rb client.rb result.rb version.rb).each do |f|
2
+ require File.join(File.dirname(__FILE__), 'mccandlish/', f)
3
+ end
4
+
5
+ module Mccandlish
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,49 @@
1
+ module Mccandlish
2
+
3
+ class Article
4
+
5
+ attr_reader :id, :web_url, :snippet, :lead_paragraph, :abstract, :print_page, :blog, :source, :headline_seo, :headline,
6
+ :headline_print, :keywords, :date, :document_type, :news_desk, :byline, :type_of_material, :id, :word_count, :results,
7
+ :byline, :section_name, :subsection_name, :kicker
8
+
9
+ def initialize(params={})
10
+ params.each_pair do |k,v|
11
+ instance_variable_set("@#{k}", v)
12
+ end
13
+ end
14
+
15
+ def self.create_from_results(results)
16
+ results.map{|result| Article.create(result)}
17
+ end
18
+
19
+ def self.create(result)
20
+ self.new(:id => result['_id'],
21
+ :web_url => result['web_url'],
22
+ :snippet => result['snippet'],
23
+ :lead_paragraph => result['lead_paragraph'],
24
+ :abstract => result['abstract'],
25
+ :print_page => result['print_page'].to_i,
26
+ :blog => result['blog'],
27
+ :source => result['source'],
28
+ :headline_seo => result['headline']['seo'],
29
+ :headline => result['headline']['main'],
30
+ :kicker => result['headline']['kicker'],
31
+ :headline_print => result['headline']['print'],
32
+ :keywords => result['keywords'].sort_by{|k| k['rank'].to_i},
33
+ :date => Date.parse(result['pub_date']),
34
+ :document_type => result['document_type'],
35
+ :news_desk => result['news_desk'],
36
+ :section_name => result['section_name'],
37
+ :subsection_name => result['subsection_name'],
38
+ :byline => result['byline'],
39
+ :type_of_material => result['type_of_material'],
40
+ :word_count => result['word_count'].to_i
41
+ )
42
+ end
43
+
44
+
45
+
46
+
47
+
48
+ end
49
+ end
@@ -0,0 +1,122 @@
1
+ require 'httparty'
2
+ require 'oj'
3
+ require 'cgi'
4
+
5
+ module Mccandlish
6
+
7
+ class Client
8
+
9
+ include HTTParty
10
+
11
+ attr_reader :sort, :page, :query_filters, :result, :uri, :query, :api_key, :params
12
+
13
+ def initialize(api_key=nil)
14
+ @api_key = api_key
15
+ @params = {}
16
+ @query_filters = []
17
+ @uri
18
+ end
19
+
20
+ # clears out params, query_filters
21
+ def reset
22
+ @params = {}
23
+ @query_filters = []
24
+ self
25
+ end
26
+
27
+ # Builds a request URI to call the API server
28
+ def build_request_url(params)
29
+ "http://api.nytimes.com/svc/search/v2/articlesearch.json?"+params
30
+ end
31
+
32
+ def prepare_params(params, api_key)
33
+ params.map {|k,v| k+'='+v.to_s}.join('&')+"&api-key=#{api_key}"
34
+ end
35
+
36
+ def invoke(params={})
37
+ raise "You must initialize the API key before you run any API queries" if self.api_key.nil?
38
+ full_params = prepare_params(params, api_key=self.api_key)
39
+ @uri = build_request_url(full_params)
40
+ response = HTTParty.get(@uri)
41
+ parsed_response = check_response(response)
42
+ Result.create_from_parsed_response(parsed_response)
43
+ end
44
+
45
+ def check_response(response)
46
+ # replace with actual error handling
47
+ raise "Authentication Error" if response.code == 403
48
+ raise "Bad Request" if response.code == 400
49
+ raise "Server Error" if response.code == 500
50
+ raise "Timeout" if response.code == 504
51
+ if response.code == 200
52
+ Oj.load(response.body)
53
+ else
54
+ return nil
55
+ end
56
+ end
57
+
58
+ def result
59
+ invoke(params)
60
+ end
61
+
62
+ def query(query)
63
+ params['q'] = CGI.escape(query)
64
+ params.merge!({'fq' => query_filters.uniq.join("+AND+")})
65
+ self
66
+ end
67
+
68
+ # newest or oldest
69
+ def sort(sort)
70
+ params['sort'] = sort
71
+ self
72
+ end
73
+
74
+ # response has maximum of 10 items, page is for pagination of results
75
+ def page(page)
76
+ params['page'] = page
77
+ end
78
+
79
+ def dates(begin_date, end_date)
80
+ params['begin_date'] = begin_date if begin_date
81
+ params['end_date'] = end_date if end_date
82
+ self
83
+ end
84
+
85
+ # Full day name: Monday, Tuesday, Wednesday, etc.
86
+ def day_of_week(day)
87
+ query_filters << "day_of_week:#{day}"
88
+ self
89
+ end
90
+
91
+ # YYYY-MM-DD format
92
+ def date(date)
93
+ query_filters << "pub_date:#{date}"
94
+ self
95
+ end
96
+
97
+ def year(year)
98
+ query_filters << "pub_year:#{year}"
99
+ self
100
+ end
101
+
102
+ def location(location)
103
+ loc = CGI.escape(location)
104
+ query_filters << "glocations:#{loc}"
105
+ self
106
+ end
107
+
108
+ # article, blogpost
109
+ def doc_type(doc_type)
110
+ query_filters << "document_type:#{doc_type}"
111
+ self
112
+ end
113
+
114
+ # Foreign, Sports, Culture, etc.
115
+ def desk(desk)
116
+ # validate desk
117
+ query_filters << "news_desk:#{desk}"
118
+ self
119
+ end
120
+ end
121
+
122
+ end
@@ -0,0 +1,24 @@
1
+ module Mccandlish
2
+
3
+ class Result
4
+
5
+ attr_reader :hits, :offset, :copyright, :status, :articles
6
+
7
+ def initialize(params={})
8
+ params.each_pair do |k,v|
9
+ instance_variable_set("@#{k}", v)
10
+ end
11
+ end
12
+
13
+ def self.create_from_parsed_response(results)
14
+ self.new(:hits => results['response']['meta']['hits'],
15
+ :offset => results['response']['meta']['offset'],
16
+ :copyright => results['copyright'],
17
+ :status => results['status'],
18
+ :articles => Article.create_from_results(results['response']['docs'])
19
+ )
20
+ end
21
+
22
+
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Mccandlish
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mccandlish/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mccandlish"
8
+ spec.version = Mccandlish::VERSION
9
+ spec.authors = ["Derek Willis"]
10
+ spec.email = ["dwillis@gmail.com"]
11
+ spec.description = %q{A thin, hopefully elegant Ruby wrapper for Version 2 of the NYT Article Search API}
12
+ spec.summary = %q{NYT Article Search API Ruby gem}
13
+ spec.homepage = ""
14
+ spec.license = "Apache"
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 "minitest"
24
+ spec.add_development_dependency "webmock"
25
+ spec.add_dependency "oj"
26
+ spec.add_dependency "httparty"
27
+ spec.add_dependency "american_date"
28
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ require 'mccandlish'
3
+
4
+ describe Client do
5
+ it "should start with the website" do
6
+
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ require "minitest/autorun"
2
+ require_relative "../lib/mccandlish"
3
+ require 'webmock/minitest'
4
+ include Mccandlish
5
+
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mccandlish
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Derek Willis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: oj
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: american_date
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A thin, hopefully elegant Ruby wrapper for Version 2 of the NYT Article
112
+ Search API
113
+ email:
114
+ - dwillis@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - lib/mccandlish.rb
125
+ - lib/mccandlish/article.rb
126
+ - lib/mccandlish/client.rb
127
+ - lib/mccandlish/result.rb
128
+ - lib/mccandlish/version.rb
129
+ - mccandlish.gemspec
130
+ - spec/mccandlish/spec_client.rb
131
+ - spec/spec_helper.rb
132
+ homepage: ''
133
+ licenses:
134
+ - Apache
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.0.3
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: NYT Article Search API Ruby gem
156
+ test_files:
157
+ - spec/mccandlish/spec_client.rb
158
+ - spec/spec_helper.rb