fieldhand 0.4.0 → 0.5.0

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: 3beca3a7544bc041d0b89472b73e18ba4c682655
4
- data.tar.gz: 33ea99b5627312c8073abda6898f50b30b338907
3
+ metadata.gz: 67cb8ee7c5ff120c78181ef597a9f2ea46ed4e51
4
+ data.tar.gz: c83dbeb7abe6832c9e2b65fef55689c195c63805
5
5
  SHA512:
6
- metadata.gz: ca95ba3dfc8f3ed489edde185c135ec8beb34ba5879fabac66112f5742d63ed1e6ece180711208983882e8ecd636bcd934c3cde7fde8ca502e44ecce502b59d4
7
- data.tar.gz: 844fb6b4b2d28b690477fcd58349a67149b2e73a38d2f466f1cde7d70aa8c1e09336cbd5fd048b5f60e905215cc321acbb032451b2c4c6c394807b5235de9e69
6
+ metadata.gz: b5adbfea0e91229dce93d0e95a4daca0d206cb7746875f956c912e4934170b152e8afd3719ab851190a7f5c57a1b3f852db14b0f73a06aaffda9ad29ca09ad4a
7
+ data.tar.gz: 667cc59bb4b785d383eeb3314483bc3a05ba66ada5843d27d3998e2d75baf0605ea35de2cc83949bafaf31115a2389b8975bf1523bc66b69ee7b6effb996ba3c
data/README.md CHANGED
@@ -2,19 +2,19 @@
2
2
 
