module PMP::Connection

Constants

ALLOWED_CONNECTION_OPTIONS

Public Instance Methods

add_request_auth(opts, faraday) click to toggle source
# File lib/pmp/connection.rb, line 33
def add_request_auth(opts, faraday)
  if opts[:basic_auth] && opts[:user] && opts[:password]
    faraday.request :basic_auth, opts[:user], opts[:password]
  elsif opts[:oauth_token]
    faraday.request :authorization, opts[:token_type], opts[:oauth_token]
  end
end
connection(options={}) click to toggle source
# File lib/pmp/connection.rb, line 17
def connection(options={})
  opts = process_options(options)
  Faraday::Connection.new(opts) do |faraday|

    add_request_auth(options, faraday)

    [:multipart, :url_encoded].each{|mw| faraday.request(mw) }

    [:mashify, :json, :raise_error].each{|mw| faraday.response(mw) }

    faraday.response :logger if options[:debug]

    faraday.adapter options[:adapter]
  end
end
process_options(opts={}) click to toggle source
# File lib/pmp/connection.rb, line 41
def process_options(opts={})
  headers = opts.delete(:headers) || {}
  options = {
    :headers => {
      # generic http headers
      'User-Agent'   => opts[:user_agent],
      'Accept'       => "application/vnd.collection.doc+json",
      'Content-Type' => "application/vnd.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