Class: AbenityRuby::ApiClient

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

Constant Summary collapse

PUBLIC_KEY =
"-----BEGIN RSA PUBLIC KEY-----\n" +
"MIGJAoGBALw1VRlS2vYgeIWhjyz+oUaZk4h7AC+BLgUxdZnzVBzyWQCVmv17XvXG\n" +
"fg+Aqv5Ltix8K37VcrKjleasnJo1R79pGm0H3c0WfpGgXMTGB+mSijFwelZw/L4d\n" +
"vl7SvA8MEDrN/KthGPy4r/UeV4USvi/y78ducmaIWg0naF9lefpDAgMBAAE=\n" +
"-----END RSA PUBLIC KEY-----"

Instance Method Summary collapse

Constructor Details

#initialize(api_username, api_password, api_key, version = 2, environment = 'live', timeout = 10) ⇒ ApiClient

Public: Initialize an API Client

api_username - A String of the API username api_password - A String of the API password api_key - A String of the API key version - An Integer naming the API version number environment - A String naming the environment timeout - An Integer specifiying the timeout on a request



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/abenity_ruby.rb', line 26

def initialize(api_username, api_password, api_key, version = 2, environment = 'live', timeout = 10)
  @api_username = api_username
  @api_password = api_password
  @api_key = api_key
  @version = version

  if environment == 'live'
    @api_url = 'https://api.abenity.com'
  else
    @api_url = 'https://sandbox.abenity.com'
  end

  @timeout = timeout

  @encrypt_cipher = OpenSSL::Cipher.new("DES3")
  @encrypt_cipher.encrypt

  @triple_des_key = @encrypt_cipher.random_key
  @triple_des_iv = @encrypt_cipher.random_iv
end

Instance Method Details

#deactivate_member(client_user_id, send_notification = 'false') ⇒ Object

Public: Deactivate a Member

client_user_id - The unique Client User ID for the member send_notification - Set to true to send a notification email

Returns the raw API response



122
123
124
125
126
127
128
129
# File 'lib/abenity_ruby.rb', line 122

def deactivate_member(client_user_id, send_notification = 'false')
  data = {
    'client_user_id' => client_user_id,
    'send_notification' => send_notification
  }

  return send_request('/deactivate_member.json', 'POST', data)
end

#reactivate_member(client_user_id, send_notification = 'false') ⇒ Object

Public: Reactivate a Member

client_user_id - The unique Client User ID for the member send_notification - Set to true to send a notification email

Returns the raw API response



137
138
139
140
141
142
143
144
# File 'lib/abenity_ruby.rb', line 137

def reactivate_member(client_user_id, send_notification = 'false')
  data = {
    'client_user_id' => client_user_id,
    'send_notification' => send_notification
  }

  return send_request('/reactivate_member.json', 'POST', data)
end

#send_request(api_method, http_method = 'GET', data = nil) ⇒ Object

Public: Send a HTTP request to the API

api_method - The API method to be called http_method - The HTTP method to be used (GET, POST, PUT, DELETE, etc.) data - Any data to be sent to the API

Returns a data-object of the response



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/abenity_ruby.rb', line 54

def send_request(api_method, http_method = 'GET', data = nil)
  if data.is_a?(Hash)
    data['api_username'] = @api_username
    data['api_password'] = @api_password
    data['api_key'] = @api_key

    post_data = data.map{|k,v| "#{CGI::escape(k)}=#{CGI::escape(v)}"}.join('&')
  else
    post_data = sprintf(
        "api_username=%s&api_password=%s&api_key=%s&%s",
        CGI::escape(@api_username),
        CGI::escape(@api_password),
        CGI::escape(@api_key),
        data
    )
  end

  uri = URI.parse("#{@api_url}/v#{@version}/client#{api_method}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = http_method == 'GET' ? Net::HTTP::Get.new(uri.request_uri) : Net::HTTP::Post.new(uri.request_uri)
  request.body = !data.nil? ? post_data : ''

  request.initialize_http_header({
    "User-Agent" => "abenity/abenity-ruby v1"
  })

  response = http.request(request)

  return parse_response(response.body)
end

#sso_member(member_profile, private_key) ⇒ Object

Public: Single Sign-On a member

member_profile - A hash of key/value pairs that describes the member private_key - Your RSA private key, used to sign your message

Returns the raw API response



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/abenity_ruby.rb', line 94

def sso_member(member_profile, private_key)
  # Convert member profile hash to a HTTP query string
  payload_string = member_profile.map{|k,v| "#{k}=#{v}"}.join('&')

  # URL encode and Base 64 encode the IV
  iv_urlencoded = "#{CGI::escape(Base64.strict_encode64(@triple_des_iv))}decode"

  payload = encrypt_payload(payload_string, @triple_des_iv)
  cipher = encrypt_cipher(@triple_des_key)
  signature = sign_message(payload, private_key)

  data = sprintf(
      "Payload=%s&Cipher=%s&Signature=%s&Iv=%s",
      payload,
      cipher,
      signature,
      iv_urlencoded
  )

  return send_request('/sso_member.json', 'POST', data)
end