3
3
  A Ruby library for harvesting metadata from [OAI-PMH](https://www.openarchives.org/OAI/openarchivesprotocol.html) repositories.
4
4
 
5
- **Current version:** 0.4.0
5
+ **Current version:** 0.5.0
6
6
  **Supported Ruby versions:** 1.8.7, 1.9.2, 1.9.3, 2.0, 2.1, 2.2
7
7
 
8
8
  ## Installation
9
9
 
10
10
  ```
11
- gem install fieldhand -v '~> 0.4'
11
+ gem install fieldhand -v '~> 0.5'
12
12
  ```
13
13
 
14
14
  Or, in your `Gemfile`:
15
15
 
16
16
  ```ruby
17
- gem 'fieldhand', '~> 0.4'
17
+ gem 'fieldhand', '~> 0.5'
18
18
  ```
19
19
 
20
20
  ## Usage
@@ -88,7 +88,8 @@ repository.get('oai:www.example.com:12345')
88
88
  * [`#sets`](#fieldhandheadersets)
89
89
  * [`#response_date`](#fieldhandheaderresponse_date)
90
90
  * [`Fieldhand::NetworkError`](#fieldhandnetworkerror)
91
- * [`Fieldhand::NetworkError#response`](#fieldhandnetworkerrorresponse)
91
+ * [`Fieldhand::ResponseError`](#fieldhandresponseerror)
92
+ * [`Fieldhand::ResponseError#response`](#fieldhandresponseerrorresponse)
92
93
  * [`Fieldhand::ProtocolError`](#fieldhandprotocolerror)
93
94
  * [`Fieldhand::BadArgumentError`](#fieldhandbadargumenterror)
94
95
  * [`Fieldhand::BadResumptionTokenError`](#fieldhandbadresumptiontokenerror)
@@ -537,19 +538,22 @@ Return the time and date that the response was sent.
537
538
 
538
539
  ### `Fieldhand::NetworkError`
539
540
 
540
- An error (descended from `StandardError`) to represent any network issues encountered during interaction with the repository.
541
+ An error (descended from `StandardError`) to represent any network issues encountered during interaction with the repository. Any underlying exception is exposed in Ruby 2.1 onwards through [`Exception#cause`](https://ruby-doc.org/core-2.1.0/Exception.html#method-i-cause).
542
+
543
+ #### `Fieldhand::ResponseError`
544
+
545
+ An error (descended from `NetworkError`) to represent any issues in the response from the repository.
541
546
  If the HTTP request is not successful (returning a status code other than 200),
542
- a `NetworkError` exception will be raised containing the error message and the response object.
543
- Any underlying exception is exposed in Ruby 2.1 onwards through [`Exception#cause`](https://ruby-doc.org/core-2.1.0/Exception.html#method-i-cause).
547
+ a `ResponseError` exception will be raised containing the error message and the response object.
544
548
 
545
- #### Fieldhand::NetworkError#response
549
+ ##### Fieldhand::ResponseError#response
546
550
 
547
551
  ```ruby
548
552
  begin
549
553
  repository.records.each do |record|
550
554
  # ...
551
555
  end
552
- rescue Fieldhand::NetworkError => e
556
+ rescue Fieldhand::ResponseError => e
553
557
  puts e.response
554
558
  #=> #<Net::HTTPServiceUnavailable 503 Service Unavailable readbody=true>
555
559
  end
@@ -0,0 +1,14 @@
1
+ module Fieldhand
2
+ NetworkError = ::Class.new(::StandardError)
3
+
4
+ # Custom exception to handle unexpected HTTP responses
5
+ class ResponseError < NetworkError
6
+ attr_reader :response
7
+
8
+ def initialize(response)
9
+ super("Invalid response: #{response.code} #{response.msg}")
10
+
11
+ @response = response
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  require 'fieldhand/logger'
2
- require 'fieldhand/network_error'
2
+ require 'fieldhand/network_errors'
3
3
  require 'fieldhand/response_parser'
4
4
  require 'cgi'
5
5
  require 'net/http'
@@ -67,7 +67,10 @@ module Fieldhand
67
67
  private
68
68
 
69
69
  def parse_response(query = {})
70
- response_parser = ResponseParser.new(request(query))
70
+ response = request(query)
71
+ raise ResponseError, response unless response.is_a?(::Net::HTTPSuccess)
72
+
73
+ response_parser = ResponseParser.new(response.body)
71
74
  response_parser.errors.each do |error|
72
75
  raise error
73
76
  end
@@ -80,14 +83,10 @@ module Fieldhand
80
83
  request_uri.query = encode_query(query)
81
84
 
82
85
  logger.info('Fieldhand') { "GET #{request_uri}" }
83
- res = http.get(request_uri.request_uri)
84
- raise NetworkError.new("Invalid response: #{res.code} #{res.msg}", res) unless res.is_a?(::Net::HTTPSuccess)
85
-
86
- res.body
86
+ http.get(request_uri.request_uri)
87
87
  rescue ::Timeout::Error => e
88
88
  raise NetworkError, "timeout requesting #{query}: #{e}"
89
89
  rescue => e
90
- raise e if e.is_a?(NetworkError)
91
90
  raise NetworkError, "error requesting #{query}: #{e}"
92
91
  end
93
92
 
@@ -84,6 +84,30 @@ module Fieldhand
84
84
  expect { paginator.items('ListSets', ListSetsParser).first }.
85
85
  to raise_error(NoSetHierarchyError)
86
86
  end
87
+
88
+ it 'raises a Response Error if an unsuccessful response is returned' do
89
+ stub_request(:get, 'http://www.example.com/oai?verb=Identify').
90
+ to_return(:status => 503, :body => 'Retry after 5 seconds')
91
+ paginator = described_class.new('http://www.example.com/oai')
92
+
93
+ expect { paginator.items('Identify', IdentifyParser).first }.
94
+ to raise_error(ResponseError)
95
+ end
96
+
97
+ it 'raises a Response Error containing a response object' do
98
+ stub_request(:get, 'http://www.example.com/oai?verb=Identify').
99
+ to_return(:status => 503, :body => 'Retry after 5 seconds')
100
+ paginator = described_class.new('http://www.example.com/oai')
101
+ error = nil
102
+
103
+ begin
104
+ paginator.items('Identify', IdentifyParser).first
105
+ rescue => e
106
+ error = e
107
+ end
108
+
109
+ expect(error.response.body).to eq('Retry after 5 seconds')
110
+ end
87
111
  end
88
112
  end
89
113
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fieldhand
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Mucur
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-07-10 00:00:00.000000000 Z
13
+ date: 2017-07-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: ox
@@ -101,7 +101,7 @@ files:
101
101
  - lib/fieldhand/list_sets_parser.rb
102
102
  - lib/fieldhand/logger.rb
103
103
  - lib/fieldhand/metadata_format.rb
104
- - lib/fieldhand/network_error.rb
104
+ - lib/fieldhand/network_errors.rb
105
105
  - lib/fieldhand/paginator.rb
106
106
  - lib/fieldhand/record.rb
107
107
  - lib/fieldhand/repository.rb
@@ -1,11 +0,0 @@
1
- module Fieldhand
2
- # Custom exception to handle connection errors and invalid requests
3
- class NetworkError < StandardError
4
- attr_reader :response
5
-
6
- def initialize(message, response = nil)
7
- super(message)
8
- @response = response
9
- end
10
- end
11
- end