Skip to content

Commit d34e007

Browse files
authored
Some overdue cleanup (#65)
Configuration gets a timeout and added to request setup. The timeout matches Net HTTP defaults so should be backwards compat. Configuration uses accessor to set (no poking at internals). Default headers made reasonable, no placeholder values. Request sets basic_auth via options like everything (httparty is weird). Adds a User-Agent for the client. Fix passed in options (a reference) being mutated by Request.
1 parent 803fc97 commit d34e007

5 files changed

Lines changed: 24 additions & 29 deletions

File tree

lib/collectionspace/client/client.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class Client
77

88
attr_reader :config
99

10+
NAME = "CollectionSpaceClient"
11+
1012
def initialize(config = Configuration.new)
1113
unless config.is_a? CollectionSpace::Configuration
1214
raise CollectionSpace::ArgumentError, "Invalid configuration object"

lib/collectionspace/client/configuration.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ class Configuration
1010
page_size: 25,
1111
include_deleted: false,
1212
throttle: 0,
13+
timeout: 60,
1314
verbose: false,
1415
verify_ssl: true
1516
}.freeze
1617

17-
attr_accessor :base_uri, :username, :password, :page_size, :include_deleted, :throttle, :verbose, :verify_ssl
18+
attr_accessor :base_uri, :username, :password, :page_size, :include_deleted,
19+
:throttle, :timeout, :verbose, :verify_ssl
1820

1921
def initialize(settings = {})
20-
settings = DEFAULTS.merge(settings)
21-
settings.each do |property, value|
22+
DEFAULTS.merge(settings).each do |property, value|
2223
next unless DEFAULTS.key?(property)
2324

24-
instance_variable_set(:"@#{property}", value)
25+
send(:"#{property}=", value)
2526
end
2627
end
2728
end

lib/collectionspace/client/request.rb

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,22 @@ class Request
77

88
attr_reader :config, :headers, :method, :path, :options
99

10-
def default_headers(method = :get)
11-
headers = {
12-
delete: {},
13-
get: {},
14-
post: {
15-
"Content-Type" => "application/xml",
16-
"Content-Length" => "nnnn"
17-
},
18-
put: {
19-
"Content-Type" => "application/xml",
20-
"Content-Length" => "nnnn"
21-
}
22-
}
23-
headers[method]
24-
end
10+
DEFAULT_HEADERS = {"Content-Type" => "application/xml"}.freeze
2511

2612
def initialize(config, method = "GET", path = "", options = {})
2713
@config = config
2814
@method = method.downcase.to_sym
29-
@path = path.gsub(%r{^/}, "")
15+
@path = path.gsub(%r{^/+}, "")
16+
17+
@options = options.dup
18+
@options[:basic_auth] = {username: config.username, password: config.password}
3019

31-
@auth = {
32-
username: config.username,
33-
password: config.password
34-
}
20+
@options[:headers] = DEFAULT_HEADERS.merge(@options.fetch(:headers, {}))
21+
@options[:headers]["User-Agent"] = "#{Client::NAME}/#{Client::VERSION}"
3522

36-
headers = default_headers(@method).merge(options.fetch(:headers, {}))
37-
@options = options
38-
@options[:basic_auth] = @auth
39-
@options[:headers] = headers
23+
@options[:query] = @options.fetch(:query, {})
24+
@options[:timeout] = config.timeout
4025
@options[:verify] = config.verify_ssl
41-
@options[:query] = options.fetch(:query, {})
4226

4327
self.class.base_uri config.base_uri
4428
self.class.debug_output $stdout if config.verbose

spec/collectionspace/configuration_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
expect(config.page_size).to eq 25
1414
expect(config.include_deleted).to eq false
1515
expect(config.throttle).to eq 0
16+
expect(config.timeout).to eq 60
1617
expect(config.verbose).to eq false
1718
expect(config.verify_ssl).to eq true
1819
end

spec/collectionspace/request_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
}
1818
end
1919

20+
it "does not mutate the caller's options hash" do
21+
opts = {query: {foo: "bar"}, headers: {"X-Custom" => "1"}}
22+
caller_opts = Marshal.load(Marshal.dump(opts))
23+
CollectionSpace::Request.new(default_config, "GET", "collectionobjects", opts)
24+
expect(opts).to eq(caller_opts)
25+
end
26+
2027
it "can create a collectionobject" do
2128
VCR.use_cassette("request_collectionobjects_create") do
2229
response = client.post("collectionobjects", post_payload)

0 commit comments

Comments
 (0)