Sha256: 50dd2ac445ed79822e21171d2a04e9a7f719d0d28f26757913ffabf41fbc9e20

Contents?: true

Size: 1.99 KB

Versions: 5

Compression:

Stored size: 1.99 KB

Contents

require 'httparty'

require_relative 'spree_client/models'
require_relative 'spree_client/api/v1'

# This class is a client for Spree Commerce Backend.  It can push
# information to it and also retrieve it from the APIs.
#
# The Spree Backend needs to have Devise configured with
# http_authenticatable = true so it can send the credentials as basic
# authorization header.
module SpreeClient
  include HTTParty

  # The backend will redirect in certain actions, we want to be able to
  # distinguish them.
  follow_redirects false

  # Cookies
  # @return [String]
  attr_accessor :cookies

  # Initialize an SpreeClient.  If not parameters are given it'll try to
  # login to the default development Spree.
  #
  # @param [String] :spree_url The Spree site
  # @param [String] :username Username
  # @param [String] :password Password
  def initialize(spree_url: 'http://localhost:3000', username: 'spree@example.com', password: 'spree123')
    self.class.basic_auth username, password
    self.class.default_options[:base_uri] = HTTParty.normalize_base_uri(spree_url)
  end

  # Gets an authenticity token from the API by sending credentials to a
  # custom API endpoint.  Otherwise we have to download and parse the
  # forms.  It stores the cookie so the token can be verified afterwards.
  #
  # @see SpreeClient#cookies
  # @return [String]
  def authenticity_token
    response = self.class.get('/admin/authenticity_token',
                              headers: headers)

    @cookies = response.headers['set-cookie']

    response.to_s
  end

  # Taxonomies
  #
  # @see SpreeClient::Taxonomies
  # @return [SpreeClient::Taxonomies]
  def taxonomies
    @taxonomies ||= Taxonomies.new spree: self
  end

  # Properties
  #
  # @see SpreeClient::Properties
  # @return [SpreeClient::Properties]
  def properties
    @properties ||= Properties.new spree: self
  end

  # Default headers.  If there're cookies it sends them.
  #
  # @return [Hash] Headers
  def headers(extra = {})
    extra.merge({ 'Cookie' => cookies })
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spree-api-client-0.2.5 lib/spree_client.rb~
spree-api-client-0.2.4 lib/spree_client.rb~
spree-api-client-0.2.3 lib/spree_client.rb~
spree-api-client-0.2.2 lib/spree_client.rb~
spree-api-client-0.2.1 lib/spree_client.rb~