parliament-opensearch 0.1.0 → 0.1.1

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: d49d1bbb5b87d8f73c34291fdfd5e3c90911a04e
4
- data.tar.gz: 447fd872f92e50945605ee74a0cb258086a75b23
3
+ metadata.gz: 51c60a8ca7fca47e247c68f31f0bda226bc9d3e4
4
+ data.tar.gz: 640adba09ea47477410b8863ec803b264fd1fb2c
5
5
  SHA512:
6
- metadata.gz: ebd0b3516e486030af48bf02f8fec009825ee295bb6e39768b079f2d64652f4dd4f8e2c5106e34b1fe4d479a32499e5ebdc231deb2e5844172085d9090baa7d6
7
- data.tar.gz: 1e9b8ba24c639d2d682ff66bdbffce1698adbdd2c81920b1613ef7b516dc77d67504720ff62e7c4e21f34f0b614c3dd1bd1c46a106e05b3155c36405dafe269e
6
+ metadata.gz: 0d1fc9b1c473a8d0e24e1be7c76b36fb950a36d5d98d76deef044723a8e1c4bebebcb0c68e44d63977e986f42b393889e5281f62df11b50b9d80d5bc22c85aa5
7
+ data.tar.gz: df0940aff4ecdb2561df5881f94b390165bb9774fb608d88c3f727903186770032e3c760c56d58f752e9657c6b94449a3b0ec09495cb9a4d5b328a23e103c4b5
@@ -0,0 +1,21 @@
1
+ require 'feedjira'
2
+
3
+ module Parliament
4
+ module Builder
5
+ # OpenSearch response builder using Feedjira to parse the response.
6
+ #
7
+ # @since 0.1.0
8
+ class OpenSearchResponseBuilder < Parliament::Builder::BaseResponseBuilder
9
+ OPEN_SEARCH_ELEMENTS = %w(totalResults Query startIndex itemsPerPage).freeze
10
+
11
+ # Builds a Feedjira::Feed response. It adds the extra OpenSearch feed elements, then parses the HTTP Response.
12
+ def build
13
+ OPEN_SEARCH_ELEMENTS.each do |element|
14
+ Feedjira::Feed.add_common_feed_element(element)
15
+ end
16
+
17
+ Feedjira::Feed.parse(@response.body)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  module Parliament
2
2
  module OpenSearch
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
@@ -1,8 +1,8 @@
1
1
  require 'parliament'
2
2
 
3
3
  require 'parliament/open_search/version'
4
- require 'parliament/open_search/builder'
5
- require 'parliament/open_search/request'
4
+ require 'parliament/request/open_search_request'
5
+ require 'parliament/builder/open_search_response_builder'
6
6
 
7
7
  # Namespace for classes and modules that handle connections to, and processing of data from OpenSearch APIs.
8
8
  # @since 0.1.0
