fieldhand 0.8.0 → 0.9.0

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
  SHA256:
3
- metadata.gz: 48341fcf65a936b5bc42a7f6cdcc9881d2d3f11ef29352a14f0f545f1318b5a0
4
- data.tar.gz: 17ce75aba6b336de464b496604aec0bbda5d673ff98d6c8ee1a659b0f75fb425
3
+ metadata.gz: fc45bc1384bec75919f2d929ee512748554511d90b0818024b930f4c133a70b2
4
+ data.tar.gz: c31b6bb9872a34e3bd65bc94cb76364c645c767970e3a277a3c69b6b1831f3c2
5
5
  SHA512:
6
- metadata.gz: fddd10cde8fc771c259234deadef079468f8a3517174a82e92dd480a29d5ccbbd655dfeb5f1f4fceeda85b1e4d9707ab715ddde8baf6fa2e43fd2fedd6867121
7
- data.tar.gz: 9f201592dc6968fdf41c5bd8134c67496f4eed2c89077442474b0491ef83417e170a182da2c1262aaca3d811ad6dd2e756a18e1e8e025a799e4cbd6e1ef2f616
6
+ metadata.gz: 76896e2562bd40ed991d10fe57bb07532a42aa08f5653c78342756819010d62ff66b854c39cf3fb74d6004ab049623870091a9725fbd960de8a89bcc25d02b83
7
+ data.tar.gz: 071e2ee3300fa813699eda02ab107c02f1362241a893e7beb2b1d2becba8ac14364ab7890a870fe4de43f0dde1f87a593856a310c19b26b3740f76baf4802d84
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2017 Altmetric LLP
3
+ Copyright (c) 2017-2018 Altmetric LLP
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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.8.0
5
+ **Current version:** 0.9.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.8'
11
+ gem install fieldhand -v '~> 0.9'
12
12
  ```
13
13
 
14
14
  Or, in your `Gemfile`:
15
15
 
16
16
  ```ruby
17
- gem 'fieldhand', '~> 0.8'
17
+ gem 'fieldhand', '~> 0.9'
18
18
  ```
19
19
 
20
20
  ## Usage
@@ -114,15 +114,16 @@ A class to represent [an OAI-PMH repository](https://www.openarchives.org/OAI/op
114
114
  ```ruby
115
115
  Fieldhand::Repository.new('http://www.example.com/oai')
116
116
  Fieldhand::Repository.new(URI('http://www.example.com/oai'))
117
- Fieldhand::Repository.new('http://www.example.com/oai', :logger => Logger.new(STDOUT), :timeout => 10)
117
+ Fieldhand::Repository.new('http://www.example.com/oai', :logger => Logger.new(STDOUT), :timeout => 10, :bearer_token => 'decafbad')
118
118
  ```
119
119
 
120
120
  Return a new [`Repository`](#fieldhandrepository) instance accessible at the given `uri` (specified
121
121
  either as a [`URI`][URI] or
122
- something that can be coerced into a `URI` such as a `String`) with two options passed as a `Hash`:
122
+ something that can be coerced into a `URI` such as a `String`) with three options passed as a `Hash`:
123
123
 
124
124
  * `:logger`: a [`Logger`](http://ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger.html)-compatible `logger`, defaults to a platform-specific null logger;
125
- * `:timeout`: a `Numeric` number of seconds to wait before timing out any HTTP requests, defaults to 60.
125
+ * `:timeout`: a `Numeric` number of seconds to wait before timing out any HTTP requests, defaults to 60;
126
+ * `:bearer_token`: a `String` bearer token to authorize any HTTP requests, defaults to `nil`.
126
127
 
127
128
  #### `Fieldhand::Repository#identify`
128
129
 
@@ -632,6 +633,6 @@ This can be used to rescue all the following child error types.
632
633
 
633
634
  ## License
634
635
 
635
- Copyright © 2017 Altmetric LLP
636
+ Copyright © 2017-2018 Altmetric LLP
636
637
 
637
638
  Distributed under the MIT License.
@@ -16,6 +16,7 @@ module Fieldhand
16
16
  # * :logger - A `Logger`-compatible class for logging the activity of the library, defaults to a platform-specific
