parliament-ruby 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d3d807385e5cf4830f2c8b745caf21ca30c66b6
4
- data.tar.gz: 3dd2e1564b9386b40a7cd283700c4d3033a22db4
3
+ metadata.gz: 798b518850479eb9ebbb0ca1b660bfa7bb11c473
4
+ data.tar.gz: 6288812a80c05e34237acd34916738f5e3cc5cd6
5
5
  SHA512:
6
- metadata.gz: e5bafa520f5678a106a650866495148337fc8932ef10aa789afa22c84d9cc2de7cbc8930f528acdfe7b21aa33af957ac322dba89101f54cdde2b1b79a522d301
7
- data.tar.gz: 3fab5b9bcc3f6267bd2b61f0c76d8bfba91d3176925c62efc34689f0ca30c48654b898757c8903e25bc561b6f450eea60d018f41746bb74348b3ef42f4500eae
6
+ metadata.gz: 8f3e2c54ee9792720f702a81cab0e48c33f26b8be1186927e35806d210099ead3a8a0d0f95ce507c36130dd371e69ab5eaba4a98e66dbe0eb0f5e743570595dd
7
+ data.tar.gz: e36a0edf790c51c58e0c767bae8323948360fafc4d4cfe223af8d38de1b27f9ecdab041f92c4d16c58bc7aa11b7a6f14d9ad1fe8a60e00bc7a2f8250aeb47ab4
@@ -1 +1 @@
1
- 2.6.0
1
+ 2.6.1
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Parliament Data API Wrapper (Ruby)
2
- [parliament-ruby][parliament-ruby] is a gem created by the [Parliamentary Digital Service][pds] to allow easy communication with the internal parliament data api.
2
+ [parliament-ruby][parliament-ruby] is a gem created by the [Parliamentary Digital Service][pds] to allow easy communication with the internal parliament data API.
3
3
 
4
4
  [![Gem][shield-gem]][info-gem] [![Build Status][shield-travis]][info-travis] [![Test Coverage][shield-coveralls]][info-coveralls] [![License][shield-license]][info-license]
5
5
 
@@ -40,14 +40,13 @@
40
40
 
41
41
 
42
42
  ## Installation
43
- This gem is currently not available on RubyGems. To use it in an application, install it directly from GitHub via your Gemfile
44
43
  ```bash
45
- gem 'parliament', git: 'https://github.com/ukparliament/parliament-ruby.git', branch: 'master'
44
+ gem 'parliament-ruby'
46
45
  ```
47
46
 
48
47
 
49
48
  ## Usage
50
- This gem's main function is fetching an n-triple file from a remote server and converting it into linked ruby objects.
49
+ This gem's main function is fetching an n-triples from a remote server and converting it into linked ruby objects.
51
50
 
52
51
  > **Note:** Comprehensive class documentation can be found on [rubydocs][rubydocs].
53
52
 
@@ -111,170 +110,6 @@ Parliament::Request.new.headers #=> { 'Accept' => 'Test' }
111
110
  # You can still override the headers on an instance by instance basis
112
111
  Parliament::Request.new(headers: { 'Accept' => 'Test2' }).headers #=> { 'Accept' => 'Test2' }