@@ -0,0 +1,93 @@
1
+ module Parliament
2
+ module Request
3
+ # API request object, allowing the user to build a request to an OpenSearch API and build a response.
4
+ #
5
+ # @since 0.1.0
6
+ class OpenSearchRequest < Parliament::Request::BaseRequest
7
+ OPEN_SEARCH_PARAMETERS = {
8
+ count: 10,
9
+ start_index: 1,
10
+ start_page: 1,
11
+ language: '*',
12
+ output_encoding: 'UTF-8',
13
+ input_encoding: 'UTF-8'
14
+ }.freeze
15
+
16
+ # Creates a new instance of Parliament::OpenSearch::Request.
17
+ #
18
+ # An interesting note for #initialize is that setting base_url on the class, or using the environment variable
19
+ # OPENSEARCH_DESCRIPTION_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
+ #
22
+ # @see Parliament::BaseRequest#initialize
23
+ # @param [String] base_url the base url for the OpenSearch API description. (expected: http://example.com - without the trailing slash).
24
+ # @param [Hash] headers the headers being sent in the request.
25
+ # @param [Parliament::OpenSearch::Builder] builder the builder required to create the response.
26
+ def initialize(base_url: nil, headers: nil, builder: nil)
27
+ @base_url = Parliament::Request::OpenSearchRequest.get_description(base_url) || self.class.base_url || ENV['OPENSEARCH_DESCRIPTION_URL']
28
+ @open_search_parameters = self.class.open_search_parameters
29
+
30
+ super(base_url: @base_url, headers: headers, builder: builder)
31
+ end
32
+
33
+ # Sets up the query url using the base_url and parameters, then make an HTTP GET request and process results.
34
+ #
35
+ # @see Parliament::BaseRequest#get
36
+ # @params [Hash] search_params the search parameters to be passed to the OpenSearch API. This is the search term and/or any of the keys from OPEN_SEARCH_PARAMETERS, depending on the parameters allowed in the API.
37
+ # @params [Hash] params any extra parameters.
38
+ def get(search_params, params: nil)
39
+ setup_query_url(search_params)
40
+
41
+ super(params: params)
42
+ end
43
+
44
+ private
45
+
46
+ # @attr [String] base_url the base url for the OpenSearch API description. (expected: http://example.com - without the trailing slash).
47
+ # @attr [Hash] open_search_parameters the possible parameters to use in the query url.
48
+ class << self
49
+ attr_reader :base_url, :open_search_parameters
50
+
51
+ def base_url=(base_url)
52
+ @base_url = get_description(base_url)
53
+ end
54
+
55
+ def open_search_parameters
56
+ OPEN_SEARCH_PARAMETERS.dup
57
+ end
58
+
59
+ def get_description(url)
60
+ return if url.nil?
61
+
62
+ request = Parliament::Request::BaseRequest.new(base_url: url,
63
+ headers: {'Accept' => 'application/opensearchdescription+xml'})
64
+ xml_response = request.get
65
+
66
+ xml_root = REXML::Document.new(xml_response.body).root
67
+ xml_root.elements['Url'].attributes['template']
68
+ end
69
+ end
70
+
71
+ def query_url
72
+ @query_url
73
+ end
74
+
75
+ def setup_query_url(search_params)
76
+ search_terms = search_params[:query]
77
+ query_url = @base_url.dup
78
+ query_url.gsub!('{searchTerms}', search_terms)
79
+
80
+ @open_search_parameters.each do |key, value|
81
+ camel_case_key = ActiveSupport::Inflector.camelize(key.to_s, false)
82
+ if search_params.keys.include?(key)
83
+ query_url.gsub!("{#{camel_case_key}?}", search_params[key].to_s)
84
+ else
85
+ query_url.gsub!("{#{camel_case_key}?}", value.to_s)
86
+ end
87
+ end
88
+
89
+ @query_url = query_url
90
+ end
91
+ end
92
+ end
93
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parliament-opensearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rebecca Appleyard
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-07 00:00:00.000000000 Z
11
+ date: 2017-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: feedjira
@@ -161,12 +161,10 @@ files:
161
161
  - Rakefile
162
162
  - bin/console
163
163
  - bin/setup
164
+ - lib/parliament/builder/open_search_response_builder.rb
164
165
  - lib/parliament/open_search.rb
165
- - lib/parliament/open_search/builder.rb
166
- - lib/parliament/open_search/builder/open_search_response_builder.rb
167
- - lib/parliament/open_search/request.rb
168
- - lib/parliament/open_search/request/open_search_request.rb
169
166
  - lib/parliament/open_search/version.rb
167
+ - lib/parliament/request/open_search_request.rb
170
168
  - parliament-opensearch.gemspec
171
169
  homepage: http://github.com/ukparliament/parliament_opensearch
172
170
  licenses: []
@@ -187,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
185
  version: '0'
188
186
  requirements: []
189
187
  rubyforge_project:
190
- rubygems_version: 2.6.6
188
+ rubygems_version: 2.6.11
191
189
  signing_key:
192
190
  specification_version: 4
193
191
  summary: Parliamentary OpenSearch response builder
