parliament-opensearch 0.1.0 → 0.1.1
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/lib/parliament/builder/open_search_response_builder.rb +21 -0
- data/lib/parliament/open_search/version.rb +1 -1
- data/lib/parliament/open_search.rb +2 -2
- data/lib/parliament/request/open_search_request.rb +93 -0
- metadata +5 -7
- data/lib/parliament/open_search/builder/open_search_response_builder.rb +0 -23
- data/lib/parliament/open_search/builder.rb +0 -11
- data/lib/parliament/open_search/request/open_search_request.rb +0 -95
- data/lib/parliament/open_search/request.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51c60a8ca7fca47e247c68f31f0bda226bc9d3e4
|
4
|
+
data.tar.gz: 640adba09ea47477410b8863ec803b264fd1fb2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,8 +1,8 @@
|
|
1
1
|
require 'parliament'
|
2
2
|
|
3
3
|
require 'parliament/open_search/version'
|
4
|
-
require 'parliament/
|
5
|
-
require 'parliament/
|
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.
|
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-
|
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.
|
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
|