Class: AbacatePay::Clients::ProductClient

Inherits:
Client
  • Object
show all
Defined in:
lib/abacate_pay/clients/product_client.rb

Overview

Client for the product catalogue in the AbacatePay API.

Products are the line items referenced by billings and checkouts.

Constant Summary collapse

URI =
"products"

Instance Method Summary collapse

Constructor Details

#initialize(client = nil) ⇒ ProductClient

Returns a new instance of ProductClient.



11
12
13
# File 'lib/abacate_pay/clients/product_client.rb', line 11

def initialize(client = nil)
  super(URI, client)
end

Instance Method Details

#create(data) ⇒ Resources::Products

Parameters:

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/abacate_pay/clients/product_client.rb', line 31

def create(data)
  request_data = {
    externalId: data.external_id,
    name: data.name,
    price: data.price,
    currency: data.currency || "BRL",
    description: data.description
  }
  request_data[:imageUrl] = data.image_url if data.image_url && !data.image_url.to_s.empty?
  request_data[:cycle] = data.cycle if data.cycle && !data.cycle.to_s.empty?

  response = request("POST", "create", json: request_data)
  Resources::Products.new(response)
end

#delete(id) ⇒ Resources::Products

Parameters:

  • id (String)

    Product ID

Returns:



48
49
50
51
# File 'lib/abacate_pay/clients/product_client.rb', line 48

def delete(id)
  response = request("POST", "delete", json: { id: id })
  Resources::Products.new(response)
end

#get(id) ⇒ Resources::Products

Parameters:

  • id (String)

    Product ID or externalId

Returns:



24
25
26
27
# File 'lib/abacate_pay/clients/product_client.rb', line 24

def get(id)
  response = request("GET", "get", params: { id: id })
  Resources::Products.new(response)
end

#list(**params) ⇒ Array<Resources::Products>

Parameters:

  • params (Hash)

    Optional pagination params (after, before, limit)

Returns:



17
18
19
20
# File 'lib/abacate_pay/clients/product_client.rb', line 17

def list(**params)
  response = request("GET", "list", params: params.empty? ? nil : params)
  Array(response).map { |data| Resources::Products.new(data) }
end