Skip to content

Commit 710e23b

Browse files
Initial library.
1 parent 4fcd513 commit 710e23b

19 files changed

+850
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
doc
2+
pkg

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2009 frankieroberto
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.

README

Whitespace-only changes.

README.rdoc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
= guardian-content
2+
3+
This gem allows you to interface with the Guardian-Content-API[http://www.guardian.co.uk/open-platform] via Ruby.
4+
5+
== Installation
6+
7+
Installation is as simple as typing the following text into a command prompt (such as Terminal):
8+
9+
gem install guardian-content
10+
11+
== Usage
12+
13+
To use the library within a project, simply include the line <code>require 'guardian-content'</code> - you can then use any of the methods listed below.
14+
15+
Whilst the Guardian Content API doesn't require an API key for basic usage, for increased rate limits, and the ability to retrieve full text content, it is recommended that you register for an API key at http://guardian.mashery.com/. Once authorised, you can use the key via either declaring a <code>GUARDIAN_CONTENT_API_KEY</code> constant, or via the <code>key</code> param when initialising the class.
16+
17+
=== Basic searching
18+
19+
To do a basic free text search of Guardian documents, simply use the following command:
20+
21+
articles = GuardianContent::Content.search("your search term")
22+
23+
If successful, this will the return an array of results, which you can loop through and access via some standard attributes:
24+
25+
articles.each do |article|
26+
puts article.title
27+
puts article.url
28+
end
29+
30+
To include additional meta-data in the results, use the <code>:select</code> parameter. There are two types of data you can request, tags and fields. To select all of both, use <code>:select => {:tags => :all, :fields => :all}</code>. You can then access the tags and keywords via attributes:
31+
32+
articles = GuardianContent::Content.search("your search term", :select => {:tags => :all, :fields => :all})
33+
34+
articles.first.tags.each do |tag|
35+
puts tag[:id]
36+
puts tag[:title]
37+
end
38+
39+
puts articles.first.fields[:standfirst]
40+
puts articles.first.fields[:headline]
41+
42+
=== Retrieving an article based on id
43+
44+
Each article or content item has a unique id. This can be accessed either via the #id attribute on a returned search result, or you can determine it from the Guardian website itself - the id is simply everything in an article's URL after the host name - eg for http://www.guardian.co.uk/world/2010/may/17/iran-nuclear-uranium-swap-turkey the id is <code>world/2010/may/17/iran-nuclear-uranium-swap-turkey</code>.
45+
46+
To retrieve an article based on its id, use the #find_by_id method:
47+
48+
article = GuardianContent::Content.find_by_id("world/2010/may/17/iran-nuclear-uranium-swap-turkey")
49+
50+
The article's content and meta-data can then be accessed in the same way as for search results.
51+
52+
== Note on Patches/Pull Requests
53+
54+
* Fork the project.
55+
* Make your feature addition or bug fix.
56+
* Add tests for it. This is important so I don't break it in a
57+
future version unintentionally.
58+
* Commit, do not mess with rakefile, version, or history.
59+
(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)
60+
* Send me a pull request. Bonus points for topic branches.
61+
62+
== Copyright
63+
64+
Copyright (c) 2010 frankieroberto. See LICENSE for details.

Rakefile

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = "guardian-content"
8+
gem.summary = %Q{Queries the Guardian Content API}
9+
gem.description = %Q{A library for the Guardian Content API}
10+
gem.email = "frankie@frankieroberto.com"
11+
gem.homepage = "http://github.com/frankieroberto/guardian-content"
12+
gem.authors = ["frankieroberto"]
13+
gem.add_development_dependency "shoulda", ">= 2.10.3"
14+
gem.add_dependency('httparty', '>= 0.5.2')
15+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16+
end
17+
Jeweler::GemcutterTasks.new
18+
rescue LoadError
19+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20+
end
21+
22+
require 'rake/testtask'
23+
Rake::TestTask.new(:test) do |test|
24+
test.libs << 'lib' << 'test'
25+
test.pattern = 'test/**/test_*.rb'
26+
test.verbose = true
27+
end
28+
29+
begin
30+
require 'rcov/rcovtask'
31+
Rcov::RcovTask.new do |test|
32+
test.libs << 'test'
33+
test.pattern = 'test/**/test_*.rb'
34+
test.verbose = true
35+
end
36+
rescue LoadError
37+
task :rcov do
38+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39+
end
40+
end
41+
42+
task :test => :check_dependencies
43+
44+
task :default => :test
45+
46+
require 'rake/rdoctask'
47+
Rake::RDocTask.new do |rdoc|
48+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
49+
50+
rdoc.rdoc_dir = 'rdoc'
51+
rdoc.title = "guardian-content #{version}"
52+
rdoc.rdoc_files.include('README*')
53+
rdoc.rdoc_files.include('lib/**/*.rb')
54+
end

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.2.0

guardian-content.gemspec

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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{guardian-content}
8+
s.version = "0.2.0"
9+
10+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11+
s.authors = ["frankieroberto"]
12+
s.date = %q{2010-05-17}
13+
s.description = %q{A library for the Guardian Content API}
14+
s.email = %q{frankie@frankieroberto.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+
"guardian-content.gemspec",
27+
"lib/guardian-content.rb",
28+
"lib/guardian-content/content.rb",
29+
"lib/guardian-content/contributor.rb",
30+
"lib/guardian-content/extensions.rb",
31+
"lib/guardian-content/keyword.rb",
32+
"lib/guardian-content/section.rb",
33+
"lib/guardian-content/tag.rb",
34+
"test/helper.rb",
35+
"test/test_guardian-content.rb",
36+
"test/test_search_content.rb",
37+
"test/test_section.rb",
38+
"test/test_tag_search.rb"
39+
]
40+
s.homepage = %q{http://github.com/frankieroberto/guardian-content}
41+
s.rdoc_options = ["--charset=UTF-8"]
42+
s.require_paths = ["lib"]
43+
s.rubygems_version = %q{1.3.6}
44+
s.summary = %q{Queries the Guardian Content API}
45+
s.test_files = [
46+
"test/helper.rb",
47+
"test/test_guardian-content.rb",
48+
"test/test_search_content.rb",
49+
"test/test_section.rb",
50+
"test/test_tag_search.rb"
51+
]
52+
53+
if s.respond_to? :specification_version then
54+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
55+
s.specification_version = 3
56+
57+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
58+
s.add_development_dependency(%q<shoulda>, [">= 2.10.3"])
59+
s.add_runtime_dependency(%q<httparty>, [">= 0.5.2"])
60+
else
61+
s.add_dependency(%q<shoulda>, [">= 2.10.3"])
62+
s.add_dependency(%q<httparty>, [">= 0.5.2"])
63+
end
64+
else
65+
s.add_dependency(%q<shoulda>, [">= 2.10.3"])
66+
s.add_dependency(%q<httparty>, [">= 0.5.2"])
67+
end
68+
end
69+

lib/guardian-content.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module GuardianContent
2+
3+
require 'httparty'
4+
5+
6+
def self.new(*params)
7+
GuardianContent::Base.new(*params)
8+
end
9+
10+
class Base
11+
include HTTParty
12+
base_uri 'http://content.guardianapis.com/'
13+
14+
def initialize(key = nil)
15+
if defined?(GUARDIAN_CONTENT_API_KEY)
16+
key = GUARDIAN_CONTENT_API_KEY
17+
end
18+
self.class.default_params "api-key" => key, "format" => "json"
19+
end
20+
21+
def reload(options = nil)
22+
@attributes.update(self.class.find_by_id(self.id, options).instance_variable_get('@attributes'))
23+
self
24+
end
25+
26+
27+
end
28+
end
29+
30+
%w(extensions content section tag keyword contributor).each do |file|
31+
require File.join(File.dirname(__FILE__), 'guardian-content', file)
32+
end

lib/guardian-content/content.rb

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
class GuardianContent::Content < GuardianContent::Base
2+
3+
attr_reader :title, :id, :url, :publication_date, :section_id, :body, :keywords, :fields, :headline, :tags, :attributes
4+
5+
def initialize(attributes = {})
6+
@attributes = attributes
7+
@id = attributes[:id]
8+
@title = attributes[:webTitle]
9+
@url = attributes[:webUrl]
10+
@section_id = attributes[:sectionId]
11+
@publication_date = DateTime.parse(attributes[:webPublicationDate]) if attributes && attributes[:webPublicationDate]
12+
@body = attributes[:body]
13+
14+
if attributes[:tags]
15+
@tags = []
16+
attributes[:tags].each do |tag|
17+
@tags << tag
18+
end
19+
end
20+
21+
@fields = attributes[:fields].nested_symbolize_keys! if attributes[:fields]
22+
23+
if attributes[:fields]
24+
@headline = attributes[:fields][:headline]
25+
end
26+
end
27+
28+
def inspect
29+
"#<Article id: \"" + self.id.to_s + "\" title: \"" + self.title.to_s + "\" + url: \"" + self.url.to_s + "\">"
30+
end
31+
32+
def section
33+
return GuardianContent::Section.find_by_id(self.section_id)
34+
end
35+
36+
# Does a text search for content, with various options.
37+
# === Parameters
38+
# * <tt>:conditions</tt> - A hash which can accept <tt>:tag</tt>, <tt>:section</tt>, <tt>:from</tt> and <tt>:to</tt>, eg: <code>:conditions => {:tag => "poltiics/gordon-brown", :from => Date.yesterday}</code>
39+
# * <tt>:limit</tt> - An integer determining the maximum number of results to return (max: 50).
40+
# * <tt>:order</tt> - The order of the results. Can be <tt>:relevance</tt>, <tt>:oldest</tt> or <tt>:newest</tt>
41+
# === Examples
42+
# * GuardianContent::Content.search("election")
43+
# * GuardianContent::Content.search("election", :conditions => {:section => "politics"})
44+
def self.search(q, options = {})
45+
46+
query = {}
47+
query["page-size"] = options[:limit] if options[:limit]
48+
query["order-by"] = options[:order] if options[:order]
49+
50+
if options[:select]
51+
if options[:select][:tags]
52+
query["show-tags"] = options[:select][:tags]
53+
end
54+
if options[:select][:fields]
55+
query["show-fields"] = options[:select][:fields]
56+
end
57+
end
58+
59+
if options[:conditions]
60+
conditions = options[:conditions]
61+
62+
query[:tag] = conditions[:tag] if conditions[:tag]
63+
query[:section] = conditions[:section] if conditions[:section]
64+
65+
query["from-date"] = Date.parse(conditions[:from]).to_s if conditions[:from]
66+
query["to-date"] = Date.parse(conditions[:to]).to_s if conditions[:to]
67+
68+
end
69+
70+
71+
query[:format] = "json"
72+
73+
query[:q] = q
74+
75+
opts = {:query => query}
76+
77+
response = self.get("/search", opts).nested_symbolize_keys![:response]
78+
79+
results = recursively_symbolize_keys!(response[:results])
80+
content = []
81+
82+
results.each do |result|
83+
content << GuardianContent::Content.new(recursively_symbolize_keys!(result))
84+
end
85+
return content
86+
end
87+
88+
# Fetch a Content item using its id. IDs are usually in the form of <tt>section/YYYY/month/DD/name-of-article</tt>.
89+
def self.find_by_id(id, options = {})
90+
91+
query = {}
92+
query["show-fields"] = "all"
93+
query["show-tags"] = "all"
94+
query[:format] = "json"
95+
opts = {:query => query}
96+
97+
attributes = {}
98+
99+
response = recursively_symbolize_keys!self.get("/#{id}", opts).nested_symbolize_keys![:response]
100+
101+
content = response[:content]
102+
103+
return GuardianContent::Content.new(recursively_symbolize_keys!(content))
104+
105+
end
106+
107+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class GuardianContent::Contributor < GuardianContent::Tag
2+
3+
attr_reader :title, :id, :url
4+
5+
def initialize(attributes = {})
6+
@id = attributes[:id] || nil
7+
@title = attributes[:web_title] || nil
8+
@url = attributes[:web_url] || nil
9+
end
10+
11+
def inspect
12+
"#<Contributor id:" + self.id.to_s + " title:\"" + self.title.to_s + "\">"
13+
end
14+
15+
def self.find_by_id(id)
16+
17+
query = {}
18+
query["show-fields"] = "all"
19+
query[:format] = "json"
20+
opts = {:query => query}
21+
22+
return GuardianContent::Contributor.new(self.get("/#{id}").nested_symbolize_keys![:response][:tag])
23+
24+
end
25+
26+
end

0 commit comments

Comments
 (0)