module MadCart::Store::Base

Constants

DEFAULT_CONNECTION_OPTIONS

Public Class Methods

included(base) click to toggle source
# File lib/mad_cart/store/base.rb, line 23
def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    include InheritableAttributes
    inheritable_attributes :connection_delegate, :required_connection_args,
      :fetch_delegates, :format_delegates, :after_init_delegate
  end
end
new(*args) click to toggle source
# File lib/mad_cart/store/base.rb, line 32
def initialize(*args)
  set_init_args(*args)
  after_initialize(*args)
end

Public Instance Methods

connection() click to toggle source
# File lib/mad_cart/store/base.rb, line 37
def connection
  validate_connection_args!
  return init_connection
end
init_connection() click to toggle source
# File lib/mad_cart/store/base.rb, line 42
def init_connection
  @connection ||= execute_delegate(klass.connection_delegate, @init_args)
end

Private Instance Methods

after_initialize(*args) click to toggle source
# File lib/mad_cart/store/base.rb, line 98
def after_initialize(*args)
  return nil unless klass.after_init_delegate
  execute_delegate(klass.after_init_delegate, *args)
end
check_for_errors() { || ... } click to toggle source
# File lib/mad_cart/store/base.rb, line 62
def check_for_errors
  response = yield

  case response.status
  when 401
    raise InvalidCredentials
  when 404
    raise InvalidStore
  when 500
    raise ServerError
  end

  response
rescue Faraday::Error::ConnectionFailed, Faraday::ParsingError, Faraday::SSLError
  raise UnavailableStore
end
configured_connection_args() click to toggle source
# File lib/mad_cart/store/base.rb, line 123
def configured_connection_args
  MadCart.config.send(klass.to_s.demodulize.underscore) || {}
end
empty_body?(response) click to toggle source
# File lib/mad_cart/store/base.rb, line 80
def empty_body?(response)
  response.status == 204 || response.body.nil?
end
ensure_model_format(model, results) click to toggle source
# File lib/mad_cart/store/base.rb, line 128
def ensure_model_format(model, results)
  if results.is_a?(Array)
    map_to_madcart_model(model, results)
  else
    "MadCart::Model::#{model.to_s.classify}".constantize.new(results)
  end
end
execute_delegate(delegate, *args) click to toggle source
# File lib/mad_cart/store/base.rb, line 90
def execute_delegate(delegate, *args)
  return self.send(delegate, *args) if delegate.is_a?(Symbol)
  return delegate.call(*args) if delegate.is_a?(Proc)

  raise ArgumentError, "Invalid delegate" # if not returned by now
end
klass() click to toggle source
# File lib/mad_cart/store/base.rb, line 85
def klass
  self.class
end
map_to_madcart_model(model, results) click to toggle source
# File lib/mad_cart/store/base.rb, line 137
def map_to_madcart_model(model, results)
  return results if results.first.is_a?(MadCart::Model::Base)

  results.map do |args|
    "MadCart::Model::#{model.to_s.classify}".constantize.new(args)
  end
end
missing_args() click to toggle source
# File lib/mad_cart/store/base.rb, line 113
def missing_args
  klass.required_connection_args - @init_args.keys
end
parse_response(&block) click to toggle source
# File lib/mad_cart/store/base.rb, line 56
def parse_response(&block)
  response = check_for_errors(&block)
  empty_body?(response) ? [] : response.body
end
set_init_args(*args) click to toggle source
# File lib/mad_cart/store/base.rb, line 118
def set_init_args(*args)
  @init_args ||= configured_connection_args.merge(args.last || {})
end
valid_by_path?(path) click to toggle source
# File lib/mad_cart/store/base.rb, line 46
def valid_by_path?(path)
  check_for_errors do
    connection.get(path)
  end
  true
rescue InvalidCredentials, InvalidStore, ServerError, UnavailableStore
  false
end
validate_connection_args!() click to toggle source
# File lib/mad_cart/store/base.rb, line 104
def validate_connection_args!
  return true if klass.required_connection_args.empty?

  raise(SetupError, SetupError.message) if @init_args.nil?
  raise(ArgumentError,"Missing connection arguments: "\
        "#{missing_args.join(', ')}") if missing_args.present?
end