times_topics 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jacqui Maher
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ = TimesTopics
2
+ A simple wrapper for the TimesTags API and Times Topics
3
+
4
+ Interact with the TimesTags API (http://developer.nytimes.com/docs/timestags_api) with this rubygem. Find people, organizations, locations, and subjects matching queries. Find Times Topics pages for query matches along with related topics, when applicable.
5
+
6
+ = Usage
7
+
8
+ == Note on Patches/Pull Requests
9
+
10
+ * Fork the project.
11
+ * Make your feature addition or bug fix.
12
+ * Add tests for it. This is important so I don't break it in a
13
+ future version unintentionally.
14
+ * Commit, do not mess with rakefile, version, or history.
15
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
16
+ * Send me a pull request. Bonus points for topic branches.
17
+
18
+ == Copyright
19
+
20
+ Copyright (c) 2010 Jacqui Maher. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "times_topics"
8
+ gem.summary = %Q{A simple wrapper for the TimesTags API and Times Topics}
9
+ gem.description = %Q{Interact with the TimesTags API (http://developer.nytimes.com/docs/timestags_api) with this rubygem. Find people, organizations, locations, and subjects matching queries. Find Times Topics pages for query matches along with related topics, when applicable.}
10
+ gem.email = "jacqui.maher@nytimes.com"
11
+ gem.homepage = "http://github.com/jacqui/times_topics"
12
+ gem.authors = ["Jacqui Maher"]
13
+ gem.add_development_dependency "shoulda", ">= 0"
14
+ gem.add_development_dependency "mocha", ">= 0"
15
+ gem.add_development_dependency "yard", ">= 0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ begin
48
+ require 'yard'
49
+ YARD::Rake::YardocTask.new
50
+ rescue LoadError
51
+ task :yardoc do
52
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
53
+ end
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/times_tags.rb ADDED
@@ -0,0 +1,62 @@
1
+ require 'httparty'
2
+
3
+ class TimesTags
4
+ include HTTParty
5
+
6
+ base_uri "http://api.nytimes.com/svc/timestags"
7
+ format :json
8
+
9
+ def self.api_key
10
+ config['api_key']
11
+ end
12
+
13
+ def self.config
14
+ @config ||= YAML::load_file(config_path)
15
+ end
16
+
17
+ # Path to the times_topics.yml file.
18
+ def self.config_path
19
+ if @config_path.nil?
20
+ if defined?(Rails.env)
21
+ @config_path = File.join(RAILS_ROOT, 'config', 'times_topics.yml')
22
+ end
23
+ end
24
+ @config_path
25
+ end
26
+
27
+ def self.config_path= value
28
+ @config_path = value
29
+ end
30
+
31
+ def self.person_search(name)
32
+ get('/suggest', :query => { :query => name, :filter => '(Per)'})
33
+ end
34
+
35
+ def self.organization_search(name)
36
+ get('/suggest', :query => { :query => name, :filter => '(Org)'})
37
+ end
38
+
39
+ def self.geographic_search(name)
40
+ get('/suggest', :query => { :query => name, :filter => '(Geo)'})
41
+ end
42
+
43
+ def self.subject_search(name)
44
+ get('/suggest', :query => { :query => name, :filter => '(Des)'})
45
+ end
46
+
47
+ def self.search(query, type)
48
+ case type
49
+ when /person/
50
+ person_search(query)
51
+ when /organization/
52
+ organization_search(query)
53
+ when /geographic/
54
+ geographic_search(query)
55
+ when /subject|description/
56
+ subject_search(query)
57
+ else
58
+ person_search(query)
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,77 @@
1
+ class TimesTopics
2
+ BASE_URI = 'http://topics.nytimes.com/top/reference/timestopics/'
3
+ attr_accessor :query, :type, :url, :result, :related_results, :num_results
4
+
5
+ def initialize(query, type = 'person')
6
+ @type = type
7
+ @query = query
8
+
9
+ if @result = TimesTags.search(query, type)
10
+ @num_results = @result["num_results"]
11
+ if @num_results > 0
12
+ top_result = @result["results"].shift
13
+ case @type
14
+ when /person/
15
+ @url = build_person_url(top_result)
16
+ when /organization/
17
+ @url = build_organization_url(top_result)
18
+ end
19
+
20
+ if @result["num_results"] > 1
21
+ @related_results = @result["results"]
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def filter(long_form)
28
+ case long_form
29
+ when /org/i
30
+ "(Org)"
31
+ when /people|person/i
32
+ "(Per)"
33
+ when /geo/i
34
+ "(Geo)"
35
+ when /des/i
36
+ "(Des)"
37
+ end
38
+ end
39
+
40
+ def page_exists?
41
+ return false if @url.nil?
42
+ parsed_url = URI.parse(@url)
43
+ request = Net::HTTP::Get.new(parsed_url.path)
44
+ result = Net::HTTP.start(parsed_url.host, parsed_url.port) {|http|
45
+ http.request(request)
46
+ }
47
+ result.is_a?(Net::HTTPOK)
48
+ end
49
+
50
+ def related_result_urls
51
+ return [] if @related_results.nil? || @related_results.empty?
52
+ @related_results.map do |result|
53
+ case @type
54
+ when /person/i
55
+ build_person_url(result)
56
+ when /organization/
57
+ build_organization_url(result)
58
+ end
59
+ end
60
+ end
61
+
62
+ def build_person_url(result)
63
+ # http://topics.nytimes.com/top/reference/timestopics/people/b/osama_bin_laden/index.html
64
+ topic_name = result.sub(filter('person'), "").split(',').map(&:strip).reverse.join('_').gsub(/\s/, '_').downcase
65
+ index_char = result[0,1].downcase
66
+ BASE_URI + 'people/' + index_char + '/' + topic_name + '/index.html'
67
+ end
68
+
69
+ def build_organization_url(result)
70
+ # http://topics.nytimes.com/top/reference/timestopics/organizations/a/al_qaeda/index.html
71
+ topic_name = result.sub(filter('organization'), "").strip.gsub(/\s/, '_').downcase
72
+ index_char = result[0,1].downcase
73
+ BASE_URI + 'organizations/' + index_char + '/' + topic_name + '/index.html'
74
+ end
75
+
76
+
77
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'times_topics'
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,48 @@
1
+ require 'helper'
2
+ require File.join(File.dirname(__FILE__), '../lib/times_tags.rb')
3
+
4
+ class TestTimesTags < Test::Unit::TestCase
5
+ context "API Key" do
6
+ should "find the api key specified in a config file" do
7
+ TimesTags.config_path = File.join(File.dirname(__FILE__), 'times_topics.yml')
8
+ assert_equal "000111222333444555666777888999abcdefg+", TimesTags.api_key
9
+ end
10
+ end
11
+
12
+ context "Searching" do
13
+ setup do
14
+ TimesTags.config_path = File.join(File.dirname(__FILE__), 'times_topics.yml')
15
+ TimesTags.stubs(:get)
16
+ end
17
+ should "person_search" do
18
+ TimesTags.expects(:get).with('/suggest', :query => { :query => 'Sebastien', :filter => '(Per)' })
19
+ TimesTags.person_search('Sebastien')
20
+ end
21
+ should "organization_search" do
22
+ TimesTags.expects(:get).with('/suggest', :query => { :query => 'Sebastien', :filter => '(Org)' })
23
+ TimesTags.organization_search('Sebastien')
24
+ end
25
+ should "geographic_search" do
26
+ TimesTags.expects(:get).with('/suggest', :query => { :query => 'Sebastien', :filter => '(Geo)' })
27
+ TimesTags.geographic_search('Sebastien')
28
+ end
29
+ should "subject_search" do
30
+ TimesTags.expects(:get).with('/suggest', :query => { :query => 'Sebastien', :filter => '(Des)' })
31
+ TimesTags.subject_search('Sebastien')
32
+ end
33
+ should "search" do
34
+ TimesTags.expects(:get).with('/suggest', :query => { :query => 'Sebastien', :filter => '(Des)' })
35
+ TimesTags.search('Sebastien', 'subject')
36
+
37
+ TimesTags.expects(:get).with('/suggest', :query => { :query => 'Sebastien', :filter => '(Geo)' })
38
+ TimesTags.search('Sebastien', 'geographic')
39
+
40
+ TimesTags.expects(:get).with('/suggest', :query => { :query => 'Sebastien', :filter => '(Org)' })
41
+ TimesTags.search('Sebastien', 'organization')
42
+
43
+ TimesTags.expects(:get).with('/suggest', :query => { :query => 'Sebastien', :filter => '(Per)' })
44
+ TimesTags.search('Sebastien', 'person')
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,37 @@
1
+ require 'helper'
2
+
3
+ class TestTimesTopics < Test::Unit::TestCase
4
+ context "TimesTopics" do
5
+ context "not found" do
6
+ setup do
7
+ TimesTags.stubs(:search).returns({ "results"=>[], "filter"=>"(Per)", "num_results"=>0, "query"=>"Not found"})
8
+ @topic = TimesTopics.new("Not found")
9
+ end
10
+ should "page_exists?" do
11
+ assert !@topic.page_exists?
12
+ end
13
+ should "url" do
14
+ assert_nil @topic.url
15
+ end
16
+ should "related_result_urls" do
17
+ assert @topic.related_result_urls.empty?
18
+ end
19
+ end
20
+
21
+ context "result found" do
22
+ setup do
23
+ TimesTags.stubs(:search).returns({"results"=>["bin Laden, Osama (Per)"], "filter"=>"(Per)", "num_results"=>1, "query"=>"Osama Bin Laden"})
24
+ @topic = TimesTopics.new("Osama Bin Laden")
25
+ end
26
+ should "page_exists?" do
27
+ assert @topic.page_exists?
28
+ end
29
+ should "url" do
30
+ assert_equal "http://topics.nytimes.com/top/reference/timestopics/people/b/osama_bin_laden/index.html", @topic.url
31
+ end
32
+ should "related_result_urls" do
33
+ assert @topic.related_result_urls.empty?
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1 @@
1
+ api_key: 000111222333444555666777888999abcdefg+
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{times_topics}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jacqui Maher"]
12
+ s.date = %q{2010-06-03}
13
+ s.description = %q{Interact with the TimesTags API (http://developer.nytimes.com/docs/timestags_api) with this rubygem. Find people, organizations, locations, and subjects matching queries. Find Times Topics pages for query matches along with related topics, when applicable.}
14
+ s.email = %q{jacqui.maher@nytimes.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/times_tags.rb",
27
+ "lib/times_topics.rb",
28
+ "test/helper.rb",
29
+ "test/test_times_tags.rb",
30
+ "test/test_times_topics.rb",
31
+ "test/times_topics.yml",
32
+ "times_topics.gemspec"
33
+ ]
34
+ s.homepage = %q{http://github.com/jacqui/times_topics}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{A simple wrapper for the TimesTags API and Times Topics}
39
+ s.test_files = [
40
+ "test/helper.rb",
41
+ "test/test_times_tags.rb",
42
+ "test/test_times_topics.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
51
+ s.add_development_dependency(%q<mocha>, [">= 0"])
52
+ s.add_development_dependency(%q<yard>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<shoulda>, [">= 0"])
55
+ s.add_dependency(%q<mocha>, [">= 0"])
56
+ s.add_dependency(%q<yard>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<shoulda>, [">= 0"])
60
+ s.add_dependency(%q<mocha>, [">= 0"])
61
+ s.add_dependency(%q<yard>, [">= 0"])
62
+ end
63
+ end
64
+
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: times_topics
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Jacqui Maher
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-03 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: mocha
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: yard
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :development
55
+ version_requirements: *id003
56
+ description: Interact with the TimesTags API (http://developer.nytimes.com/docs/timestags_api) with this rubygem. Find people, organizations, locations, and subjects matching queries. Find Times Topics pages for query matches along with related topics, when applicable.
57
+ email: jacqui.maher@nytimes.com
58
+ executables: []
59
+
60
+ extensions: []
61
+
62
+ extra_rdoc_files:
63
+ - LICENSE
64
+ - README.rdoc
65
+ files:
66
+ - .document
67
+ - .gitignore
68
+ - LICENSE
69
+ - README.rdoc
70
+ - Rakefile
71
+ - VERSION
72
+ - lib/times_tags.rb
73
+ - lib/times_topics.rb
74
+ - test/helper.rb
75
+ - test/test_times_tags.rb
76
+ - test/test_times_topics.rb
77
+ - test/times_topics.yml
78
+ - times_topics.gemspec
79
+ has_rdoc: true
80
+ homepage: http://github.com/jacqui/times_topics
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options:
85
+ - --charset=UTF-8
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.3.6
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: A simple wrapper for the TimesTags API and Times Topics
109
+ test_files:
110
+ - test/helper.rb
111
+ - test/test_times_tags.rb
112
+ - test/test_times_topics.rb