Class: PatchRetention::Products

Inherits:
Object
  • Object
show all
Defined in:
lib/patch_retention/products.rb

Class Method Summary collapse

Class Method Details

.create(name:, price:, status:, description: nil, membership: nil, tags: nil, external_id: nil, external_data: nil, config: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/patch_retention/products.rb', line 6

def create(name:, price:, status:, description: nil, membership: nil,
  tags: nil, external_id: nil, external_data: nil, config: nil)
  payload = {
    name: name,
    price: price,
    status: status,
  }
  payload[:description] = description if description
  payload[:membership] = membership unless membership.nil?
  payload[:tags] = tags if tags
  payload[:external_id] = external_id if external_id
  payload[:external_data] = external_data if external_data

  response = PatchRetention.connection(config).post("/v2/products") do |req|
    req.body = payload.to_json
    req.headers["Content-Type"] = "application/json"
  end

  JSON.parse(response.body)
rescue Faraday::Error => e
  # You might want to handle different types of Faraday errors differently
  # or raise a custom error class
  raise Error, "Failed to create product: #{e.message}"
end

.find(product_id:, config: nil) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/patch_retention/products.rb', line 53

def find(product_id:, config: nil)
  response = PatchRetention.connection(config).get("/v2/products/#{product_id}")

  JSON.parse(response.body)
rescue Faraday::Error => e
  raise Error, "Failed to find product: #{e.message}"
end

.update(product_id:, name: nil, price: nil, status: nil, description: nil, membership: nil, tags: nil, external_id: nil, external_data: nil, config: nil) ⇒ Object



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

def update(product_id:, name: nil, price: nil, status: nil, description: nil,
  membership: nil, tags: nil, external_id: nil, external_data: nil, config: nil)
  payload = {}
  payload[:name] = name if name
  payload[:price] = price if price
  payload[:status] = status if status
  payload[:description] = description if description
  payload[:membership] = membership unless membership.nil?
  payload[:tags] = tags if tags
  payload[:external_id] = external_id if external_id
  payload[:external_data] = external_data if external_data

  response = PatchRetention.connection(config).patch("/v2/products/#{product_id}") do |req|
    req.body = payload.to_json
    req.headers["Content-Type"] = "application/json"
  end

  JSON.parse(response.body)
rescue Faraday::Error => e
  raise Error, "Failed to update product: #{e.message}"
end