Class: Smarteru::Client
- Inherits:
-
Object
- Object
- Smarteru::Client
- Defined in:
- lib/smarteru/client.rb
Instance Attribute Summary collapse
-
#account_api_key ⇒ Object
Returns the value of attribute account_api_key.
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#fail_on_error ⇒ Object
readonly
Returns the value of attribute fail_on_error.
-
#ssl_ca_file ⇒ Object
readonly
Returns the value of attribute ssl_ca_file.
-
#use_ssl ⇒ Object
readonly
Returns the value of attribute use_ssl.
-
#user_api_key ⇒ Object
Returns the value of attribute user_api_key.
-
#verify_ssl ⇒ Object
readonly
Returns the value of attribute verify_ssl.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Client
constructor
Create an instance of an API client.
-
#request(operation, data) ⇒ Object
Make an API request.
- #users ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Create an instance of an API client
Attributes
-
options
- Access credentials and options hash, required keys are: account_api_key, user_api_key
Example
client = Smarteru::Client.new(‘abc’, user_api_key: ‘abc’)
12 13 14 15 16 17 18 19 20 |
# File 'lib/smarteru/client.rb', line 12 def initialize( = {}) @account_api_key = [:account_api_key] || ENV['SMARTERU_ACCOUNT_API_KEY'] @user_api_key = [:user_api_key] || ENV['SMARTERU_USER_API_KEY'] @use_ssl = [:use_ssl] || true @verify_ssl = [:verify_ssl] @ssl_ca_file = [:ssl_ca_file] @api_url = (@use_ssl ? 'https' : 'http') + "://#{API_HOST}/#{API_VERSION}/" @fail_on_error = [:fail_on_error] != false end |
Instance Attribute Details
#account_api_key ⇒ Object
Returns the value of attribute account_api_key.
3 4 5 |
# File 'lib/smarteru/client.rb', line 3 def account_api_key @account_api_key end |
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
4 5 6 |
# File 'lib/smarteru/client.rb', line 4 def api_url @api_url end |
#fail_on_error ⇒ Object (readonly)
Returns the value of attribute fail_on_error.
4 5 6 |
# File 'lib/smarteru/client.rb', line 4 def fail_on_error @fail_on_error end |
#ssl_ca_file ⇒ Object (readonly)
Returns the value of attribute ssl_ca_file.
4 5 6 |
# File 'lib/smarteru/client.rb', line 4 def ssl_ca_file @ssl_ca_file end |
#use_ssl ⇒ Object (readonly)
Returns the value of attribute use_ssl.
4 5 6 |
# File 'lib/smarteru/client.rb', line 4 def use_ssl @use_ssl end |
#user_api_key ⇒ Object
Returns the value of attribute user_api_key.
3 4 5 |
# File 'lib/smarteru/client.rb', line 3 def user_api_key @user_api_key end |
#verify_ssl ⇒ Object (readonly)
Returns the value of attribute verify_ssl.
4 5 6 |
# File 'lib/smarteru/client.rb', line 4 def verify_ssl @verify_ssl end |
Instance Method Details
#request(operation, data) ⇒ Object
Make an API request
Attributes
-
operation
- Operation method eg getGroup -
data
- Data hash
Example
client.request(“getGroup”, {
group: {
name: 'MyGroup'
}
})
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/smarteru/client.rb', line 33 def request(operation, data) opts = { method: :post, url: api_url, payload: { 'Package' => body(operation, data) }, content_type: :xml, verify_ssl: verify_ssl, ssl_ca_file: ssl_ca_file } response = RestClient::Request.execute(opts) response = Response.new(response) if !response.success? && fail_on_error fail Error.new(response) end response end |