parliament-ruby 0.10.2 → 1.0.0.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/parliament/network_error.rb +1 -1
- data/lib/parliament/request/base_request.rb +44 -43
- data/lib/parliament/request/url_request.rb +2 -2
- data/lib/parliament/version.rb +1 -1
- data/parliament-ruby.gemspec +2 -0
- metadata +19 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 291f639647bb81fcc5dd82a384cd0ecff70db477
|
4
|
+
data.tar.gz: 26f6551f09bcd80f4ff351ba55189bb77bc71580
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1dd49cab50167b71972428f3d53f61f22f72b8ff8d7dd69af0607d616d9280e19fee121864697ed9e423a0981e72b90ca8eae03b85146f8c6885f2a7b7d865a1
|
7
|
+
data.tar.gz: 321ece67fd29fea8af22fcd3197a12218edb9443a3760e4525ed84a158fe3ba787a9ca38c8c5a4abd5501d01f867c2e73b12ee8b1c03e09f11ce13f82e2575a3
|
data/README.md
CHANGED
@@ -21,7 +21,7 @@
|
|
21
21
|
- [`#filter`](#filter)
|
22
22
|
- [`#sort_by`](#sort_by)
|
23
23
|
- [`#reverse_sort_by`](#reverse_sort_by)
|
24
|
-
- [`
|
24
|
+
- [`Parliament::Utils`](#parliamentutils)
|
25
25
|
- [Methods](#methods-1)
|
26
26
|
- [`Parliament::Utils.sort_by`](#parliamentutilssort_by)
|
27
27
|
- [`Parliament::Utils.reverse_sort_by`](#parliamentutilsreverse_sort_by)
|
@@ -137,7 +137,7 @@ response.each do |node|
|
|
137
137
|
#
|
138
138
|
# You would be able to access the `name` attribute like so:
|
139
139
|
puts node.name #=> 'Matthew Rayner'
|
140
|
-
|
140
|
+
|
141
141
|
# If your n-triple file contains a triple who's object is a URI, and that URI is defined within your file, a link will
|
142
142
|
# be created, allowing you to 'connect' the two objects. For example, with the following triples:
|
143
143
|
# <http://id.ukpds.org/1234> <http://id.ukpds.org/schema/name> 'Matthew Rayner' .
|
@@ -148,7 +148,7 @@ response.each do |node|
|
|
148
148
|
puts node.graph_id #=> '12345'
|
149
149
|
puts node.name #=> 'Matthew Rayner'
|
150
150
|
puts node.partyMembership #=> [#<Grom::Node @startDate=...>]
|
151
|
-
|
151
|
+
|
152
152
|
puts node.partyMembership.first.startDate #=> "1992-04-09"
|
153
153
|
end
|
154
154
|
```
|
@@ -214,7 +214,7 @@ sorted_response.each { |node| puts "#{node.graph_id} - #{node.respond_to?(:start
|
|
214
214
|
# http://id.ukpds.org/3141 - undefined
|
215
215
|
```
|
216
216
|
|
217
|
-
### `
|
217
|
+
### `Parliament::Utils`
|
218
218
|
Included with [parliament-ruby][parliament-ruby] is `Parliament::Utils`. This module includes helper methods commonly used throughout parliament.uk.
|
219
219
|
|
220
220
|
#### Methods
|
@@ -7,7 +7,7 @@ module Parliament
|
|
7
7
|
# @since 0.6.0
|
8
8
|
class NetworkError < StandardError
|
9
9
|
def initialize(url, response)
|
10
|
-
super("#{response.code} HTTP status code received from: #{url} - #{response.
|
10
|
+
super("#{response.code} HTTP status code received from: #{url} - #{response.status_message}")
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'typhoeus'
|
2
|
+
|
1
3
|
module Parliament
|
2
4
|
module Request
|
3
5
|
# Base request object, allowing the user to make a request to an API.
|
@@ -55,10 +57,10 @@ module Parliament
|
|
55
57
|
# @param [Parliament::Builder] builder the builder to use in order to build a response.
|
56
58
|
# @params [Module] decorators the decorator module to use in order to provide possible alias methods for any objects created by the builder.
|
57
59
|
def initialize(base_url: nil, headers: nil, builder: nil, decorators: nil)
|
58
|
-
@base_url
|
59
|
-
@headers
|
60
|
-
@builder
|
61
|
-
@decorators
|
60
|
+
@base_url = base_url || self.class.base_url
|
61
|
+
@headers = headers || self.class.headers || {}
|
62
|
+
@builder = builder || Parliament::Builder::BaseResponseBuilder
|
63
|
+
@decorators = decorators
|
62
64
|
@query_params = {}
|
63
65
|
end
|
64
66
|
|
@@ -86,20 +88,22 @@ module Parliament
|
|
86
88
|
#
|
87
89
|
# @return [Parliament::Response::BaseResponse] a Parliament::Response::BaseResponse object containing all of the data returned from the URL.
|
88
90
|
def get(params: nil)
|
89
|
-
|
91
|
+
Typhoeus::Config.user_agent = 'Ruby'
|
90
92
|
|
91
|
-
|
92
|
-
endpoint_uri.query = URI.encode_www_form(@query_params.to_a) unless @query_params.empty?
|
93
|
+
@query_params = @query_params.merge(params) unless params.nil?
|
93
94
|
|
94
|
-
|
95
|
-
|
95
|
+
endpoint_uri = URI.parse(query_url)
|
96
|
+
endpoint_params = URI.encode_www_form(@query_params.to_a) unless @query_params.empty?
|
96
97
|
|
97
|
-
|
98
|
-
|
99
|
-
|
98
|
+
typhoeus_request = Typhoeus::Request.new(
|
99
|
+
endpoint_uri,
|
100
|
+
method: :get,
|
101
|
+
params: endpoint_params,
|
102
|
+
headers: headers,
|
103
|
+
accept_encoding: 'gzip'
|
104
|
+
)
|
100
105
|
|
101
|
-
|
102
|
-
end
|
106
|
+
net_response = typhoeus_request.run
|
103
107
|
|
104
108
|
handle_errors(net_response)
|
105
109
|
|
@@ -132,25 +136,24 @@ module Parliament
|
|
132
136
|
#
|
133
137
|
# @return [Parliament::Response::BaseResponse] a Parliament::Response::BaseResponse object containing all of the data returned from the URL.
|
134
138
|
def post(params: nil, body: nil, timeout: 60)
|
135
|
-
|
139
|
+
Typhoeus::Config.user_agent = 'Ruby'
|
136
140
|
|
137
|
-
|
138
|
-
endpoint_uri.query = URI.encode_www_form(@query_params.to_a) unless @query_params.empty?
|
139
|
-
|
140
|
-
http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
|
141
|
-
http.use_ssl = true if endpoint_uri.scheme == 'https'
|
142
|
-
http.read_timeout = timeout
|
141
|
+
@query_params = @query_params.merge(params) unless params.nil?
|
143
142
|
|
144
|
-
|
145
|
-
|
146
|
-
|
143
|
+
endpoint_uri = URI.parse(query_url)
|
144
|
+
endpoint_params = URI.encode_www_form(@query_params.to_a) unless @query_params.empty?
|
145
|
+
|
146
|
+
typhoeus_request = Typhoeus::Request.new(
|
147
|
+
endpoint_uri,
|
148
|
+
method: :post,
|
149
|
+
params: endpoint_params,
|
150
|
+
headers: headers.merge({ 'Content-Type' => 'application/json' }),
|
151
|
+
accept_encoding: 'gzip',
|
152
|
+
timeout: timeout,
|
153
|
+
body: body
|
147
154
|
)
|
148
155
|
|
149
|
-
|
150
|
-
|
151
|
-
request.body = body unless body.nil?
|
152
|
-
|
153
|
-
net_response = http.request(request)
|
156
|
+
net_response = typhoeus_request.run
|
154
157
|
|
155
158
|
handle_errors(net_response)
|
156
159
|
|
@@ -174,28 +177,26 @@ module Parliament
|
|
174
177
|
end
|
175
178
|
|
176
179
|
def default_headers
|
177
|
-
{ 'Accept' => 'application/n-triples' }
|
180
|
+
{ 'Accept' => ['*/*', 'application/n-triples'] }
|
178
181
|
end
|
179
182
|
|
180
|
-
def
|
181
|
-
|
182
|
-
headers.each do |key, value|
|
183
|
-
request.add_field(key, value)
|
184
|
-
end
|
183
|
+
def headers
|
184
|
+
default_headers.merge(@headers)
|
185
185
|
end
|
186
186
|
|
187
187
|
def handle_errors(net_response)
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
188
|
+
exception_class = if net_response.success? # 2xx Status
|
189
|
+
Parliament::NoContentResponseError if net_response.headers&.[]('Content-Length') == '0' ||
|
190
|
+
(net_response.headers&.[]('Content-Length').nil? && net_response.body.empty?)
|
191
|
+
elsif /\A4\w{2}/.match(net_response.code.to_s) # 4xx Status
|
192
|
+
Parliament::ClientError
|
193
|
+
elsif /\A5\w{2}/.match(net_response.code.to_s) # 5xx Status
|
194
|
+
Parliament::ServerError
|
195
|
+
end
|
196
196
|
|
197
197
|
raise exception_class.new(query_url, net_response) if exception_class
|
198
198
|
end
|
199
199
|
end
|
200
200
|
end
|
201
201
|
end
|
202
|
+
|
@@ -13,7 +13,7 @@ module Parliament
|
|
13
13
|
#
|
14
14
|
# @param [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash).
|
15
15
|
# @param [Hash] headers the headers being sent in the request.
|
16
|
-
# @param [Parliament::Builder] builder the builder to use in order to build a response.
|
16
|
+
# @param [Parliament::Builder] builder the builder to use in order to build a response.{{}}
|
17
17
|
# @param [Module] decorators the decorator module to use in order to provide possible alias methods for any objects created by the builder.
|
18
18
|
# @example Passing headers
|
19
19
|
#
|
@@ -53,6 +53,7 @@ module Parliament
|
|
53
53
|
# @return [Parliament::Request::UrlRequest] self (this is to allow method chaining).
|
54
54
|
def method_missing(method, *params, &block)
|
55
55
|
@endpoint_parts << method.to_s
|
56
|
+
|
56
57
|
@endpoint_parts << params
|
57
58
|
@endpoint_parts = @endpoint_parts.flatten!
|
58
59
|
|
@@ -72,7 +73,6 @@ module Parliament
|
|
72
73
|
uri_string = [@base_url, @endpoint_parts].join('/').gsub(' ', '%20')
|
73
74
|
|
74
75
|
uri = URI.parse(uri_string)
|
75
|
-
uri.query = URI.encode_www_form(@query_params) unless @query_params.empty?
|
76
76
|
|
77
77
|
uri.to_s
|
78
78
|
end
|
data/lib/parliament/version.rb
CHANGED
data/parliament-ruby.gemspec
CHANGED
@@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
21
|
spec.require_paths = ['lib']
|
22
22
|
|
23
|
+
spec.add_dependency 'typhoeus', '~> 1.3'
|
24
|
+
|
23
25
|
spec.add_development_dependency 'bundler', '~> 1.13'
|
24
26
|
spec.add_development_dependency 'rake', '~> 10.0'
|
25
27
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parliament-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Rayner
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: typhoeus
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -188,12 +202,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
188
202
|
version: '0'
|
189
203
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
190
204
|
requirements:
|
191
|
-
- - "
|
205
|
+
- - ">"
|
192
206
|
- !ruby/object:Gem::Version
|
193
|
-
version:
|
207
|
+
version: 1.3.1
|
194
208
|
requirements: []
|
195
209
|
rubyforge_project:
|
196
|
-
rubygems_version: 2.6.
|
210
|
+
rubygems_version: 2.6.11
|
197
211
|
signing_key:
|
198
212
|
specification_version: 4
|
199
213
|
summary: Internal parliamentary API wrapper
|