Class: ElvantoAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/elvanto/client.rb

Constant Summary collapse

DEFAULTS =
{
  :scheme => 'http',
  :host => 'localhost',
  :api_version => nil,
  :port => 3000,
  :version => '1',
  :logging_level => 'WARN',
  :connection_timeout => 60,
  :read_timeout => 60,
  :logger => nil,
  :ssl_verify => false,
  :faraday_adapter => Faraday.default_adapter,
  :accept_type => 'application/json'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



28
29
30
31
# File 'lib/elvanto/client.rb', line 28

def initialize(options={})
  @config = DEFAULTS.merge options
  build_conn
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



26
27
28
# File 'lib/elvanto/client.rb', line 26

def access_token
  @access_token
end

#configObject

Returns the value of attribute config.



26
27
28
# File 'lib/elvanto/client.rb', line 26

def config
  @config
end

#connObject (readonly)

Returns the value of attribute conn.



25
26
27
# File 'lib/elvanto/client.rb', line 25

def conn
  @conn
end

Instance Method Details

#api_versionObject



80
81
82
83
# File 'lib/elvanto/client.rb', line 80

def api_version
  return "" unless ElvantoAPI.config[:api_version] 
  ElvantoAPI.config[:api_version] + "/"
end

#build_connObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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 = {}) ⇒ Object



90
91
92
# File 'lib/elvanto/client.rb', line 90

def get(href, options={})
  conn.get href, options
end

#post(href, options = {}) ⇒ Object



85
86
87
88
# File 'lib/elvanto/client.rb', line 85

def post(href, options={})
  uri = api_version  + href + "." + config[:accept]
  conn.post uri, options
end

#urlObject

Building the host url of the API Endpoint



75
76
77
78
# 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