Sha256: 87ca38221690e4f59e5e768281912aba0353cd5e16b653f447e1d4ff9eecb862

Contents?: true

Size: 1.55 KB

Versions: 10

Compression:

Stored size: 1.55 KB

Contents

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

require 'faraday_middleware'
require 'active_support'

module PMP
  module Connection

    ALLOWED_CONNECTION_OPTIONS = [
      :headers,
      :url,
      :params,
      :request,
      :ssl,
      :proxy
    ].freeze

    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

    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

    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

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pmp-0.5.7 lib/pmp/connection.rb
pmp-0.5.6 lib/pmp/connection.rb
pmp-0.5.5 lib/pmp/connection.rb
pmp-0.5.4 lib/pmp/connection.rb
pmp-0.5.3 lib/pmp/connection.rb
pmp-0.5.2 lib/pmp/connection.rb
pmp-0.5.1 lib/pmp/connection.rb
pmp-0.5.0 lib/pmp/connection.rb
pmp-0.4.1 lib/pmp/connection.rb
pmp-0.4.0 lib/pmp/connection.rb