class ElvantoAPI::Client

Constants

DEFAULTS

Attributes

access_token[RW]
config[RW]
conn[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/elvanto/client.rb, line 28
def initialize(options={})
  @config = DEFAULTS.merge options
  build_conn
end

Public Instance Methods

api_version() click to toggle source
# File lib/elvanto/client.rb, line 80
def api_version
  return "" unless ElvantoAPI.config[:api_version] 
  ElvantoAPI.config[:api_version] + "/"
end
build_conn() click to toggle source
# File lib/elvanto/client.rb, line 33
def build_conn
  if config[:logger]
    logger = config[:logger]
  else
    logger = Logger.new(STDOUT)
    logger.level = Logger.const_get(config[:logging_level].to_s)
  end

  Faraday::Response.register_middleware :handle_elvanto_errors => lambda { Faraday::Response::RaiseElvantoError }

  options = {
    :request => {
      :open_timeout => config[:connection_timeout],
      :timeout => config[:read_timeout]
    },
    :ssl => {
      :verify => @config[:ssl_verify] # Only set this to false for testing
    }
  }
  @conn = Faraday.new(url, options) do |cxn|
    cxn.request :json

    cxn.response :logger, logger
    cxn.response :handle_elvanto_errors
    cxn.response :json
    #cxn.response :raise_error # raise exceptions on 40x, 50x responses
    cxn.adapter config[:faraday_adapter]
  end
  conn.path_prefix = '/'
  conn.headers['User-Agent'] = "elvanto-ruby/" + config[:version]

  if config[:access_token]
    # Authenticating with OAuth
    conn.headers["Authorization"]  = "Bearer " + config[:access_token] 
  elsif config[:api_key]
    # Authenticating with an API key
    conn.basic_auth(config[:api_key], '')
  end

end
get(href, options={}) click to toggle source
# File lib/elvanto/client.rb, line 90
def get(href, options={})
  conn.get href, options
end
post(href, options={}) click to toggle source
# File lib/elvanto/client.rb, line 85
def post(href, options={})
  uri = api_version  + href + "." + config[:accept]
  conn.post uri, options
end
url() click to toggle source

Building the host url of the API Endpoint

# File lib/elvanto/client.rb, line 75
def url
  builder = (config[:scheme] == 'http') ? URI::HTTP : URI::HTTPS
  builder.build({:host => config[:host],:port => config[:port],:scheme => config[:scheme]})
end