parliament-opensearch 0.2.5 → 0.2.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 624c7a0bb16dc5fbc8da124f2625425f5a7a9934
4
- data.tar.gz: 7eb3e957efe50ea05bd723dd8f275eac526e9e4c
3
+ metadata.gz: d3ed8751eb8608de936ab34091f78c00647c9bde
4
+ data.tar.gz: 8987fa2c6ae21159f9288fbcdfd0779484d319d0
5
5
  SHA512:
6
- metadata.gz: b579f51e21cc50a84bd9832170d68630068f97ec241a3db299510d5cc69acd407b8567078a15ae14d581bdcd13a969f8625c3d552887a73b08cd86c5ccb47296
7
- data.tar.gz: 63240287a3b8746dadd1d9767fb80d013dd64240085640611703b3ae7f82f200868c0e4261cdc045fa1dff352bd6d37611876b215d342713928b3d51c3bae6bf
6
+ metadata.gz: 2f85f5127b033c6b23583c679858582d534b5ffe7a966bb38194d991fe04aa696bdb723b5c445db15b7210347208c5753723a115ea20c06117495e72c6ac3233
7
+ data.tar.gz: 7f05a0dc8c3f3d35115d15ac7e679e6853f7a9c221724e56204edf285168afc3232e70f8b8664056ab6d0139f6041a9f9be0380b529138c350a6c1346323da4d
data/Rakefile CHANGED
@@ -3,4 +3,4 @@ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "parliament/open_search"
3
+ require 'bundler/setup'
4
+ require 'parliament/open_search'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "parliament/open_search"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start(__FILE__)
@@ -1,6 +1,6 @@
1
- # Namespace for classes and modules that handle connections to, and processing of data from OpenSearch APIs.
2
- # @since 0.1.0
3
1
  module Parliament
2
+ # Namespace for classes and modules that handle connections to, and processing of data from OpenSearch APIs.
3
+ # @since 0.1.0
4
4
  module OpenSearch
5
5
  # Currently just a namespace definition
6
6
  class << self
@@ -1,5 +1,5 @@
1
1
  module Parliament
2
2
  module OpenSearch
3
- VERSION = '0.2.5'.freeze
3
+ VERSION = '0.2.6'.freeze
4
4
  end
5
5
  end
@@ -3,14 +3,17 @@ module Parliament
3
3
  # API request object, allowing the user to build a request to an OpenSearch API and build a response.
4
4
  #
5
5
  # @since 0.1.0
6
+ # attr [Hash] templates the different types and template urls set from the OpenSearch API description document.
6
7
  class OpenSearchRequest < Parliament::Request::BaseRequest
8
+ attr_reader :templates
9
+
7
10
  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'
11
+ count: 10,
12
+ start_index: 1,
13
+ start_page: 1,
14
+ language: '*',
15
+ output_encoding: 'UTF-8',
16
+ input_encoding: 'UTF-8'
14
17
  }.freeze
15
18
 
16
19
  # Creates a new instance of Parliament::OpenSearch::Request.
@@ -26,35 +29,36 @@ module Parliament
26
29
  def initialize(description_url: nil, headers: nil, builder: nil)
27
30
  @description_url = description_url
28
31
 
29
- raise Parliament::OpenSearch::DescriptionError.new(@description_url), 'No description URL passed to Parliament::OpenSearchRequest#new and, no Parliament::OpenSearchRequest#base_url value set. Without a description URL, we are unable to make any search requests.' if @description_url.nil? && self.class.base_url.nil?
32
+ raise Parliament::OpenSearch::DescriptionError.new(@description_url), 'No description URL passed to Parliament::OpenSearchRequest#new and, no Parliament::OpenSearchRequest#base_url value set. Without a description URL, we are unable to make any search requests.' if @description_url.nil? && self.class.templates.nil?
30
33
 
31
- @base_url = Parliament::Request::OpenSearchRequest.get_description(@description_url) || self.class.base_url
34
+ @templates = Parliament::Request::OpenSearchRequest.get_description(@description_url) || self.class.templates
32
35
  @open_search_parameters = self.class.open_search_parameters
33
36
 
34
- super(base_url: @base_url, headers: headers, builder: builder)
37
+ super(base_url: nil, headers: headers, builder: builder)
35
38
  end
