require 'faraday' require 'faraday_middleware' require 'transistor/middleware/authentication' require 'transistor/error' module Transistor class Client def connection_options @collection_options ||= { builder: middleware, headers: { accept: 'application/json', user_agent: Transistor.config.user_agent }, request: { open_timeout: 5, timeout: 10 } } end def middleware @middleware = Faraday::Builder.new do |builder| builder.use Faraday::Request::UrlEncoded builder.use Transistor::Middleware::Authentication builder.adapter Faraday.default_adapter end end def delete(path, params = {}) request(:delete, path, params) end def get(path, params = {}) request(:get, path, params) end def post(path, params = {}) request(:post, path, params) end def put(path, params = {}) request(:put, path, params) end private def connection @connection ||= Faraday.new Transistor.config.base_url, connection_options do |conn| conn.response :json, content_type: /\bjson$/ end end def request(method, path, params = {}) response = connection.send(method.to_sym, path, params) do |request| end response.env rescue Faraday::Error::ClientError => error raise Transistor::Error.new(err) end end end