inventory_level.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # frozen_string_literal: true
  2. module ShopifyAPI
  3. class InventoryLevel < Base
  4. # The default path structure in ActiveResource for delete would result in:
  5. # /admin/api/<version>/inventory_levels/#{ inventory_level.id }.json?#{ params }, but since
  6. # InventoryLevels are a second class resource made up of a Where and a What
  7. # (Location and InventoryItem), it does not have a resource ID. Here we
  8. # redefine element_path to remove the id so HTTP DELETE requests go to
  9. # /admin/api/<version>/inventory_levels.json?#{ params } instead.
  10. #
  11. def self.element_path(prefix_options = {}, query_options = nil)
  12. prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  13. "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
  14. end
  15. def destroy
  16. load_attributes_from_response(
  17. self.class.delete('/', location_id: location_id, inventory_item_id: inventory_item_id)
  18. )
  19. end
  20. def connect(relocate_if_necessary: nil)
  21. body = { location_id: location_id, inventory_item_id: inventory_item_id }
  22. body[:relocate_if_necessary] = relocate_if_necessary unless relocate_if_necessary.nil?
  23. load_attributes_from_response(
  24. self.class.post(:connect, {}, body.to_json)
  25. )
  26. end
  27. def set(new_available, disconnect_if_necessary: nil)
  28. body = {
  29. location_id: location_id,
  30. inventory_item_id: inventory_item_id,
  31. available: new_available
  32. }
  33. body[:disconnect_if_necessary] = disconnect_if_necessary unless disconnect_if_necessary.nil?
  34. load_attributes_from_response(
  35. self.class.post(:set, {}, body.to_json)
  36. )
  37. end
  38. def adjust(available_adjustment)
  39. body = {
  40. location_id: location_id,
  41. inventory_item_id: inventory_item_id,
  42. available_adjustment: available_adjustment
  43. }
  44. load_attributes_from_response(
  45. self.class.post(:adjust, {}, body.to_json)
  46. )
  47. end
  48. end
  49. end