Sha256: 6b2135042facb9d17850881d228847d337a2a2db916e9275d98302391d16d03a

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transistor-client-0.0.2 lib/transistor/client.rb
transistor-client-0.0.1 lib/transistor/client.rb