Class: Square::Inventory::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/square/inventory/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Square::Inventory::Client



7
8
9
# File 'lib/square/inventory/client.rb', line 7

def initialize(client:)
  @client = client
end

Instance Method Details

#batch_create_changes(request_options: {}, **params) ⇒ Square::Types::BatchChangeInventoryResponse

Applies adjustments and counts to the provided item quantities.

On success: returns the current calculated counts for all objects referenced in the request. On failure: returns a list of related errors.



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

def batch_create_changes(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/inventory/changes/batch-create",
    body: Square::Types::BatchChangeInventoryRequest.new(params).to_h
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::BatchChangeInventoryResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#batch_get_changes(request_options: {}, **params) ⇒ Square::Types::BatchGetInventoryChangesResponse

Returns historical physical counts and adjustments based on the provided filter criteria.

Results are paginated and sorted in ascending order according their ‘occurred_at` timestamp (oldest first).

BatchRetrieveInventoryChanges is a catch-all query endpoint for queries that cannot be handled by other, simpler endpoints.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/square/inventory/client.rb', line 172

def batch_get_changes(request_options: {}, **params)
  Square::Internal::CursorItemIterator.new(
    cursor_field: :cursor,
    item_field: :changes,
    initial_cursor: params[:cursor]
  ) do |next_cursor|
    params[:cursor] = next_cursor
    _request = Square::Internal::JSON::Request.new(
      base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
      method: "POST",
      path: "v2/inventory/changes/batch-retrieve",
      body: Square::Types::BatchRetrieveInventoryChangesRequest.new(params).to_h
    )
    begin
      _response = @client.send(_request)
    rescue Net::HTTPRequestTimeout
      raise Square::Errors::TimeoutError
    end
    code = _response.code.to_i
    if code.between?(200, 299)
      Square::Types::BatchGetInventoryChangesResponse.load(_response.body)
    else
      error_class = Square::Errors::ResponseError.subclass_for_code(code)
      raise error_class.new(_response.body, code: code)
    end
  end
end

#batch_get_counts(request_options: {}, **params) ⇒ Square::Types::BatchGetInventoryCountsResponse

Returns current counts for the provided [CatalogObject](entity:CatalogObject)s at the requested [Location](entity:Location)s.

Results are paginated and sorted in descending order according to their ‘calculated_at` timestamp (newest first).

When ‘updated_after` is specified, only counts that have changed since that time (based on the server timestamp for the most recent change) are returned. This allows clients to perform a “sync” operation, for example in response to receiving a Webhook notification.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/square/inventory/client.rb', line 213

def batch_get_counts(request_options: {}, **params)
  Square::Internal::CursorItemIterator.new(
    cursor_field: :cursor,
    item_field: :counts,
    initial_cursor: params[:cursor]
  ) do |next_cursor|
    params[:cursor] = next_cursor
    _request = Square::Internal::JSON::Request.new(
      base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
      method: "POST",
      path: "v2/inventory/counts/batch-retrieve",
      body: Square::Types::BatchGetInventoryCountsRequest.new(params).to_h
    )
    begin
      _response = @client.send(_request)
    rescue Net::HTTPRequestTimeout
      raise Square::Errors::TimeoutError
    end
    code = _response.code.to_i
    if code.between?(200, 299)
      Square::Types::BatchGetInventoryCountsResponse.load(_response.body)
    else
      error_class = Square::Errors::ResponseError.subclass_for_code(code)
      raise error_class.new(_response.body, code: code)
    end
  end
end

#changes(request_options: {}, **params) ⇒ Square::Types::GetInventoryChangesResponse

Returns a set of physical counts and inventory adjustments for the provided [CatalogObject](entity:CatalogObject) at the requested [Location](entity:Location)s.

You can achieve the same result by calling [BatchRetrieveInventoryChanges](api-endpoint:Inventory-BatchRetrieveInventoryChanges) and having the ‘catalog_object_ids` list contain a single element of the `CatalogObject` ID.

Results are paginated and sorted in descending order according to their ‘occurred_at` timestamp (newest first).

There are no limits on how far back the caller can page. This endpoint can be used to display recent changes for a specific item. For more sophisticated queries, use a batch endpoint.



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/square/inventory/client.rb', line 367

def changes(request_options: {}, **params)
  params = Square::Internal::Types::Utils.symbolize_keys(params)
  _query_param_names = %i[location_ids cursor]
  _query = params.slice(*_query_param_names)
  params = params.except(*_query_param_names)

  Square::Internal::CursorItemIterator.new(
    cursor_field: :cursor,
    item_field: :changes,
    initial_cursor: _query[:cursor]
  ) do |next_cursor|
    _query[:cursor] = next_cursor
    _request = Square::Internal::JSON::Request.new(
      base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
      method: "GET",
      path: "v2/inventory/#{params[:catalog_object_id]}/changes",
      query: _query
    )
    begin
      _response = @client.send(_request)
    rescue Net::HTTPRequestTimeout
      raise Square::Errors::TimeoutError
    end
    code = _response.code.to_i
    if code.between?(200, 299)
      Square::Types::GetInventoryChangesResponse.load(_response.body)
    else
      error_class = Square::Errors::ResponseError.subclass_for_code(code)
      raise error_class.new(_response.body, code: code)
    end
  end
end

#deprecated_batch_change(request_options: {}, **params) ⇒ Square::Types::BatchChangeInventoryResponse

Deprecated version of [BatchChangeInventory](api-endpoint:Inventory-BatchChangeInventory) after the endpoint URL is updated to conform to the standard convention.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/square/inventory/client.rb', line 63

def deprecated_batch_change(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/inventory/batch-change",
    body: Square::Types::BatchChangeInventoryRequest.new(params).to_h
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::BatchChangeInventoryResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#deprecated_batch_get_changes(request_options: {}, **params) ⇒ Square::Types::BatchGetInventoryChangesResponse

Deprecated version of [BatchRetrieveInventoryChanges](api-endpoint:Inventory-BatchRetrieveInventoryChanges) after the endpoint URL is updated to conform to the standard convention.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/square/inventory/client.rb', line 88

def deprecated_batch_get_changes(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/inventory/batch-retrieve-changes",
    body: Square::Types::BatchRetrieveInventoryChangesRequest.new(params).to_h
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::BatchGetInventoryChangesResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#deprecated_batch_get_counts(request_options: {}, **params) ⇒ Square::Types::BatchGetInventoryCountsResponse

Deprecated version of [BatchRetrieveInventoryCounts](api-endpoint:Inventory-BatchRetrieveInventoryCounts) after the endpoint URL is updated to conform to the standard convention.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/square/inventory/client.rb', line 113

def deprecated_batch_get_counts(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/inventory/batch-retrieve-counts",
    body: Square::Types::BatchGetInventoryCountsRequest.new(params).to_h
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::BatchGetInventoryCountsResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#deprecated_get_adjustment(request_options: {}, **params) ⇒ Square::Types::GetInventoryAdjustmentResponse

Deprecated version of [RetrieveInventoryAdjustment](api-endpoint:Inventory-RetrieveInventoryAdjustment) after the endpoint URL is updated to conform to the standard convention.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/square/inventory/client.rb', line 15

def deprecated_get_adjustment(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "GET",
    path: "v2/inventory/adjustment/#{params[:adjustment_id]}"
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::GetInventoryAdjustmentResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#deprecated_get_physical_count(request_options: {}, **params) ⇒ Square::Types::GetInventoryPhysicalCountResponse

Deprecated version of [RetrieveInventoryPhysicalCount](api-endpoint:Inventory-RetrieveInventoryPhysicalCount) after the endpoint URL is updated to conform to the standard convention.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/square/inventory/client.rb', line 245

def deprecated_get_physical_count(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "GET",
    path: "v2/inventory/physical-count/#{params[:physical_count_id]}"
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::GetInventoryPhysicalCountResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#get(request_options: {}, **params) ⇒ Square::Types::GetInventoryCountResponse

Retrieves the current calculated stock count for a given [CatalogObject](entity:CatalogObject) at a given set of [Location](entity:Location)s. Responses are paginated and unsorted. For more sophisticated queries, use a batch endpoint.



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/square/inventory/client.rb', line 319

def get(request_options: {}, **params)
  params = Square::Internal::Types::Utils.symbolize_keys(params)
  _query_param_names = %i[location_ids cursor]
  _query = params.slice(*_query_param_names)
  params = params.except(*_query_param_names)

  Square::Internal::CursorItemIterator.new(
    cursor_field: :cursor,
    item_field: :counts,
    initial_cursor: _query[:cursor]
  ) do |next_cursor|
    _query[:cursor] = next_cursor
    _request = Square::Internal::JSON::Request.new(
      base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
      method: "GET",
      path: "v2/inventory/#{params[:catalog_object_id]}",
      query: _query
    )
    begin
      _response = @client.send(_request)
    rescue Net::HTTPRequestTimeout
      raise Square::Errors::TimeoutError
    end
    code = _response.code.to_i
    if code.between?(200, 299)
      Square::Types::GetInventoryCountResponse.load(_response.body)
    else
      error_class = Square::Errors::ResponseError.subclass_for_code(code)
      raise error_class.new(_response.body, code: code)
    end
  end
end

#get_adjustment(request_options: {}, **params) ⇒ Square::Types::GetInventoryAdjustmentResponse

Returns the [InventoryAdjustment](entity:InventoryAdjustment) object with the provided ‘adjustment_id`.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/square/inventory/client.rb', line 39

def get_adjustment(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "GET",
    path: "v2/inventory/adjustments/#{params[:adjustment_id]}"
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::GetInventoryAdjustmentResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#get_physical_count(request_options: {}, **params) ⇒ Square::Types::GetInventoryPhysicalCountResponse

Returns the [InventoryPhysicalCount](entity:InventoryPhysicalCount) object with the provided ‘physical_count_id`.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/square/inventory/client.rb', line 269

def get_physical_count(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "GET",
    path: "v2/inventory/physical-counts/#{params[:physical_count_id]}"
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::GetInventoryPhysicalCountResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#get_transfer(request_options: {}, **params) ⇒ Square::Types::GetInventoryTransferResponse

Returns the [InventoryTransfer](entity:InventoryTransfer) object with the provided ‘transfer_id`.



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/square/inventory/client.rb', line 293

def get_transfer(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "GET",
    path: "v2/inventory/transfers/#{params[:transfer_id]}"
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::GetInventoryTransferResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end