Sha256: 3745f79cbdb346694a0e3b56dcb42b724ae80d5d1ebfaaf92501adea76007ce0

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

module Cordial
  # Wraps all interaction with the Product resource.
  # @see https://api.cordial.io/docs/v1/#!/products
  class Products
    include ::HTTParty
    extend Client

    # Find a product.
    # @example Usage
    #  Cordial::Products.find(id: 1)
    # @example Response when the product was found.
    # {
    #   "_id"=>"5b28275fe1dc0fa0c872abec",
    #   "productID"=>"1",
    #   "productName"=>"Test Product",
    #   "price"=>10,
    #   "variants"=>[
    #     {
    #       "sku"=>"123456789",
    #       "attr"=>{"color"=>"blue", "size"=>"Large"},
    #       "qty"=>"10"}
    #   ],
    #   "accountID"=>645,
    #   "totalQty"=>10,
    #   "lm"=>"2018-06-18T21:42:55+0000",
    #   "ct"=>"2018-06-18T21:42:55+0000"
    # }
    #
    # @example Response when the product was not found.
    #  {"error"=>true, "message"=>"record not found"}
    def self.find(id:)
      client.get("/products/#{id}")
    end

    # Create a new product.
    # options keyword argument is for optional parameters
    # https://support.cordial.com/hc/en-us/articles/203886098#postProducts
    # If the product already exists it will be updated.
    # @example Usage.
    #  Cordial::Products.create(
    #    id: 1,
    #    name: 'Test Product',
    #    options: {
    #      price: 99.99,
    #      variants: [{
    #        sku: '123',
    #        attr: {
    #          color: 'red',
    #          size: 'L'
    #        }
    #      }],
    #      inStock: true,
    #      taxable: true,
    #      enabled: true,
    #      properties: {test_metadata: 'my test metadata'}
    #    }
    #  )
    def self.create(id:, name:, options: {})
      body = {
        productID: id,
        productName: name
      }.merge(options).compact

      client.post('/products', body: body.to_json)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cordial-0.1.12 lib/cordial/products.rb
cordial-0.1.11 lib/cordial/products.rb
cordial-0.1.10 lib/cordial/products.rb
cordial-0.1.9 lib/cordial/products.rb
cordial-0.1.8 lib/cordial/products.rb