Class: Elk::Client

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

Constant Summary collapse

BASE_DOMAIN =

Base domain for 46elks API

"api.46elks.com"
API_VERSION =

API version supported

"a1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters = {}) ⇒ Client

Returns a new instance of Client.



17
18
19
20
# File 'lib/elk/client.rb', line 17

def initialize(parameters = {})
  @username = parameters[:username]
  @password = parameters[:password]
end

Instance Attribute Details

#base_domainObject

Used to overrid Elk::BASE_DOMAIN (in tests)



15
16
17
# File 'lib/elk/client.rb', line 15

def base_domain
  @base_domain
end

#passwordObject

API Password from 46elks.com



13
14
15
# File 'lib/elk/client.rb', line 13

def password
  @password
end

#usernameObject

API Username from 46elks.com



11
12
13
# File 'lib/elk/client.rb', line 11

def username
  @username
end

Instance Method Details

#base_urlObject

Base URL used for calling 46elks API



34
35
36
37
38
39
40
# File 'lib/elk/client.rb', line 34

def base_url
  unless username && password
    raise AuthError, "API username and password required"
  end

  "https://#{username}:#{password}@#{(base_domain || BASE_DOMAIN)}/#{API_VERSION}"
end

#configure {|_self| ... } ⇒ Object

Set authentication credentials

client = Elk::Client.new
client.configure do |config|
  config.username = "USERNAME"
  config.password = "PASSWORD"
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Elk::Client)

    the object that the method was called on



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

def configure
  yield self
end

#execute(method, path, parameters, headers = { accept: :json }, &block) ⇒ Object

Wrapper around RestClient::RestClient.execute

  • Sets accept header to json

  • Handles some exceptions



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/elk/client.rb', line 57

def execute(method, path, parameters, headers = { accept: :json }, &block)
  payload = {}.merge(parameters)
  url = base_url + path

  request_arguments = {
    method:  method,
    url:     url,
    payload: payload,
    headers: headers
  }

  RestClient::Request.execute(request_arguments, &block)
rescue RestClient::Unauthorized
  raise AuthError, "Authentication failed"
rescue RestClient::InternalServerError
  raise ServerError, "Server error"
rescue RestClient::Forbidden => e
  raise BadRequest, e.http_body
end

#get(path, parameters = {}) ⇒ Object

Wrapper for Elk.execute(:get)



43
44
45
# File 'lib/elk/client.rb', line 43

def get(path, parameters = {})
  execute(:get, path, parameters)
end

#post(path, parameters = {}) ⇒ Object

Wrapper for Elk::execute(:post)



48
49
50
# File 'lib/elk/client.rb', line 48

def post(path, parameters = {})
  execute(:post, path, parameters)
end