Sha256: efbaad23dfbd4e8ecd4bf3b30007fdb49a09b109d802ea6c598985a0dad24309

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

require 'faraday'
require 'evrythng/core_ext/hash'
require 'evrythng/request/gateway'
require 'evrythng/request/token_authentication'
require 'evrythng/response/parse_json'
require 'evrythng/response/raise_client_error'
require 'evrythng/response/raise_server_error'

module Evrythng
  module Connection
    private

    # Returns a Faraday::Connection object
    #
    # @param options [Hash] A hash of options
    # @return [Faraday::Connection]
    def connection(options={})
      default_options = {
        :headers => {
          :accept => 'application/vnd.evrythng-v2+json',
          :user_agent => user_agent,
        },
        :proxy => proxy,
        :ssl => {:verify => false},
        :url => options.fetch(:endpoint, endpoint),
      }
      Faraday.new(default_options.deep_merge(connection_options)) do |builder|
        builder.use Faraday::Request::Multipart
        builder.use Faraday::Request::UrlEncoded
        builder.use Evrythng::Request::Gateway, gateway if gateway
        builder.use Evrythng::Request::TokenAuthentication, token
        builder.use Evrythng::Response::RaiseClientError
        builder.use Evrythng::Response::ParseJson unless options[:raw]
        builder.use Evrythng::Response::RaiseServerError
        builder.adapter(adapter)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
evrythng-0.1.1 lib/evrythng/connection.rb
evrythng-0.1.0 lib/evrythng/connection.rb