Class: Leal::Cards::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void

Parameters:



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

def initialize(client:)
  @client = client
end

Instance Method Details

#create(request_options: {}, **params) ⇒ Leal::Cards::Types::CreateCardsResponse

Creates a new loyalty stamp card template for the store. The card defines the visual design (colours, icon, strip) and program rules (stamps required, initial stamps).

Examples:

client.cards.create(
  account_id: 1,
  card: {
    name: "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:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/leal/cards/client.rb', line 77

def create(request_options: {}, **params)
  params = Leal::Internal::Types::Utils.normalize_keys(params)
  request_data = Leal::Cards::Types::CreateCardsRequest.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)}/cards",
    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::Cards::Types::CreateCardsResponse.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::Cards::Types::GetCardsResponse

Returns a single loyalty card template by ID, including reward and customer card counts.

Examples:

client.cards.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:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/leal/cards/client.rb', line 123

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)}/cards/#{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::Cards::Types::GetCardsResponse.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) ⇒ Array[Leal::Cards::Types::ListCardsResponseItem]

Returns loyalty card templates for the specified store. By default, only active (unarchived) cards are returned. Use the scope parameter to include archived cards.

Examples:

client.cards.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)
  • :scope (String, nil)

Returns:

Raises:

  • (error_class)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/leal/cards/client.rb', line 31

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

  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)}/cards",
    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
  return if code.between?(200, 299)

  error_class = Leal::Errors::ResponseError.subclass_for_code(code)
  raise error_class.new(response.body, code: code)
end

#update(request_options: {}, **params) ⇒ Leal::Cards::Types::UpdateCardsResponse

Updates an existing loyalty card template. Only the provided attributes are changed.

Examples:

client.cards.update(
  account_id: 1,
  id: 1,
  card: {}
)

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:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/leal/cards/client.rb', line 165

def update(request_options: {}, **params)
  params = Leal::Internal::Types::Utils.normalize_keys(params)
  request_data = Leal::Cards::Types::UpdateCardsRequest.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)}/cards/#{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::Cards::Types::UpdateCardsResponse.load(response.body)
  else
    error_class = Leal::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end