parliament-opensearch 0.2.2 → 0.2.3

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: b23156551353cbfe6be82648b354761bbbce30fb
4
- data.tar.gz: f236613bf33beee7deb6f2fecff00f6659e547f8
3
+ metadata.gz: 89764f0e23de18af2c597be14a52198dc7011e16
4
+ data.tar.gz: 95f44a316c601778ad1fd466757b2b9b5deec1ce
5
5
  SHA512:
6
- metadata.gz: 9ab2fea386df769c812c429a1157e7ff8522277f41a2ee3508b0986795d24eead0ba7db7e2c39ff809e032259d148dad10ae0b916499f840d9703d20a0b2886f
7
- data.tar.gz: 53f36a86528539c02a9ca4be4b2141ebf1d8965f3c22474ecaab708c257ab0f99b9d5b8545965c9bae5d8d56a65ad60f1ad4e199e587dce31e61f67a359a0b4d
6
+ metadata.gz: d9d48aa35039abcee3cef6731d79af106f020b306098f11e01bca5bec6e49f16aecf7415d65613ce680dfa944b2f8a467a4236fe788aac86a51f67ace2d2c1df
7
+ data.tar.gz: acb15d5c8218eb7fd4d817f9dfe01683c734b3fff359b6c7cea1a59723d08e219bd3e617152d9ce69252c8f7b96ac67ca4b3ec94641d8f29c682b8f14f8dadc4
@@ -3,6 +3,7 @@ require 'rexml/document'
3
3
  require 'parliament'
4
4
 
5
5
  require 'parliament/open_search/version'
6
+ require 'parliament/open_search/description_error'
6
7
  require 'parliament/request/open_search_request'
7
8
  require 'parliament/builder/open_search_response_builder'
8
9
 
@@ -0,0 +1,18 @@
1
+ module Parliament
2
+ module OpenSearch
3
+ # An error raised when there is an issue processing an OpenSearch description file.
4
+ # This could be raised when the URL provided is invalid, or when we are unable to find a URL template for future requests.
5
+ #
6
+ # @attr_reader url the description url that caused the Parliament::OpenSearchDescriptionError.
7
+ #
8
+ # @since 0.2.3
9
+ class DescriptionError < StandardError
10
+ attr_reader :url
11
+
12
+ # @param [String] description_url the description url that caused the Parliament::OpenSearchDescriptionError.
13
+ def initialize(description_url)
14
+ @url = description_url
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,5 +1,5 @@
1
1
  module Parliament
2
2
  module OpenSearch
3
- VERSION = '0.2.2'.freeze
3
+ VERSION = '0.2.3'.freeze
4
4
  end
5
5
  end
@@ -15,16 +15,20 @@ module Parliament
15
15
 
16
16
  # Creates a new instance of Parliament::OpenSearch::Request.
17
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.
18
+ # An interesting note for #initialize is that setting description_url on the class means you don't need to pass in a description_url.
19
+ # You can pass one anyway to override the class parameter. Similarly, headers can be set by either settings the headers on the class,
20
+ # or passing headers in.
21
21
  #
22
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).
23
+ # @param [String] description_url the url for the OpenSearch API description file. (expected: http://example.com/description.xml - without the trailing slash).
24
24
  # @param [Hash] headers the headers being sent in the request.
25
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
26
+ def initialize(description_url: nil, headers: nil, builder: nil)
27
+ @description_url = description_url
28
+
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?
30
+
31
+ @base_url = Parliament::Request::OpenSearchRequest.get_description(@description_url) || self.class.base_url
28
32
  @open_search_parameters = self.class.open_search_parameters
29
33
 
30
34
  super(base_url: @base_url, headers: headers, builder: builder)
@@ -46,10 +50,11 @@ module Parliament
46
50
  # @attr [String] base_url the base url for the OpenSearch API description. (expected: http://example.com - without the trailing slash).
47
51
  # @attr [Hash] open_search_parameters the possible parameters to use in the query url.
48
52
  class << self
49
- attr_reader :base_url, :open_search_parameters
53
+ attr_reader :description_url, :base_url
50
54
 
51
- def base_url=(base_url)
52
- @base_url = get_description(base_url)
55
+ def description_url=(description_url)
56
+ @description_url = description_url
57
+ @base_url = Parliament::Request::OpenSearchRequest.get_description(@description_url)
53
58
  end
54
59
 
55
60
  def open_search_parameters
@@ -59,12 +64,26 @@ module Parliament
59
64
  def get_description(url)
60
65
  return if url.nil?
61
66
 
67
+ begin
68
+ URI.parse(url)
69
+ rescue URI::InvalidURIError => e
70
+ raise Parliament::OpenSearch::DescriptionError.new(url), "Invalid description URI passed '#{url}': #{e.message}"
71
+ end
72
+
62
73
  request = Parliament::Request::BaseRequest.new(base_url: url,
63
74
  headers: {'Accept' => 'application/opensearchdescription+xml'})
64
75
  xml_response = request.get
65
76
 
66
- xml_root = REXML::Document.new(xml_response.body).root
67
- xml_root.elements['Url'].attributes['template']
77
+ begin
78
+ xml_root = REXML::Document.new(xml_response.body).root
79
+ template = xml_root.elements['Url'].attributes['template']
80
+
81
+ 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?
82
+ rescue NoMethodError
83
+ 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."
84
+ end
85
+
86
+ template
68
87
  end
69
88
  end
70
89
 
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.2
4
+ version: 0.2.3
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-04-11 00:00:00.000000000 Z
12
+ date: 2017-04-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: feedjira
@@ -165,6 +165,7 @@ files:
165
165
  - bin/setup
166
166
  - lib/parliament/builder/open_search_response_builder.rb
167
167
  - lib/parliament/open_search.rb
168
+ - lib/parliament/open_search/description_error.rb
168
169
  - lib/parliament/open_search/version.rb
169
170
  - lib/parliament/request/open_search_request.rb
170
171
  - parliament-opensearch.gemspec