lib/ticketing_hub/connection.rb in ticketinghub-0.0.2 vs lib/ticketing_hub/connection.rb in ticketinghub-0.0.3

- old
+ new

@@ -14,39 +14,42 @@ 501 => TicketingHub::NotImplemented, 502 => TicketingHub::BadGateway, 503 => TicketingHub::ServiceUnavailable } - def on_complete(response) + def on_complete response key = response[:status].to_i raise ERROR_MAP[key].new(response) if ERROR_MAP.has_key? key end end - # @private module Connection private - def connection(options={}) + + def connection options={} + token = options.delete(:access_token) || options.delete(:oauth_token) || oauth_token + options = { - authenticate: true, + authenticate: !token.nil?, force_urlencoded: false, - raw: false, - ssl: { verify: false } + raw: false, + accept: 'application/json', + user_agent: user_agent }.merge(options) options.merge! proxy: proxy unless proxy.nil? - # TODO: Don't build on every request - Faraday.new(options) do |builder| - builder.request options[:force_urlencoded] ? :url_encoded : :json - builder.use ErrorHandler - builder.use FaradayMiddleware::FollowRedirects - builder.use FaradayMiddleware::ParseJson, content_type: /\bjson$/ - faraday_config_block.call builder if faraday_config_block - builder.adapter *adapter - end.tap do |connection| - connection.headers[:user_agent] = user_agent - connection.basic_auth authentication[:token], authentication[:secret] + Faraday.new(options) do |conn| + conn.request :oauth2, token unless token.nil? + conn.request options[:force_urlencoded] ? :url_encoded : :json + + conn.use ErrorHandler + conn.response :follow_redirects + conn.response :mashify + conn.response :json, content_type: /\bjson$/ + + faraday_config_block.call conn if faraday_config_block + conn.adapter *adapter end end end end \ No newline at end of file