Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/collectionspace/client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Client

attr_reader :config

NAME = "CollectionSpaceClient"

def initialize(config = Configuration.new)
unless config.is_a? CollectionSpace::Configuration
raise CollectionSpace::ArgumentError, "Invalid configuration object"
Expand Down
9 changes: 5 additions & 4 deletions lib/collectionspace/client/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ class Configuration
page_size: 25,
include_deleted: false,
throttle: 0,
timeout: 60,
verbose: false,
verify_ssl: true
}.freeze

attr_accessor :base_uri, :username, :password, :page_size, :include_deleted, :throttle, :verbose, :verify_ssl
attr_accessor :base_uri, :username, :password, :page_size, :include_deleted,
:throttle, :timeout, :verbose, :verify_ssl

def initialize(settings = {})
settings = DEFAULTS.merge(settings)
settings.each do |property, value|
DEFAULTS.merge(settings).each do |property, value|
next unless DEFAULTS.key?(property)

instance_variable_set(:"@#{property}", value)
send(:"#{property}=", value)
end
end
end
Expand Down
34 changes: 9 additions & 25 deletions lib/collectionspace/client/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,22 @@ class Request

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

def default_headers(method = :get)
headers = {
delete: {},
get: {},
post: {
"Content-Type" => "application/xml",
"Content-Length" => "nnnn"
},
put: {
"Content-Type" => "application/xml",
"Content-Length" => "nnnn"
}
}
headers[method]
end
DEFAULT_HEADERS = {"Content-Type" => "application/xml"}.freeze

def initialize(config, method = "GET", path = "", options = {})
@config = config
@method = method.downcase.to_sym
@path = path.gsub(%r{^/}, "")
@path = path.gsub(%r{^/+}, "")

@options = options.dup
@options[:basic_auth] = {username: config.username, password: config.password}

@auth = {
username: config.username,
password: config.password
}
@options[:headers] = DEFAULT_HEADERS.merge(@options.fetch(:headers, {}))
@options[:headers]["User-Agent"] = "#{Client::NAME}/#{Client::VERSION}"

headers = default_headers(@method).merge(options.fetch(:headers, {}))
@options = options
@options[:basic_auth] = @auth
@options[:headers] = headers
@options[:query] = @options.fetch(:query, {})
@options[:timeout] = config.timeout
@options[:verify] = config.verify_ssl
@options[:query] = options.fetch(:query, {})

self.class.base_uri config.base_uri
self.class.debug_output $stdout if config.verbose
Expand Down
1 change: 1 addition & 0 deletions spec/collectionspace/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
expect(config.page_size).to eq 25
expect(config.include_deleted).to eq false
expect(config.throttle).to eq 0
expect(config.timeout).to eq 60
expect(config.verbose).to eq false
expect(config.verify_ssl).to eq true
end
Expand Down
7 changes: 7 additions & 0 deletions spec/collectionspace/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
}
end

it "does not mutate the caller's options hash" do
opts = {query: {foo: "bar"}, headers: {"X-Custom" => "1"}}
caller_opts = Marshal.load(Marshal.dump(opts))
CollectionSpace::Request.new(default_config, "GET", "collectionobjects", opts)
expect(opts).to eq(caller_opts)
end

it "can create a collectionobject" do
VCR.use_cassette("request_collectionobjects_create") do
response = client.post("collectionobjects", post_payload)
Expand Down
Loading