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
-
.base_domain ⇒ Object
Defaults to Elk::BASE_DOMAIN, but can be overriden for testing.
-
.password ⇒ Object
API Password from 46elks.com.
-
.username ⇒ Object
API Username from 46elks.com.
Class Method Summary collapse
-
.base_url ⇒ Object
Base URL used for calling 46elks API.
-
.configure {|_self| ... } ⇒ Object
Set up authentication credentials, has to be done before using Elk::Number and Elk::SMS.
-
.execute(method, path, parameters, headers = {:accept => :json}, &block) ⇒ Object
Wrapper around RestClient::RestClient.execute.
-
.get(path, parameters = {}) ⇒ Object
Wrapper for Elk.execute(:get).
-
.parse_json(body) ⇒ Object
Wrapper around MultiJson.load, symbolize names.
-
.post(path, parameters = {}) ⇒ Object
Wrapper for Elk::execute(:post).
Class Attribute Details
.base_domain ⇒ Object
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 |
.password ⇒ Object
API Password from 46elks.com
30 31 32 |
# File 'lib/elk.rb', line 30 def password @password end |
.username ⇒ Object
API Username from 46elks.com
28 29 30 |
# File 'lib/elk.rb', line 28 def username @username end |
Class Method Details
.base_url ⇒ Object
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
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:: 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 |