Class: Smarteru::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @account_api_key = options[:account_api_key] || ENV['SMARTERU_ACCOUNT_API_KEY']
  @user_api_key = options[:user_api_key] || ENV['SMARTERU_USER_API_KEY']
  @use_ssl = options[:use_ssl] || true
  @verify_ssl = options[:verify_ssl]
  @ssl_ca_file = options[:ssl_ca_file]
  @api_url = (@use_ssl ? 'https' : 'http') + "://#{API_HOST}/#{API_VERSION}/"
  @fail_on_error = options[:fail_on_error] != false
end

Instance Attribute Details

#account_api_keyObject

Returns the value of attribute account_api_key.



3
4
5
# File 'lib/smarteru/client.rb', line 3

def 
  @account_api_key
end

#api_urlObject (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_errorObject (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_fileObject (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_sslObject (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_keyObject

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_sslObject (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

#usersObject



51
52
53
# File 'lib/smarteru/client.rb', line 51

def users
  @users ||= Resources::Users.new(self)
end