Sha256: 55816aea9483f398cf1fdd7144c7cc30384810ba5818fbd9e0426bb253033448

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

require_relative 'errors'
module SearchSpring
  API_ENDPOINT = 'https://api.searchspring.net'.freeze

  #
  #
  class Client
    attr_reader :site_id, :conn, :secret_key

    def initialize(site_id, secret_key, req_options = {})
      @site_id = site_id
      @secret_key = secret_key
      raise Errors::AuthenticationError, 'No Site ID provided.' unless @site_id
      unless @secret_key
        raise Errors::AuthenticationError, 'No Secret Key provided.'
      end

      @conn = connection(req_options)
    end

    def upsert(feed_id:, products: [])
      conn.post(
        'api/index/upsert.json',
        feedId: feed_id,
        records: products
      )
    end

    def update(feed_id:, products: [])
      conn.post(
        'api/index/update.json',
        feedId: feed_id,
        records: products
      )
    end

    def delete(feed_id:, product_ids: [])
      conn.post(
        'api/index/delete.json',
        feedId: feed_id,
        records: product_ids
      )
    end

    private

    def connection(options = {})
      Faraday.new(default_req_options.merge(options)) do |f|
        f.headers['Accept'] = 'application/json'
        f.request  :json
        f.request  :basic_auth, site_id, secret_key
        f.response :logger
        f.adapter :excon
        f.use Errors::RequestError
      end
    end

    def default_req_options
      {
        url: API_ENDPOINT,
        ssl: {
          ca_path: SearchSpring::DEFAULT_CA_BUNDLE_PATH,
          ca_file: File.join(
            SearchSpring::DEFAULT_CA_BUNDLE_PATH, 'ca-certificates.crt'
          )
        }
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
search_spring-0.2.1 lib/search_spring/client.rb
search_spring-0.2.0 lib/search_spring/client.rb