module Doshii::Connection

Constants

URL

Protected Instance Methods

http_connection() click to toggle source
# File lib/doshii/connection.rb, line 12
def http_connection
  @http_connection ||=
    Faraday.new("#{URL % { subdomain: subdomain }}/#{version}/") do |faraday|
      # faraday.response :logger
      faraday.adapter  Faraday.default_adapter
      faraday.use      Faraday::Response::ParseJson

      faraday.headers['Authorization'] = "Basic #{base64_encoded_key}"
      faraday.headers['Content-Type']  = 'application/json'
      faraday.ssl['verify']            = verify_ssl
    end
end
request(method, url, query = {}) { |body| ... } click to toggle source
# File lib/doshii/connection.rb, line 25
def request(method, url, query = {}, &block)
  body = Hash.new
  yield body if block_given?
  response = http_connection.send(method) do |req|
    req.url url, query
    req.body = JSON.generate(body)
  end
  raise Doshii::AuthenticationError.new(response.body) if response.status == 401
  response
rescue Faraday::ConnectionFailed => e
  raise Doshii::ConnectionError.new(e)
end

Private Instance Methods

base64_encoded_key() click to toggle source
# File lib/doshii/connection.rb, line 40
def base64_encoded_key
  key = "#{client_id}:#{client_secret}"
  Base64.urlsafe_encode64 key
end