Sha256: de73df27e69d38ca54f72685b71d7c327fb1d5a8ac5d9edb3cb8ad23b1ce8d8c

Contents?: true

Size: 1.92 KB

Versions: 16

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require_relative '../api_resource'

module ErpIntegration
  module Fulfil
    module Resources
      class Product < ApiResource
        self.model_name = 'product.product'

        # Checks whether a certain product is a BOM.
        # @param sku [String] The product's SKU.
        # @return [Boolean] Whether it's a BOM or not.
        def bom?(sku)
          select(:boms).find_by!(code: sku).boms.any?
        end
        alias billing_of_materials? bom?

        # https://developers.fulfil.io/rest_api/model/product.product/#add-product-images
        # If you have your product images hosted not on Fulfil, you can use this endpoint to add the media in Fulfil
        # to a product.
        # The method expects a list of objects when making the request:
        #
        # url (string) image URL
        # name (string) name of the image file, including image extension
        #
        # example:
        #
        #   [
        #     [
        #       {
        #         "url": "https://dns.mysite.com/media/image1.jpg?format=jpg&name=small",
        #         "name": "product1_1.jpg"
        #       },
        #       {
        #         "url": "https://mysite.net/us/images/product_2.jpg",
        #         "name": "product1_2.jpg"
        #       }
        #     ]
        #   ]
        #
        # To simplify the API usage, we're sending the received image_objects wrapped in an array
        #
        # @param [Integer] id
        # @param [Array<Object>] image_objects
        def add_media(id, image_objects)
          client.put("model/#{model_name}/#{id}/add_media", [image_objects])
          true

        # Workaround: Fulfil api does not return a json when status code is 200 (a.k.a. "Ok")
        # and faraday is having an error when trying to parse it. Let's skip the parse error
        # and move on.
        rescue Faraday::ParsingError
          true
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
erp_integration-0.57.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.56.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.55.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.54.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.53.1 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.53.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.52.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.50.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.49.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.48.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.43.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.42.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.41.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.40.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.39.0 lib/erp_integration/fulfil/resources/product.rb
erp_integration-0.38.0 lib/erp_integration/fulfil/resources/product.rb