Class: LoyaltyLabSDK::LoyaltyAPI

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

Constant Summary collapse

ENDPOINT =
'https://api.loyaltylab.com/loyaltyapi/loyaltyapi.asmx'
NAMESPACE =
'http://www.loyaltylab.com/loyaltyapi/'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LoyaltyAPI

Constructs an object-oriented API to loyalty lab.

In addition to the options specified below, any of the options documented in LoyaltyLabSDK#config may be overridden.

It will establish a connection, authenticate the username/password, and store the authentication data upon construction unless the lazy_authentication parameter is set. This authentication token times out after 20 minutes (double-check Loyalty Lab’s documentation), so a client wishing to use this object should accommodate that, and re-call #authenticate! if necessary.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :lazy_authentication (boolean) — default: false

    Delays authentication until either the first call is made or it is done explicitly.

  • :allow_reauthenticate (boolean) — default: true

    If true, will attempt to re-authenticate the client once automatically if an AuthenticationError is thrown (on any call).



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/loyalty_lab_sdk/loyalty_api.rb', line 29

def initialize(options = {})
  self.config = {
    :lazy_authentication => false,
    :allow_reauthenticate => true,
  }.merge!(LoyaltyLabSDK.config).merge!(options)

  Savon.configure do |c|
    c.logger = config[:logger]
    c.raise_errors = false
  end

  initialize_client

  @authenticated = false

  unless config[:lazy_authentication]
    authenticate!
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Implements most API calls. The API method should be passed as defined in Loyalty Lab’s API documentation here: api.loyaltylab.com/loyaltyapi/help/index.html

Each of the parameters should be passed in a hash as documented.

Responses will be in the documented format (a “string” return value with respond with a string, etc…), or if it’s an object (such as a Shopper object), the result will be a hash with string keys for object

Examples:

Retrieve a shopper

shopper = api.GetShopperByEmail 'email' => '[email protected]'

Adjust shopper point balance

point_balance = api.AdjustShopperPoints 'shopperId' => shopper['ShopperId'],
 'pointChange' => 1000,
 'pointType' => 'Base',
 'description' => 'Bonus'

Create a shopper with card

new_shopper = api.build_default_shopper(1234)
new_shopper['EmailAddress'] = '[email protected]'
new_shopper['FirstName'] = 'Joe'
new_shopper['LastName'] = 'Schmoe'
new_card = api.build_default_card(1234)
api.CreateShopperWithCard 'shopper' => new_shopper, 'card' => new_card


115
116
117
# File 'lib/loyalty_lab_sdk/loyalty_api.rb', line 115

def method_missing(method_name, *args)
  call_api_method(method_name, *args)
end

Instance Method Details

#authenticate!Object

Authenticates the client.

This method is called by the constructor unless lazy_authentication is on, so this will not typically need to be invoked directly.

An object that hasn’t been called in 20 minutes will have its authentication headers expired by Loyalty Lab, requiring this method to be invoked again on long-lived objects.



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/loyalty_lab_sdk/loyalty_api.rb', line 57

def authenticate!
  response = self.AuthenticateUser(
    { :username => config[:username], :password => config[:password] },
    :is_authenticate => true,
    :allow_reauthenticate => false)

  auth_data = {
    :retailer_guid => response['RetailerGuid'],
    :authenticated => response['Authenticated'],
    :token => response['Token'],
    :ics_user_id => response['ICSUserID']
  }

  if !auth_data[:authenticated]
    raise AuthenticationError, 'authentication failed'
  end

  self.retailer_guid = auth_data[:retailer_guid]

  self.auth_header = {
    "wsdl:AuthenticationResult" => {
      "wsdl:RetailerGuid" => auth_data[:retailer_guid],
      "wsdl:Authenticated" => auth_data[:authenticated],
      "wsdl:Token" => auth_data[:token],
      "wsdl:ICSUserID" => auth_data[:ics_user_id],
    }
  }

  @authenticated = true
end

#build_default_card(retailer_shopper_id) ⇒ Object

Initializes and returns a card object with default fields set for use with a CreateShopper call.

This object should be updated with all relevant fields before being saved (if necessary).



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/loyalty_lab_sdk/loyalty_api.rb', line 168

def build_default_card(retailer_shopper_id)
  now = DateTime.now

  {
    'RegisteredCardId' => 0,
    'ShopperId' => 0,
    'CommonName' => 'loyalty member id',
    'AlternateCardIdentifier' => retailer_shopper_id.to_s,
    'CardType' => 'L',
    'ExpirationMonth' => 12,
    'ExpirationYear' => 3010,
    'LastFour' => ' ',
    'CardHolderName' => ' ',
    'Status' => 'A',
    'CreateDateTime' => now,
    'IsPreferred' => ' ',
    'FileImportId' => 0
  }
end

#build_default_shopper(retailer_shopper_id) ⇒ Object

Initializes and returns a shopper object with default fields set for use with a CreateShopper call.

This object should be updated with all relevant fields (ie. email address, first name, phone number, etc) before being saved.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/loyalty_lab_sdk/loyalty_api.rb', line 124

def build_default_shopper(retailer_shopper_id)
  now = DateTime.now

  {
    'ShopperId' => 0,
    'RetailerGUID' => retailer_guid,
    'EmailAddress' => '',
    'EmailFrequency' => 1,
    'EmailFrequencyUnit' => 'D',
    'EmailFormat' => 'HTML',
    'Password' => ' ',
    'Status' => 'A',
    'LastName' => '',
    'MiddleInitial' => '',
    'FirstName' => '',
    'Address1' => '',
    'City' => '',
    'State' => '',
    'Zip' => '',
    'PhoneNumber' => '',
    'ProfileCreateDateTime' => now,
    'ProfileUpdateDateTime' => now,
    'CreateDateTime' => now,
    'PasswordLastChanged' => now,
    'Origin' => 'W',
    'RetailerShopperId' => retailer_shopper_id.to_s,
    'FileImportId' => 0,
    'BulkEmail' => 1,
    'LoyaltyMember' => true,
    'PersonStatus' => 'P',
    'RetailerRegistered' => false,
    'MailOptIn' => false,
    'PhoneOptIn' => false,
    'RetailerShopperCreationDate' => now,
    'LoyaltyLabCreateDateTime' => now,
    'StatusUpdateDateTime' => now
  }
end