Class: Leal::Customers::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void

Parameters:



9
10
11
# File 'lib/leal/customers/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

#create(request_options: {}, **params) ⇒ Leal::Customers::Types::CreateCustomersResponse

Creates a new customer for the store. Requires first_name and at least one of email or phone. Optionally enroll the customer in a loyalty card by passing card_id, and trigger delivery of card links (email/SMS) by passing send_card_links. When a card with initial stamps is assigned, those stamps are automatically applied as a welcome bonus.

Pass metadata to attach arbitrary key/value data, and external_references to link the customer to records in other systems (e.g. Square, Shopify). External references are upserted by (source, external_id) so this endpoint is safe to call with the same references twice.

Examples:

client.customers.create(
  account_id: 1,
  customer: {
    first_name: "first_name"
  }
)

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :account_id (Integer)

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/leal/customers/client.rb', line 93

def create(request_options: {}, **params)
  params = Leal::Internal::Types::Utils.normalize_keys(params)
  request_data = Leal::Customers::Types::CreateCustomersRequest.new(params).to_h
  non_body_param_names = %w[account_id]
  body = request_data.except(*non_body_param_names)

  request = Leal::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "api/v1/accounts/#{URI.encode_uri_component(params[:account_id].to_s)}/customers",
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Leal::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Leal::Customers::Types::CreateCustomersResponse.load(response.body)
  else
    error_class = Leal::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#get(request_options: {}, **params) ⇒ Leal::Customers::Types::GetCustomersResponse

Returns detailed information about a single customer, including all of their enrolled loyalty cards with stamp progress and wallet pass URLs (apple_wallet_url and google_wallet_url) for each card. Also includes metadata and external_references so you can sync state with external systems.

Examples:

client.customers.get(
  account_id: 1,
  id: 1
)

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :account_id (Integer)
  • :id (Integer)

Returns:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/leal/customers/client.rb', line 142

def get(request_options: {}, **params)
  params = Leal::Internal::Types::Utils.normalize_keys(params)
  request = Leal::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "api/v1/accounts/#{URI.encode_uri_component(params[:account_id].to_s)}/customers/#{URI.encode_uri_component(params[:id].to_s)}",
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Leal::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Leal::Customers::Types::GetCustomersResponse.load(response.body)
  else
    error_class = Leal::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#list(request_options: {}, **params) ⇒ Leal::Customers::Types::ListCustomersResponse

Returns a paginated list of customers for the store. Use the search parameter to filter by name, email, phone, card code (barcode), or external reference ID. Alternatively, pass source AND external_id together to perform an exact lookup by an external reference - the response will contain at most one customer.

Examples:

client.customers.list(account_id: 1)

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :account_id (Integer)
  • :search (String, nil)
  • :source (String, nil)
  • :external_id (String, nil)
  • :page (Integer, nil)
  • :items (Integer, nil)

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/leal/customers/client.rb', line 36

def list(request_options: {}, **params)
  params = Leal::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["search"] = params[:search] if params.key?(:search)
  query_params["source"] = params[:source] if params.key?(:source)
  query_params["external_id"] = params[:external_id] if params.key?(:external_id)
  query_params["page"] = params[:page] if params.key?(:page)
  query_params["items"] = params[:items] if params.key?(:items)

  request = Leal::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "api/v1/accounts/#{URI.encode_uri_component(params[:account_id].to_s)}/customers",
    query: query_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Leal::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Leal::Customers::Types::ListCustomersResponse.load(response.body)
  else
    error_class = Leal::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#update(request_options: {}, **params) ⇒ Leal::Customers::Types::UpdateCustomersResponse

Updates an existing customer's details. To add stamps or redeem rewards, use the customer cards endpoints instead.

metadata is shallow-merged into the existing metadata. external_references are upserted by (source, external_id) - to remove a reference, omit it from subsequent calls and use a separate DELETE workflow (not yet exposed via API; manage in dashboard for now).

Examples:

client.customers.update(
  account_id: 1,
  id: 1,
  customer: {}
)

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :account_id (Integer)
  • :id (Integer)

Returns:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/leal/customers/client.rb', line 189

def update(request_options: {}, **params)
  params = Leal::Internal::Types::Utils.normalize_keys(params)
  request_data = Leal::Customers::Types::UpdateCustomersRequest.new(params).to_h
  non_body_param_names = %w[account_id id]
  body = request_data.except(*non_body_param_names)

  request = Leal::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "PATCH",
    path: "api/v1/accounts/#{URI.encode_uri_component(params[:account_id].to_s)}/customers/#{URI.encode_uri_component(params[:id].to_s)}",
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Leal::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Leal::Customers::Types::UpdateCustomersResponse.load(response.body)
  else
    error_class = Leal::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end