Class: BigcommerceProductAgent::Client::Product

Inherits:
AbstractClient show all
Defined in:
lib/client/product.rb

Instance Method Summary collapse

Methods inherited from AbstractClient

#client, #get, #index, #initialize, #uri, uri_base, #uri_base

Constructor Details

This class inherits a constructor from BigcommerceProductAgent::Client::AbstractClient

Instance Method Details

#create(payload, params = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/client/product.rb', line 23

def create(payload, params={})
    begin
        response = client.post(uri, payload.to_json) do |request|
            request.params.update(params) if params
        end
    rescue Faraday::Error::ClientError => e
        raise e, "\n#{e.message}\nFailed to create product with payload = #{payload.to_json}\n", e.backtrace
    end

    return response.body['data']
end

#delete(id) ⇒ Object



18
19
20
21
# File 'lib/client/product.rb', line 18

def delete(id)
    response = client.delete(uri(product_id: id))
    return true
end

#get_by_skus(skus, include = %w[custom_fields modifiers]) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/client/product.rb', line 43

def get_by_skus(skus, include = %w[custom_fields modifiers])
    products = index({
        'sku:in': skus.join(','),
        include: include.join(','),
    })

    map = {}

    products.each do |product|
        map[product['sku']] = product
    end

    map
end

#update(id, payload, params = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/client/product.rb', line 6

def update(id, payload, params={})
    begin
        response = client.put(uri(product_id: id), payload.to_json) do |request|
            request.params.update(params) if params
        end
    rescue Faraday::Error::ClientError => e
        raise e, "\n#{e.message}\nFailed to update product with payload = #{payload.to_json}\n", e.backtrace
    end

    return response.body['data']
end

#upsert(payload, params = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/client/product.rb', line 35

def upsert(payload, params={})
    if payload[:id]
        return update(payload[:id], payload, params)
    else
        return create(payload, params)
    end
end