Skip to content

Commit 0e8d143

Browse files
author
Thomas Johnson
committed
Merge pull request #43 from code4lib/oai-client-headers
Allow OAI::Client to pass through additional request headers (e.g. From, User-Agent)
2 parents 2962523 + af79e97 commit 0e8d143

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: ruby
22
rvm:
33
- 1.8.7
44
- 1.9.3
5+
- 2.2.1
56
- jruby-18mode
67
- jruby-19mode
78
script: bundle exec rake test

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ group :test do
1313
gem 'redcarpet', :platform => :ruby # For fast, Github-like Markdown
1414
gem 'kramdown', :platform => :jruby # For Markdown without a C compiler
1515
gem 'rcov', '~> 0.9', :platform => [:ruby_18, :jruby]
16+
gem 'test-unit'
1617
gem 'simplecov', :platform => :ruby_19
1718
gem 'simplecov-rcov', :platform => :ruby_19
1819
gem 'sqlite3', :platform => [:ruby, :mswin]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ For example to initiate a ListRecords request to pubmed you can:
2626

2727
```ruby
2828
require 'oai'
29-
client = OAI::Client.new 'http://www.pubmedcentral.gov/oai/oai.cgi'
29+
client = OAI::Client.new 'http://www.pubmedcentral.gov/oai/oai.cgi', :headers => { "From" => "[email protected]" }
3030
response = client.list_records
3131
# Get the first page of records
3232
response.each do |record|

lib/oai/client.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def initialize(base_url, options={})
8787
@base = URI.parse base_url
8888
@debug = options.fetch(:debug, false)
8989
@parser = options.fetch(:parser, 'rexml')
90+
@headers = options.fetch(:headers, {})
9091

9192
@http_client = options.fetch(:http) do
9293
Faraday.new(:url => @base.clone) do |builder|
@@ -258,7 +259,11 @@ def load_document(xml)
258259

259260
# Do the actual HTTP get, following any temporary redirects
260261
def get(uri)
261-
response = @http_client.get uri
262+
response = @http_client.get do |req|
263+
req.url uri
264+
req.headers.merge! @headers
265+
end
266+
262267
response.body
263268
end
264269

test/client/tc_http_client.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
require 'webrick'
33

44
class HttpClientTest < Test::Unit::TestCase
5+
def test_user_agent_and_from_headers
6+
faraday_stub = Faraday.new do |builder|
7+
builder.adapter :test do |stub|
8+
stub.get('/echo') { |env| [200, {}, Marshal.dump(env)] }
9+
end
10+
end
11+
12+
client = OAI::Client.new 'http://localhost:3333/oai', :headers => { 'From' => '[email protected]', 'User-Agent' => 'ruby-oai' }, :http => faraday_stub
13+
14+
response = client.send(:get, '/echo')
15+
env = Marshal.load(response)
16+
17+
assert_equal('[email protected]', env.request_headers['From'])
18+
assert_equal('ruby-oai', env.request_headers['User-Agent'])
19+
end
520

621
def test_pluggable_http_client
722
oai_response = <<-eos

0 commit comments

Comments
 (0)