Module: PMP::Connection

Included in:
CollectionDocument
Defined in:
lib/pmp/connection.rb

Constant Summary collapse

ALLOWED_CONNECTION_OPTIONS =
[
  :headers,
  :url,
  :params,
  :request,
  :adapter,
  :ssl,
  :oauth_token,
  :debug
].freeze

Instance Method Summary collapse

Instance Method Details

#connection(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pmp/connection.rb', line 20

def connection(options={})
  opts = process_options(options)
  Faraday::Connection.new(opts) do |faraday|
    faraday.request :authorization, 'Bearer', opts[:oauth_token] unless opts[:oauth_token].nil?
    faraday.request :url_encoded
    faraday.request :multipart

    faraday.response :mashify
    faraday.response :logger if opts[:debug]
    faraday.response :json

    faraday.adapter opts[:adapter]
  end
end

#process_options(opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pmp/connection.rb', line 35

def process_options(opts={})
  headers = opts.delete(:headers) || {}
  options = {
    :headers => {
      # generic http headers
      'User-Agent'   => opts[:user_agent],
      'Accept'       => "application/vnd.pmp.collection.doc+json",
      'Content-Type' => "application/vnd.pmp.collection.doc+json"
    },
    :ssl => {:verify => false},
    :url => opts[:endpoint]
  }.merge(opts)
  options[:headers] = options[:headers].merge(headers)

  # clean out any that don't belong
  options.select{|k,v| ALLOWED_CONNECTION_OPTIONS.include?(k.to_sym)}
end