Module: Elk

Defined in:
lib/elk.rb,
lib/elk/sms.rb,
lib/elk/util.rb,
lib/elk/number.rb,
lib/elk/version.rb

Overview

Base module Used for to configure username and password through Elk.configure

Defined Under Namespace

Modules: Util Classes: AuthError, BadRequest, BadResponse, MissingParameter, Number, SMS, ServerError

Constant Summary collapse

BASE_DOMAIN =

Base domain for 46elks API

'api.46elks.com'
API_VERSION =

API version supported

'a1'
VERSION =
'0.0.13'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.base_domainObject

Defaults to Elk::BASE_DOMAIN, but can be overriden for testing



32
33
34
# File 'lib/elk.rb', line 32

def base_domain
  @base_domain
end

.passwordObject

API Password from 46elks.com



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

def password
  @password
end

.usernameObject

API Username from 46elks.com



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

def username
  @username
end

Class Method Details

.base_urlObject

Base URL used for calling 46elks API



45
46
47
48
49
50
51
# File 'lib/elk.rb', line 45

def base_url
  if not username or not password
    raise AuthError, "API username and password required"
  end

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

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

Set up authentication credentials, has to be done before using Elk::Number and Elk::SMS

Elk.configure do |config|
  config.username = 'USERNAME'
  config.password = 'PASSWORD
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Elk)

    the object that the method was called on



40
41
42
# File 'lib/elk.rb', line 40

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



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/elk.rb', line 68

def execute(method, path, parameters, headers={:accept => :json}, &block)
  payload = {}.merge(parameters)
  url = base_url + path
  RestClient::Request.execute(:method => method, :url => url, :payload => payload, :headers => headers, &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)



54
55
56
# File 'lib/elk.rb', line 54

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

.parse_json(body) ⇒ Object

Wrapper around MultiJson.load, symbolize names



81
82
83
84
85
# File 'lib/elk.rb', line 81

def parse_json(body)
  MultiJson.load(body, :symbolize_keys => true)
rescue MultiJson::DecodeError
  raise BadResponse, "Can't parse JSON"
end

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

Wrapper for Elk::execute(:post)



59
60
61
# File 'lib/elk.rb', line 59

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