@@ -1,23 +0,0 @@
1
- require 'feedjira'
2
-
3
- module Parliament
4
- module OpenSearch
5
- module Builder
6
- # OpenSearch response builder using Feedjira to parse the response.
7
- #
8
- # @since 0.1.0
9
- class OpenSearchResponseBuilder < Parliament::Builder::BaseResponseBuilder
10
- OPEN_SEARCH_ELEMENTS = %w(totalResults Query startIndex itemsPerPage).freeze
11
-
12
- # Builds a Feedjira::Feed response. It adds the extra OpenSearch feed elements, then parses the HTTP Response.
13
- def build
14
- OPEN_SEARCH_ELEMENTS.each do |element|
15
- Feedjira::Feed.add_common_feed_element(element)
16
- end
17
-
18
- Feedjira::Feed.parse(@response.body)
19
- end
20
- end
21
- end
22
- end
23
- end
@@ -1,11 +0,0 @@
1
- require 'parliament/open_search/builder/open_search_response_builder'
2
-
3
- # Namespace for classes and modules that processes data from OpenSearch APIs.
4
- # @since 0.1.0
5
- module Parliament
6
- module OpenSearch
7
- module Builder
8
- # Currently just a namespace definition
9
- end
10
- end
11
- end
@@ -1,95 +0,0 @@
1
- module Parliament
2
- module OpenSearch
3
- module Request
4
- # API request object, allowing the user to build a request to an OpenSearch API and build a response.
5
- #
6
- # @since 0.1.0
7
- class OpenSearchRequest < Parliament::Request::BaseRequest
8
- OPEN_SEARCH_PARAMETERS = {
9
- count: 10,
10
- start_index: 1,
11
- start_page: 1,
12
- language: '*',
13
- output_encoding: 'UTF-8',
14
- input_encoding: 'UTF-8'
15
- }.freeze
16
-
17
- # Creates a new instance of Parliament::OpenSearch::Request.
18
- #
19
- # An interesting note for #initialize is that setting base_url on the class, or using the environment variable
20
- # OPENSEARCH_DESCRIPTION_URL means you don't need to pass in a base_url. You can pass one anyway to override the
21
- # environment variable or class parameter. Similarly, headers can be set by either settings the headers on the class, or passing headers in.
22
- #
23
- # @see Parliament::BaseRequest#initialize
24
- # @param [String] base_url the base url for the OpenSearch API description. (expected: http://example.com - without the trailing slash).
25
- # @param [Hash] headers the headers being sent in the request.
26
- # @param [Parliament::OpenSearch::Builder] builder the builder required to create the response.
27
- def initialize(base_url: nil, headers: nil, builder: nil)
28
- @base_url = Parliament::Request::OpenSearchRequest.get_description(base_url) || self.class.base_url || ENV['OPENSEARCH_DESCRIPTION_URL']
29
- @open_search_parameters = self.class.open_search_parameters
30
-
31
- super(base_url: @base_url, headers: headers, builder: builder)
32
- end
33
-
34
- # Sets up the query url using the base_url and parameters, then make an HTTP GET request and process results.
35
- #
36
- # @see Parliament::BaseRequest#get
37
- # @params [Hash] search_params the search parameters to be passed to the OpenSearch API. This is the search term and/or any of the keys from OPEN_SEARCH_PARAMETERS, depending on the parameters allowed in the API.
38
- # @params [Hash] params any extra parameters.
39
- def get(search_params, params: nil)
40
- setup_query_url(search_params)
41
-
42
- super(params: params)
43
- end
44
-
45
- private
46
-
47
- # @attr [String] base_url the base url for the OpenSearch API description. (expected: http://example.com - without the trailing slash).
48
- # @attr [Hash] open_search_parameters the possible parameters to use in the query url.
49
- class << self
50
- attr_reader :base_url, :open_search_parameters
51
-
52
- def base_url=(base_url)
53
- @base_url = get_description(base_url)
54
- end
55
-
56
- def open_search_parameters
57
- OPEN_SEARCH_PARAMETERS.dup
58
- end
59
-
60
- def get_description(url)
61
- return if url.nil?
62
-
63
- request = Parliament::Request::BaseRequest.new(base_url: url,
64
- headers: {'Accept' => 'application/opensearchdescription+xml'})
65
- xml_response = request.get
66
-
67
- xml_root = REXML::Document.new(xml_response.body).root
68
- xml_root.elements['Url'].attributes['template']
69
- end
70
- end
71
-
72
- def query_url
73
- @query_url
74
- end
75
-
76
- def setup_query_url(search_params)
77
- search_terms = search_params[:query]
78
- query_url = @base_url.dup
79
- query_url.gsub!('{searchTerms}', search_terms)
80
-
81
- @open_search_parameters.each do |key, value|
82
- camel_case_key = ActiveSupport::Inflector.camelize(key.to_s, false)
83
- if search_params.keys.include?(key)
84
- query_url.gsub!("{#{camel_case_key}?}", search_params[key].to_s)
85
- else
86
- query_url.gsub!("{#{camel_case_key}?}", value.to_s)
87
- end
88
- end
89
-
90
- @query_url = query_url
91
- end
92
- end
93
- end
94
- end
95
- end
@@ -1,11 +0,0 @@
1
- require 'parliament/open_search/request/open_search_request'
2
-
3
- # Namespace for classes and modules that handle connections to OpenSearch APIs.
4
- # @since 0.1.0
5
- module Parliament
6
- module OpenSearch
7
- module Request
8
- # Currently just a namespace definition
9
- end
10
- end
11
- end