Sha256: 6cc06af47e4cfcceb83f83eb39f9e9079b8f417f08067272bb04500d3d3f156b

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

# -*- encoding: utf-8 -*-

require 'faraday_middleware'
require 'active_support'

module PMP
  module Connection

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

    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

    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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pmp-0.1.3 lib/pmp/connection.rb
pmp-0.1.2 lib/pmp/connection.rb
pmp-0.1.1 lib/pmp/connection.rb
pmp-0.1.0 lib/pmp/connection.rb