theguardian 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/theguardian.rb +23 -0
- data/lib/theguardian/api/item.rb +31 -0
- data/lib/theguardian/api/search.rb +54 -0
- data/lib/theguardian/api/section.rb +32 -0
- data/lib/theguardian/api/tag.rb +42 -0
- data/lib/theguardian/connection.rb +21 -0
- data/lib/theguardian/content_api.rb +19 -0
- data/lib/theguardian/version.rb +3 -0
- data/spec/spec_helper.rb +7 -0
- data/theguardian.gemspec +30 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2a32db6465bd9c8c8476d3495c6d33dcbe748dcc
|
4
|
+
data.tar.gz: 4972b9caff04c4d5704488708081960277fc5c51
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 35a851414b0f3389a81ef5b87abaa0f209c2261be5e2bb812e7e84c3627fbbe3bd0c36ea6306f7c515d78c8628877bf7048ad0bd72940bb9580ea41f466b26c6
|
7
|
+
data.tar.gz: 78fe846497fcf4b80a33d53a8b5669bf273dc4fab4ea462b62c74116f44e062c93801fdc0f948b05099cb12e1899a5d805345784bda7a811f8cc21f52e78ea1a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mohamed Magdy
|
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
|
+
# Theguardian
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'theguardian'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install theguardian
|
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"
|
data/lib/theguardian.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "theguardian/version"
|
2
|
+
|
3
|
+
# JSON
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
# REST Client
|
7
|
+
require "rest_client"
|
8
|
+
|
9
|
+
# Addressable
|
10
|
+
require "addressable/uri"
|
11
|
+
|
12
|
+
# OpenStruct
|
13
|
+
require "recursive-open-struct"
|
14
|
+
|
15
|
+
# TheGuardian connection file
|
16
|
+
require "theguardian/connection"
|
17
|
+
|
18
|
+
# TheGuardian API files
|
19
|
+
require "theguardian/content_api"
|
20
|
+
require "theguardian/api/search"
|
21
|
+
require "theguardian/api/tag"
|
22
|
+
require "theguardian/api/section"
|
23
|
+
require "theguardian/api/item"
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Theguardian
|
2
|
+
module Api
|
3
|
+
class Item < RecursiveOpenStruct
|
4
|
+
def initialize(*params)
|
5
|
+
content = params.first["content"] ? params.first.merge(params.first["content"]["fields"] ) : params.first
|
6
|
+
item_params = params.first["fields"] || content
|
7
|
+
|
8
|
+
super(item_params, params.last)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.process(params)
|
12
|
+
connection = Theguardian::Connection.new(resource: params.delete(:id))
|
13
|
+
self.new(connection.get(self.process_params(params)), recurse_over_arrays: true)
|
14
|
+
end
|
15
|
+
|
16
|
+
def fetch
|
17
|
+
self.class.process({ id: URI(self.apiUrl).path })
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.process_params(params)
|
21
|
+
# The item id
|
22
|
+
theguardian_api_params = { id: params[:id] }
|
23
|
+
|
24
|
+
# The show fields
|
25
|
+
theguardian_api_params.merge!({ "show-fields" => params[:fields] || "all" })
|
26
|
+
|
27
|
+
theguardian_api_params.reject{ |_, value| value.nil? }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Theguardian
|
2
|
+
module Api
|
3
|
+
class Search < RecursiveOpenStruct
|
4
|
+
def self.process(params = {})
|
5
|
+
connection = Theguardian::Connection.new(resource: "search")
|
6
|
+
params = self.process_params(params)
|
7
|
+
|
8
|
+
self.new(connection.get(params), recurse_over_arrays: true)
|
9
|
+
end
|
10
|
+
|
11
|
+
def items
|
12
|
+
self.results.map do |result|
|
13
|
+
Theguardian::Api::Item.new(result.to_h)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.process_params(params)
|
18
|
+
theguardian_api_params = { q: params[:q] }
|
19
|
+
|
20
|
+
# Date
|
21
|
+
if params.has_key?(:date)
|
22
|
+
if params[:date].has_key?(:from)
|
23
|
+
theguardian_api_params.merge!({ "from-date" => params[:date][:from].strftime("%Y-%m-%d") })
|
24
|
+
end
|
25
|
+
|
26
|
+
if params[:date].has_key?(:to)
|
27
|
+
theguardian_api_params.merge!({ "to-date" => params[:date][:to].strftime("%Y-%m-%d") })
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Tags
|
32
|
+
theguardian_api_params.merge!({ "tag" => params[:tags].join(",") }) if params.has_key?(:tags)
|
33
|
+
|
34
|
+
# Sections
|
35
|
+
theguardian_api_params.merge!({ "section" => params[:sections].join(",") }) if params.has_key?(:sections)
|
36
|
+
|
37
|
+
# Shown fields
|
38
|
+
theguardian_api_params.merge!({ "show-fields" => params[:fields].join(",") }) if params[:fields]
|
39
|
+
|
40
|
+
# Refinements
|
41
|
+
theguardian_api_params.merge!({ "show-refinements" => params[:refinements].join(",") }) if params.has_key?(:refinements)
|
42
|
+
|
43
|
+
# Ordering
|
44
|
+
theguardian_api_params.merge!({ "order-by" => params[:order_by] }) if params.has_key?(:order_by)
|
45
|
+
|
46
|
+
# Pagination
|
47
|
+
theguardian_api_params.merge!({ "page" => params[:page] }) if params.has_key?(:page)
|
48
|
+
theguardian_api_params.merge!({ "page-size" => params[:per_page] }) if params.has_key?(:per_page)
|
49
|
+
|
50
|
+
theguardian_api_params.reject{ |_, value| value.nil? }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Theguardian
|
2
|
+
module Api
|
3
|
+
class Section < RecursiveOpenStruct
|
4
|
+
def self.process(params = {})
|
5
|
+
connection = Theguardian::Connection.new(resource: params[:id] || "sections")
|
6
|
+
response = connection.get
|
7
|
+
|
8
|
+
if params[:id]
|
9
|
+
self.new(response, recurse_over_arrays: true)
|
10
|
+
else
|
11
|
+
response["results"].map do |section_params|
|
12
|
+
self.new(section_params, recurse_over_arrays: true)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def items
|
18
|
+
self.results.map do |item_params|
|
19
|
+
Theguardian::Api::Item.new(item_params, recurse_over_arrays: true)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def fetch
|
24
|
+
self.class.process(id: self.id)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.process_params(params)
|
28
|
+
params.reject{ |_, value| value.nil? }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Theguardian
|
2
|
+
module Api
|
3
|
+
class Tag < RecursiveOpenStruct
|
4
|
+
def self.process(params = {})
|
5
|
+
connection = Theguardian::Connection.new(resource: "tags")
|
6
|
+
params = self.process_params(params)
|
7
|
+
response = connection.get(params)
|
8
|
+
|
9
|
+
response["results"].map do |tag_params|
|
10
|
+
self.new(tag_params, recurse_over_arrays: true)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def fetch
|
15
|
+
connection = Theguardian::Connection.new(resource: URI(self.apiUrl).path)
|
16
|
+
|
17
|
+
self.class.new(connection.get, recurse_over_arrays: true)
|
18
|
+
end
|
19
|
+
|
20
|
+
def items
|
21
|
+
self.results.map do |item_params|
|
22
|
+
Theguardian::Api::Item.new(item_params, recurse_over_arrays: true)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def section
|
27
|
+
Theguardian::Api::Section.process(id: self.tag.sectionId)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.process_params(params)
|
31
|
+
# Query string and tag type
|
32
|
+
theguardian_api_params = { q: params[:q], type: params[:type] }
|
33
|
+
|
34
|
+
# Pagination
|
35
|
+
theguardian_api_params.merge!({ "page" => params[:page] }) if params.has_key?(:page)
|
36
|
+
theguardian_api_params.merge!({ "page-size" => params[:per_page] }) if params.has_key?(:per_page)
|
37
|
+
|
38
|
+
theguardian_api_params.reject{ |_, value| value.nil? }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Theguardian
|
2
|
+
class Connection
|
3
|
+
attr_accessor :url, :resource
|
4
|
+
|
5
|
+
def initialize(params = {})
|
6
|
+
@resource = params[:resource]
|
7
|
+
@url = params[:url] || "http://content.guardianapis.com"
|
8
|
+
end
|
9
|
+
|
10
|
+
def get(params = {})
|
11
|
+
addressable = Addressable::URI.new
|
12
|
+
addressable.query_values = params.merge({ format: "JSON" })
|
13
|
+
p "#{@url}/#{resource}?#{addressable.query}", "==="
|
14
|
+
self.parse(RestClient.get("#{@url}/#{resource}?#{addressable.query}"))
|
15
|
+
end
|
16
|
+
|
17
|
+
def parse(response)
|
18
|
+
JSON.parse(response)["response"]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Theguardian
|
2
|
+
class ContentApi
|
3
|
+
def self.search(params = {})
|
4
|
+
Theguardian::Api::Search.process(params)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.search_tags(params = {})
|
8
|
+
Theguardian::Api::Tag.process(params)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.sections
|
12
|
+
Theguardian::Api::Section.process
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.item(params)
|
16
|
+
Theguardian::Api::Item.process(params)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/theguardian.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'theguardian/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "theguardian"
|
8
|
+
spec.version = Theguardian::VERSION
|
9
|
+
spec.authors = ["Mohamed Magdy"]
|
10
|
+
spec.email = ["eng.mohamagdy@gmail.com"]
|
11
|
+
spec.description = "The Guardian Ruby API"
|
12
|
+
spec.summary = "Get an access to the Guardian 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
|
+
# Dependencies
|
22
|
+
spec.add_dependency("rest-client")
|
23
|
+
spec.add_dependency("addressable")
|
24
|
+
spec.add_dependency("recursive-open-struct")
|
25
|
+
|
26
|
+
# Development dependencies
|
27
|
+
spec.add_development_dependency("bundler", "~> 1.3")
|
28
|
+
spec.add_development_dependency("rake")
|
29
|
+
spec.add_development_dependency("rspec")
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: theguardian
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mohamed Magdy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: addressable
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: recursive-open-struct
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
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: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
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: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: The Guardian Ruby API
|
98
|
+
email:
|
99
|
+
- eng.mohamagdy@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- .rspec
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/theguardian.rb
|
111
|
+
- lib/theguardian/api/item.rb
|
112
|
+
- lib/theguardian/api/search.rb
|
113
|
+
- lib/theguardian/api/section.rb
|
114
|
+
- lib/theguardian/api/tag.rb
|
115
|
+
- lib/theguardian/connection.rb
|
116
|
+
- lib/theguardian/content_api.rb
|
117
|
+
- lib/theguardian/version.rb
|
118
|
+
- spec/spec_helper.rb
|
119
|
+
- theguardian.gemspec
|
120
|
+
homepage: ''
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - '>='
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.0.3
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: Get an access to the Guardian API
|
144
|
+
test_files:
|
145
|
+
- spec/spec_helper.rb
|