lib/evrythng/connection.rb in evrythng-0.0.5 vs lib/evrythng/connection.rb in evrythng-0.1.0

- old
+ new

@@ -1,39 +1,38 @@ -require 'faraday_middleware' -require 'faraday/request/multipart_with_file' -require 'faraday/request/gateway' -require 'faraday/request/evrythng_oauth' -require 'faraday/request/basic_authentication' -require 'faraday/response/raise_http_4xx' -require 'faraday/response/raise_http_5xx' +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 - # @private module Connection private - def connection(format=format) - options = { + # Returns a Faraday::Connection object + # + # @param options [Hash] A hash of options + # @return [Faraday::Connection] + def connection(options={}) + default_options = { :headers => { - :accept => "application/#{format}", - :user_agent => user_agent + :accept => 'application/vnd.evrythng-v2+json', + :user_agent => user_agent, }, :proxy => proxy, :ssl => {:verify => false}, - :url => api_endpoint, + :url => options.fetch(:endpoint, endpoint), } - - Faraday.new(options) do |builder| - builder.use Faraday::Request::MultipartWithFile - # builder.use Faraday::Request::EvrythngOAuth, authentication if authenticated? - builder.use Faraday::Request::BasicAuthentication, username, password + Faraday.new(default_options.deep_merge(connection_options)) do |builder| builder.use Faraday::Request::Multipart builder.use Faraday::Request::UrlEncoded - builder.use Faraday::Request::Gateway, gateway if gateway - builder.use Faraday::Response::RaiseHttp4xx - builder.use Faraday::Response::Mashify - builder.use Faraday::Response::ParseJson - builder.use Faraday::Response::RaiseHttp5xx + 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