17
17
  # null logger
18
18
  # * :timeout - A `Numeric` number of seconds to wait for any HTTP requests, defaults to 60 seconds
19
+ # * :bearer_token - A `String` bearer token to use when sending any HTTP requests, defaults to nil
19
20
  def initialize(logger_or_options = {})
20
21
  @logger_or_options = logger_or_options
21
22
  end
@@ -30,6 +31,11 @@ module Fieldhand
30
31
  options.fetch(:logger) { Logger.null }
31
32
  end
32
33
 
34
+ # Return the current bearer token.
35
+ def bearer_token
36
+ options[:bearer_token]
37
+ end
38
+
33
39
  private
34
40
 
35
41
  def options
@@ -11,19 +11,21 @@ module Fieldhand
11
11
  #
12
12
  # See https://www.openarchives.org/OAI/openarchivesprotocol.html#FlowControl
13
13
  class Paginator
14
- attr_reader :uri, :logger, :timeout, :http
14
+ attr_reader :uri, :logger, :timeout, :bearer_token, :http
15
15
 
16
- # Return a new paginator for the given repository base URI and optional logger and timeout.
16
+ # Return a new paginator for the given repository base URI and optional logger, timeout and bearer token.
17
17
  #
18
18
  # The URI can be passed as either a `URI` or something that can be parsed as a URI such as a string.
19
19
  #
20
- # The logger will default to a null logger appropriate to this platform and timeout will default to 60 seconds.
20
+ # The logger will default to a null logger appropriate to this platform, timeout will default to 60 seconds and the
21
+ # bearer token will default to nil.
21
22
  def initialize(uri, logger_or_options = {})
22
23
  @uri = uri.is_a?(::URI) ? uri : URI(uri)
23
24
 
24
25
  options = Options.new(logger_or_options)
25
26
  @logger = options.logger
26
27
  @timeout = options.timeout
28
+ @bearer_token = options.bearer_token
27
29
 
28
30
  @http = ::Net::HTTP.new(@uri.host, @uri.port)
29
31
  @http.read_timeout = @timeout
@@ -89,7 +91,7 @@ module Fieldhand
89
91
  request_uri.query = encode_query(query)
90
92
 
91
93
  logger.info('Fieldhand') { "GET #{request_uri}" }
92
- http.get(request_uri.request_uri)
94
+ http.request(authenticated_request(request_uri.request_uri))
93
95
  rescue ::Timeout::Error => e
94
96
  raise NetworkError, "timeout requesting #{query}: #{e}"
95
97
  rescue => e
@@ -99,5 +101,12 @@ module Fieldhand
99
101
  def encode_query(query = {})
100
102
  query.map { |k, v| ::CGI.escape(k) << '=' << ::CGI.escape(v) }.join('&')
101
103
  end
104
+
105
+ def authenticated_request(uri)
106
+ request = ::Net::HTTP::Get.new(uri)
107
+ request['Authorization'] = "Bearer #{bearer_token}" if bearer_token
108
+
109
+ request
110
+ end
102
111
  end
103
112
  end
@@ -14,22 +14,23 @@ module Fieldhand
14
14
  #
15
15
  # See https://www.openarchives.org/OAI/openarchivesprotocol.html
16
16
  class Repository
17
- attr_reader :uri, :logger, :timeout
17
+ attr_reader :uri, :logger, :timeout, :bearer_token
18
18
 
19
- # Return a new repository with the given base URL and an optional logger and timeout.
19
+ # Return a new repository with the given base URL and an optional logger, timeout and bearer token.
20
20
  #
21
21
  # The base URL can be passed as a `URI` or anything that can be parsed as a URI such as a string.
22
22
  #
23
23
  # For backward compatibility, the second argument can either be a logger or a hash containing
24
- # a logger and timeout.
24
+ # a logger, timeout and bearer token.
25
25
  #
26
- # Defaults to using a null logger specific to this platform and a timeout of 60 seconds.
26
+ # Defaults to using a null logger specific to this platform, a timeout of 60 seconds and no bearer token.
27
27
  def initialize(uri, logger_or_options = {})
28
28
  @uri = uri.is_a?(::URI) ? uri : URI(uri)
