Class: Leal::Locations::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void

Parameters:



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

def initialize(client:)
  @client = client
end

Instance Method Details

#create(request_options: {}, **params) ⇒ Leal::Locations::Types::CreateLocationsResponse

Creates a new physical location for the store. The provided address is automatically geocoded to latitude and longitude coordinates in the background.

Examples:

client.locations.create(
  account_id: 1,
  location: {
    address: "address",
    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:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/leal/locations/client.rb', line 70

def create(request_options: {}, **params)
  params = Leal::Internal::Types::Utils.normalize_keys(params)
  request_data = Leal::Locations::Types::CreateLocationsRequest.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)}/locations",
    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::Locations::Types::CreateLocationsResponse.load(response.body)
  else
    error_class = Leal::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#delete(request_options: {}, **params) ⇒ untyped

Permanently deletes a location. This action cannot be undone.

Examples:

client.locations.delete(
  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:

  • (untyped)

Raises:

  • (error_class)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/leal/locations/client.rb', line 157

def delete(request_options: {}, **params)
  params = Leal::Internal::Types::Utils.normalize_keys(params)
  request = Leal::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "DELETE",
    path: "api/v1/accounts/#{URI.encode_uri_component(params[:account_id].to_s)}/locations/#{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
  return if code.between?(200, 299)

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

#get(request_options: {}, **params) ⇒ Leal::Locations::Types::GetLocationsResponse

Returns a single location by ID.

Examples:

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/leal/locations/client.rb', line 116

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)}/locations/#{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::Locations::Types::GetLocationsResponse.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::Locations::Types::ListLocationsResponseItem]

Returns every physical location belonging to the specified store.

Examples:

client.locations.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)

Returns:

Raises:

  • (error_class)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/leal/locations/client.rb', line 28

def list(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)}/locations",
    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::Locations::Types::UpdateLocationsResponse

Updates an existing location. If the address is changed, it will be re-geocoded automatically.

Examples:

client.locations.update(
  account_id: 1,
  id: 1,
  location: {}
)

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:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/leal/locations/client.rb', line 197

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