36
39
 
37
40
  # Sets up the query url using the base_url and parameters, then make an HTTP GET request and process results.
38
41
  #
39
42
  # @see Parliament::BaseRequest#get
40
43
  # @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.
44
+ # @params [String] type the type of data requested, eg. 'application/atom+xml'.
41
45
  # @params [Hash] params any extra parameters.
42
- def get(search_params, params: nil)
43
- setup_query_url(search_params)
46
+ def get(search_params, type: nil, params: nil)
47
+ setup_query_url(search_params, type)
44
48
 
45
49
  super(params: params)
46
50
  end
47
51
 
48
52
  private
49
53
 
50
- # @attr [String] base_url the base url for the OpenSearch API description. (expected: http://example.com - without the trailing slash).
54
+ # @attr [String] templates the different types and template urls set from the OpenSearch API description document.
51
55
  # @attr [Hash] open_search_parameters the possible parameters to use in the query url.
52
56
  class << self
53
- attr_reader :description_url, :base_url
57
+ attr_reader :description_url, :templates
54
58
 
55
59
  def description_url=(description_url)
56
60
  @description_url = description_url
57
- @base_url = Parliament::Request::OpenSearchRequest.get_description(@description_url)
61
+ @templates = Parliament::Request::OpenSearchRequest.get_description(@description_url)
58
62
  end
59
63
 
60
64
  def open_search_parameters
@@ -71,21 +75,27 @@ module Parliament
71
75
  end
72
76
 
73
77
  request = Parliament::Request::BaseRequest.new(base_url: url,
74
- headers: { 'Accept' => 'application/opensearchdescription+xml',
75
- 'Ocp-Apim-Subscription-Key' => ENV['OPENSEARCH_AUTH_TOKEN']
78
+ headers: {
79
+ 'Accept' => 'application/opensearchdescription+xml',
80
+ 'Ocp-Apim-Subscription-Key' => ENV['OPENSEARCH_AUTH_TOKEN']
76
81
  })
77
82
  xml_response = request.get
78
83
 
79
84
  begin
80
85
  xml_root = REXML::Document.new(xml_response.response.body).root
81
- template = xml_root.elements['Url'].attributes['template']
82
-
83
- raise Parliament::OpenSearch::DescriptionError.new(url), "The document found does not contain a require node. Attempted to get a 'Url' element with the attribute 'template'. Please check the description document at '#{url}' and try again." if template.nil?
86
+ templates = { url: [] }
87
+ xml_root.elements.each('Url') do |url|
88
+ type = url.attributes['type']
89
+ template = url.attributes['template']
90
+ templates[:url] << { type: type, template: template }
91
+ end
92
+
93
+ raise Parliament::OpenSearch::DescriptionError.new(url), "The document found does not contain the required node. Attempted to get a 'Url' element with the attribute 'template'. Please check the description document at '#{url}' and try again." if templates[:url].empty?
84
94
  rescue NoMethodError
85
95
  raise Parliament::OpenSearch::DescriptionError.new(url), "The document found does not appear to be XML. Please check the description document at '#{url}' and try again."
86
96
  end
87
97
 
88
- template
98
+ templates
89
99
  end
90
100
  end
91
101
 
@@ -93,9 +103,16 @@ module Parliament
93
103
  @query_url
94
104
  end
95
105
 
96
- def setup_query_url(search_params)
106
+ def setup_query_url(search_params, type = nil)
97
107
  search_terms = search_params[:query]
98
- query_url = @base_url.dup
108
+ type ||= 'application/atom+xml'
109
+
110
+ url_hash = @templates[:url].select { |url| url[:type] == type }.first
111
+
112
+ raise Parliament::OpenSearch::DescriptionError.new(type), "There is no url for the requested type '#{type}'." if url_hash.nil?
113
+
114
+ query_url = url_hash[:template].dup
115
+
99
116
  query_url.gsub!('{searchTerms}', search_terms)
100
117
 
101
118
  @open_search_parameters.each do |key, value|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parliament-opensearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rebecca Appleyard
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-05-04 00:00:00.000000000 Z
12
+ date: 2017-05-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: feedjira