29
29
 
30
30
  options = Options.new(logger_or_options)
31
31
  @logger = options.logger
32
32
  @timeout = options.timeout
33
+ @bearer_token = options.bearer_token
33
34
  end
34
35
 
35
36
  # Send an Identify request to the repository and return an `Identify` response.
@@ -133,7 +134,7 @@ module Fieldhand
133
134
  private
134
135
 
135
136
  def paginator
136
- @paginator ||= Paginator.new(uri, :logger => logger, :timeout => timeout)
137
+ @paginator ||= Paginator.new(uri, :logger => logger, :timeout => timeout, :bearer_token => bearer_token)
137
138
  end
138
139
  end
139
140
  end
@@ -55,5 +55,19 @@ module Fieldhand
55
55
  expect(options.logger).to be_nil
56
56
  end
57
57
  end
58
+
59
+ describe '#bearer_token' do
60
+ it 'defaults to nil' do
61
+ options = described_class.new({})
62
+
63
+ expect(options.bearer_token).to be_nil
64
+ end
65
+
66
+ it 'can be overridden by passing a token in an option' do
67
+ options = described_class.new(:bearer_token => 'decafbad')
68
+
69
+ expect(options.bearer_token).to eq('decafbad')
70
+ end
71
+ end
58
72
  end
59
73
  end
@@ -145,5 +145,38 @@ module Fieldhand
145
145
  expect(paginator.logger).to eq(logger)
146
146
  end
147
147
  end
148
+
149
+ describe '#bearer_token' do
150
+ it 'defaults to nil' do
151
+ paginator = described_class.new('http://www.example.com/oai')
152
+
153
+ expect(paginator.bearer_token).to be_nil
154
+ end
155
+
156
+ it 'can be overridden with an option' do
157
+ paginator = described_class.new('http://www.example.com/oai', :bearer_token => 'decafbad')
158
+
159
+ expect(paginator.bearer_token).to eq('decafbad')
160
+ end
161
+
162
+ it 'sends no authorization header without a bearer token' do
163
+ request = stub_oai_request('http://www.example.com/oai?verb=Identify', 'identify.xml')
164
+ paginator = described_class.new('http://www.example.com/oai')
165
+
166
+ paginator.items('Identify', IdentifyParser).first
167
+
168
+ expect(request).to have_been_requested
169
+ end
170
+
171
+ it 'sends an authorization header with a bearer token' do
172
+ request = stub_oai_request('http://www.example.com/oai?verb=Identify', 'identify.xml').
173
+ with(:headers => { 'Authorization' => 'Bearer decafbad' })
174
+ paginator = described_class.new('http://www.example.com/oai', :bearer_token => 'decafbad')
175
+
176
+ paginator.items('Identify', IdentifyParser).first
177
+
178
+ expect(request).to have_been_requested
179
+ end
180
+ end
148
181
  end
149
182
  end
@@ -223,5 +223,29 @@ module Fieldhand
223
223
  expect(repository.logger).to eq(logger)
224
224
  end
225
225
  end
226
+
227
+ describe '#bearer_token' do
228
+ it 'defaults to nil' do
229
+ repository = described_class.new('http://www.example.com/oai')
230
+
231
+ expect(repository.bearer_token).to be_nil
232
+ end
233
+
234
+ it 'can be overridden with an option' do
235
+ repository = described_class.new('http://www.example.com/oai', :bearer_token => 'decafbad')
236
+
237
+ expect(repository.bearer_token).to eq('decafbad')
238
+ end
239
+
240
+ it 'uses the bearer token to authorize HTTP requests' do
241
+ request = stub_oai_request('http://www.example.com/oai?verb=Identify', 'identify.xml').
242
+ with(:headers => { 'Authorization' => 'Bearer decafbad' })
243
+ repository = described_class.new('http://www.example.com/oai', :bearer_token => 'decafbad')
244
+
245
+ repository.identify
246
+
247
+ expect(request).to have_been_requested
248
+ end
249
+ end
226
250
  end
227
251
  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.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Mucur
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2018-05-12 00:00:00.000000000 Z
14
+ date: 2018-06-26 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: ox