class Smarteru::Client

Attributes

account_api_key[RW]
api_url[R]
fail_on_error[R]
ssl_ca_file[R]
use_ssl[R]
user_api_key[RW]
verify_ssl[R]

Public Class Methods

new(options = {}) click to toggle source

Create an instance of an API client

Attributes

Example

client = Smarteru::Client.new({account_api_key: 'abc', user_api_key: 'abc'})

# 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

Public Instance Methods

request(operation, data) click to toggle source

Make an API request

Attributes

  • operation - Operation method eg getGroup

  • data - Data hash

Example

client.request(“getGroup”, {

group: {
  name: 'MyGroup'
}

})

# 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
users() click to toggle source
# File lib/smarteru/client.rb, line 51
def users
  @users ||= Resources::Users.new(self)
end

Private Instance Methods

body(operation, param_data) click to toggle source

Build xml request body

Attributes

  • operation - Operation method

  • parameters - Parameters hash

Example

client.body(“createUser”, {email: 'email@example.com', …})

# File lib/smarteru/client.rb, line 64
def body(operation, param_data)
  %{<?xml version="1.0" encoding="UTF-8"?>
    <SmarterU>
    <AccountAPI>#{account_api_key}</AccountAPI>
    <UserAPI>#{user_api_key}</UserAPI>
    <Method>#{operation}</Method>
    <Parameters>#{body_parameters(param_data)}</Parameters>
    </SmarterU>}
end
body_parameters(parameters) click to toggle source

Build body parameteres xml

Attributes

  • parameters - Parameters hash

# File lib/smarteru/client.rb, line 78
def body_parameters(parameters)
  parameters_xml = ''
  parameters.each_pair do |k, v|
    key = parameter_key(k)

    val = case v
    when Hash
      body_parameters(v)
    when Array
      v.map { |i| body_parameters(i) }.join('')
    when nil
      ''
    else
      "<![CDATA[#{v}]]>"
    end

    parameters_xml << "<#{key}>#{val}</#{key}>"
  end

  parameters_xml
end
parameter_key(term) click to toggle source

Prepare parameter key

Attributes

  • parameters - Parameters hash

# File lib/smarteru/client.rb, line 104
def parameter_key(term)
  string = term.to_s
  string = string.sub(/^[a-z\d]*/) { $&.capitalize }
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
  string
end