113
112
  ```
114
- ### Methods
115
- [parliament-ruby][parliament-ruby] comes with the following common methods:
116
-
117
- | Method | Description |
118
- |----------------------------------------|-------------|
119
- | [`#get`](#get) | **Make a GET request** - Make a HTTP GET request to the endpoint we have built, and create Ruby objects. |
120
- | [`#filter`](#filter) | **Filter the response** - After making a GET request, filter the objects returned by type attribute. |
121
- | [`#sort_by`](#sort_by) | **Sort the response (ASC)** - After making a GET request, sort the result in ascending order. |
122
- | [`#reverse_sort_by`](#reverse_sort_by) | **Sort the response (DESC)** - After making a GET request, sort the result in descending order. |
123
-
124
- > **Note:** Comprehensive class documentation can be found on [rubydocs][rubydocs].
125
-
126
- #### `#get`
127
- Once you've built your endpoint (`parliament.people.current`), we use the `#get` method to tell us you're ready to get the data.
128
-
129
- ```ruby
130
- # Target endpoint: 'http://test.com/people/123/letters/456'
131
- response = parliament.people('123').letters('456').get #=> #<Parliament::Response [...]>
132
-
133
- response.each do |node|
134
- # If your n-triple file contains a literal object it is stored into an instance variable accessible via the predicate
135
- # name. For example, with the following triple:
136
- # <http://id.ukpds.org/1234> <http://id.ukpds.org/schema/name> 'Matthew Rayner' .
137
- #
138
- # You would be able to access the `name` attribute like so:
139
- puts node.name #=> 'Matthew Rayner'
140
-
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
- # be created, allowing you to 'connect' the two objects. For example, with the following triples:
143
- # <http://id.ukpds.org/1234> <http://id.ukpds.org/schema/name> 'Matthew Rayner' .
144
- # <http://id.ukpds.org/1234> <http://id.ukpds.org/schema/partyMembership> <http://id.ukpds.org/5678> .
145
- # <http://id.ukpds.org/5678> <http://id.ukpds.org/schema/startDate> "1992-04-09"^^<http://www.w3.org/2001/XMLSchema#date> .
146
- #
147
- # You would be able to access the start date attribute on the linked object like so:
148
- puts node.graph_id #=> '12345'
149
- puts node.name #=> 'Matthew Rayner'
150
- puts node.partyMembership #=> [#<Grom::Node @startDate=...>]
151
-
152
- puts node.partyMembership.first.startDate #=> "1992-04-09"
153
- end
154
- ```
155
-
156
- `#get` returns a `Parliament::Response` object which contains all of the nodes from our n-triple response.
157
-
158
- #### `#filter`
159
- If your n-triple file contains a number of different objects you can filter based on type attribute.
160
-
161
- ```ruby
162
- # Target endpoint: 'http://test.com/people/members/current'
163
- response = parliament.people.members.current.get #=> #<Parliament::Response [...]>
164
-
165
- # Given the below set of triples, you will be able to filter on 'type' attribute.
166
- # <http://id.ukpds.org/cea89432-e046-4013-a9ba-e93d0468d186> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://id.ukpds.org/schema/Person> .
167
- # <http://id.ukpds.org/4ef6c7b7-a5c8-4dde-a5a5-29c9f80d8a27> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://id.ukpds.org/schema/Constituency> .
168
- # <http://id.ukpds.org/80234c90-f86a-4942-b6ae-d1e57a0b378d> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://id.ukpds.org/schema/Party> .
169
- # <http://id.ukpds.org/HouseOfCommons> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://id.ukpds.org/schema/House> .
170
- #
171
- # The filter method returns an array of filtered responses. Each response holding just items with the given type attribute.
172
- filtered_responses = response.filter('http://id.ukpds.org/schema/Person', 'http://id.ukpds.org/schema/Party', 'http://id.ukpds.org/schema/Constituency')
173
- filtered_responses #=> [<#Parliament::Response [...]>, <#Parliament::Response []>, <#Parliament::Response [...]>]
174
- ```
175
-
176
- #### `#sort_by`
177
- Once you have a `Parliament::Response` object, you can perform an ascending sort on the results by using the `#sort_by` method.
178
-
179
- > **`#sort_by`** is an ascending order sort i.e. (a..z, 0..9)
180
-
181
- ```ruby
182
- # Target endpoint: 'http://test.com/people/members/current'
183
- response = parliament.people.members.current.get #=> #<Parliament::Response [...]>
184
-
185
- # Given the below set of triples, you can sort ascending like so:
186
- # <http://id.ukpds.org/1234> <http://id.ukpds.org/schema/startDate> "1997-01-01"^^<http://www.w3.org/2001/XMLSchema#date> .
187
- # <http://id.ukpds.org/5678> <http://id.ukpds.org/schema/startDate> "1991-03-15"^^<http://www.w3.org/2001/XMLSchema#date> .
188
- # <http://id.ukpds.org/9101> <http://id.ukpds.org/schema/startDate> "2011-09-04"^^<http://www.w3.org/2001/XMLSchema#date> .
189
- # <http://id.ukpds.org/1121> <http://id.ukpds.org/schema/startDate> "1981-07-31"^^<http://www.w3.org/2001/XMLSchema#date> .
190
- # <http://id.ukpds.org/3141> <http://id.ukpds.org/schema/endDate> "1997-07-31"^^<http://www.w3.org/2001/XMLSchema#date> .
191
- #
192
- # The sort_by method returns a sorted array of Grom::Nodes, sorted by one, or many, symbols passed to it.
193
- sorted_response = response.sort_by(:startDate)
194
-
195
- # Output each of the graph_id and startDate values
196
- sorted_response.each { |node| puts "#{node.graph_id} - #{node.respond_to?(:startDate) ? node.startDate : 'undefined'}" }
197
- # http://id.ukpds.org/3141 - undefined
198
- # http://id.ukpds.org/1121 - 1981-07-31
199
- # http://id.ukpds.org/5678 - 1991-03-15
200
- # http://id.ukpds.org/1234 - 1997-01-01
201
- # http://id.ukpds.org/9101 - 2011-09-04
202
- ```
203
-
204
- > **NOTE:** `#sort_by` places all `nil` responses at the start of the sort. For a more custom sort, take a look at `Parliament::Utils.sort_by`.
205
-
206
- #### `#reverse_sort_by`
207
- `#reverse_sort_by` simple implements `#sort_by` and calls `#reverse!` on the resulting array. The above example would become:
208
-
209
- ```ruby
210
- # http://id.ukpds.org/9101 - 2011-09-04
211
- # http://id.ukpds.org/1234 - 1997-01-01
212
- # http://id.ukpds.org/5678 - 1991-03-15
213
- # http://id.ukpds.org/1121 - 1981-07-31
214
- # http://id.ukpds.org/3141 - undefined
215
- ```
216
-
217
- ### `Parliament::Utils`
218
- Included with [parliament-ruby][parliament-ruby] is `Parliament::Utils`. This module includes helper methods commonly used throughout parliament.uk.
219
-
220
- #### Methods
221
-
222
- | Method | Description |
223
- |-------------------------------------------------------|-------------|
224
- | [`#sort_by`](#parliamentutilssort_by) | **Sort an enumerable thing (ASC)** - An implementation of ruby's `#sort_by` method that allows nil sorts. |
225
- | [`#reverse_sort_by`](#parliamentutilsreverse_sort_by) | **Sort an enumerable thing (DESC)** - Reverse the result of `Parliament::Utils.reverse_sort_by` |
226
-
227
-
228
- ##### `Parliament::Utils.sort_by`
229
- One of the common use cases we have is sorting objects by a date (e.g. seat incumbency end date) where an object without an end date is considered 'current' and should be sorted to the top (or bottom) of a list.
230
-
231
- Because we working with graph databases, a node without an endDate simply has no method when converted to an object with [GROM][grom].
232
-
233
- The `Parliament::Utils.sort_by` method takes a hash of options, detailed below:
234
-
235
- ```ruby
236
- response = parliament.people('123').get.filter('http://id.ukpds.org/schema/Person')
237
-
238
- objects = response.first.incumbencies
239
-
240
- options = {
241
- list: objects, # An enumerable of objects (most commonly an Array)
242
- parameters: [:endDate], # An array of actions which we will sort by
243
- prepend_rejected: false # {optional default=true} Should any objects that are 'rejected' be prepended to the sorted list, or appended. i.e. where to put objects that don't respond to parameters provided
244
- }
245
-
246
- sorted_list = Parliament::Util.sort_by(options)
247
-
248
- sorted_list.each { |incumbency| puts incumbency.respond_to?(:endDate) ? incumbency.endDate : 'Current' }
249
- # http://id.ukpds.org/1121 - 1981-07-31
250
- # http://id.ukpds.org/5678 - 1991-03-15
251
- # http://id.ukpds.org/1234 - 1997-01-01
252
- # http://id.ukpds.org/9101 - 2011-09-04
253
- # http://id.ukpds.org/3141 - Current
254
- ```
255
-
256
- ##### `Parliament::Utils.reverse_sort_by`
257
- This method, under the hood, calls `Parliament::Utils.sort_by` and runs `#reverse!` on the result.
258
-
259
- Following the above example, and changing:
260
- ```ruby
261
- sorted_list = Parliament::Util.sort_by(options)
262
- ```
263
-
264
- to:
265
- ```ruby
266
- sorted_list = Parliament::Util.reverse_sort_by(options)
267
- ```
268
-
269
- should result in:
270
- ```ruby
271
- # http://id.ukpds.org/3141 - Current
272
- # http://id.ukpds.org/9101 - 2011-09-04
273
- # http://id.ukpds.org/1234 - 1997-01-01
274
- # http://id.ukpds.org/5678 - 1991-03-15
275
- # http://id.ukpds.org/1121 - 1981-07-31
276
- ```
277
-
278
113
 
279
114
  ## Getting Started with Development
280
115
  To clone the repository and set up the dependencies, run the following:
@@ -3,7 +3,7 @@ module Parliament
3
3
  # Base response builder, allowing the user to return the body of an HTTPResponse.
4
4
  # @since 0.7.5
5
5
  class BaseResponseBuilder
6
- # Creates a new BaseReponseBuilder.
6
+ # Creates a new BaseResponseBuilder.
7
7
  # @param [HTTPResponse] response an HTTP response.
8
8
  # @param [Module] decorators a namespace which contains modules used to decorate the objects we receive. It is not used directly by the BaseResponseBuilder, but is there for API completeness.
9
9
  def initialize(response:, decorators: nil)
@@ -6,8 +6,9 @@ module Parliament
6
6
  #
7
7
  # @since 0.7.5
8
8
  #
9
- # @attr_reader [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash).
9
+ # @attr_reader [String] base_url the base url of our api. (eg: http://example.com - without the trailing slash).
10
10
  # @attr_reader [Hash] headers the headers being sent in the request.
11
+ # @attr_reader [Hash] query_params any query parameters to be sent in the request.
11
12
  class BaseRequest
12
13
  TIMEOUT = 40.freeze
13
14
  CONNECTTIMEOUT = 5.freeze
@@ -17,7 +18,7 @@ module Parliament
17
18
  #
18
19
  # An interesting note for #initialize is that setting base_url on the class, or using the environment variable
19
20
  # PARLIAMENT_BASE_URL means you don't need to pass in a base_url. You can pass one anyway to override the
20
- # environment variable or class parameter. Similarly, headers can be set by either settings the headers on the class, or passing headers in.
21
+ # environment variable or class parameter. Similarly, headers can be set by either setting the headers on the class, or passing headers in.
21
22
  #
22
23
  # @example Setting the base_url on the class
23
24
  # Parliament::Request::BaseRequest.base_url = 'http://example.com'
@@ -58,7 +59,7 @@ module Parliament
58
59
  # @param [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash).
59
60
  # @param [Hash] headers the headers being sent in the request.
60
61
  # @param [Parliament::Builder] builder the builder to use in order to build a response.
61
- # @params [Module] decorators the decorator module to use in order to provide possible alias methods for any objects created by the builder.
62
+ # @param [Module] decorators the decorator modules to use in order to provide possible alias methods for any objects created by the builder.
62
63
  def initialize(base_url: nil, headers: nil, builder: nil, decorators: nil)
63
64
  @base_url = base_url || self.class.base_url
64
65
  @headers = headers || self.class.headers || {}
@@ -70,7 +71,7 @@ module Parliament
70
71
  # Makes an HTTP GET request and process results into a response.
71
72
  #
72
73
  # @example HTTP GET request
73
- # request = Parliament::Request::BaseRequest.new(base_url: 'http://example.com/people/123'
74
+ # request = Parliament::Request::BaseRequest.new(base_url: 'http://example.com/people/123')
74
75
  #
75
76
  # # url: http://example.com/people/123
76
77
  #
@@ -88,6 +89,8 @@ module Parliament
88
89
  # @raise [Parliament::NoContentResponseError] when the response body is empty.
89
90
  #
90
91
  # @param [Hash] params (optional) additional URI encoded form values to be added to the URI.
92
+ # @param [Integer] timeout (optional) time limit for the entire request in seconds.
93
+ # @param [Integer] connecttimeout (optional) time limit for just the connection in seconds.
91
94
  #
92
95
  # @return [Parliament::Response::BaseResponse] a Parliament::Response::BaseResponse object containing all of the data returned from the URL.
93
96
  def get(params: nil, timeout: TIMEOUT, connecttimeout: CONNECTTIMEOUT)
@@ -134,7 +137,8 @@ module Parliament
134
137
  #
135
138
  # @param [Hash] params (optional) additional URI encoded form values to be added to the URI.
136
139
  # @param [String] body (optional) body of the post request.
137
- # @param [Integer] timeout (optional) a Net::HTTP.read_timeout value passed suring the post.
140
+ # @param [Integer] timeout (optional) time limit for the entire request in seconds.
141
+ # @param [Integer] connecttimeout (optional) time limit for just the connection in seconds.
138
142
  #
139
143
  # @return [Parliament::Response::BaseResponse] a Parliament::Response::BaseResponse object containing all of the data returned from the URL.
140
144
  def post(params: nil, body: nil, timeout: TIMEOUT, connecttimeout: CONNECTTIMEOUT)
@@ -162,7 +166,7 @@ module Parliament
162
166
 
163
167
  private
164
168
 
165
- # @attr [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash).
169
+ # @attr [String] base_url the base url of our api. (eg: http://example.com - without the trailing slash).
166
170
  # @attr [Hash] headers the headers being sent in the request.
167
171
  class << self
168
172
  attr_accessor :base_url, :headers
@@ -187,7 +191,7 @@ module Parliament
187
191
  def handle_errors(response)
188
192
  exception_class = if response.success? # 2xx Status
189
193
  Parliament::NoContentResponseError if response.headers&.[]('Content-Length') == '0' ||
190
- (response.headers&.[]('Content-Length').nil? && response.body.empty?)
194
+ (response.headers&.[]('Content-Length').nil? && response.body.empty?)
191
195
  elsif /\A4\w{2}/.match(response.code.to_s) # 4xx Status
192
196
  Parliament::ClientError
193
197
  elsif /\A5\w{2}/.match(response.code.to_s) # 5xx Status
@@ -205,7 +209,7 @@ module Parliament
205
209
  if endpoint.query
206
210
  # Returns [ ["key", "value"], ["key", "value"] ]
207
211
  key_value_array = URI.decode_www_form(endpoint.query)
208
- key_value_array.map! { |key_value_pair| [ key_value_pair[0].to_sym, key_value_pair[1] ] }
212
+ key_value_array.map! { |key_value_pair| [key_value_pair[0].to_sym, key_value_pair[1]] }
209
213
  temp_params = key_value_array.to_h
210
214
  end
211
215
 
@@ -219,4 +223,3 @@ module Parliament
219
223
  end
220
224
  end
221
225
  end
222
-
@@ -4,8 +4,6 @@ module Parliament
4
4
  #
5
5
  # @since 0.7.5
6
6
  #
7
- # @attr_reader [String] base_url the endpoint for our API which we will build our requests on. (expected: http://example.com - without the trailing slash).
8
- # @attr_reader [Hash] headers the headers being sent in the request.
9
7
  class UrlRequest < Parliament::Request::BaseRequest
10
8
  # Creates a new instance of Parliament::Request::UrlRequest.
11
9
  #
@@ -26,7 +24,7 @@ module Parliament
26
24
  super
27
25
  end
28
26
 
29
- # Overrides ruby's method_missing to allow creation of URLs through method calls.
27
+ # Overrides Ruby's method_missing to allow creation of URLs through method calls.
30
28
  #
31
29
  # @example Adding a simple URL part
32
30
  # request = Parliament::Request::UrlRequest.new(base_url: 'http://example.com')
@@ -48,7 +46,7 @@ module Parliament
48
46
  #
49
47
  # @param [Symbol] method the 'method' (url part) we are processing.
50
48
  # @param [Array<Object>] params parameters passed to the specified method (url part).
51
- # @param [Block] block additional block (kept for compatibility with method_missing API).
49
+ # @param [Block] block additional block (kept for compatibility with method_missing interface).
52
50
  #
53
51
  # @return [Parliament::Request::UrlRequest] self (this is to allow method chaining).
54
52
  def method_missing(method, *params, &block)
@@ -1,3 +1,3 @@
1
1
  module Parliament
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.0.2'.freeze
3
3
  end
@@ -6,8 +6,8 @@ require 'parliament/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'parliament-ruby'
8
8
  spec.version = Parliament::VERSION
9
- spec.authors = ['Matt Rayner']
10
- spec.email = ['mattrayner1@gmail.com']
9
+ spec.authors = ['Matt Rayner', 'Rebecca Appleyard', 'Giuseppe De Santis']
10
+ spec.email = ['mattrayner1@gmail.com', 'rklappleyard@gmail.com']
11
11
  spec.summary = %q{Internal parliamentary API wrapper}
12
12
  spec.description = %q{Internal parliamentary data API wrapper for ruby}
13
13
  spec.homepage = 'http://github.com/ukparliament/parliament_ruby'
metadata CHANGED
@@ -1,14 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parliament-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Rayner
8
+ - Rebecca Appleyard
9
+ - Giuseppe De Santis
8
10
  autorequire:
9
11
  bindir: exe
10
12
  cert_chain: []
11
- date: 2019-01-23 00:00:00.000000000 Z
13
+ date: 2019-02-13 00:00:00.000000000 Z
12
14
  dependencies:
13
15
  - !ruby/object:Gem::Dependency
14
16
  name: typhoeus
@@ -153,6 +155,7 @@ dependencies:
153
155
  description: Internal parliamentary data API wrapper for ruby
154
156
  email:
155
157
  - mattrayner1@gmail.com
158
+ - rklappleyard@gmail.com
156
159
  executables: []
157
160
  extensions: []
158
161
  extra_rdoc_files: []