module CheckoutRu

Constants

NoDeliveryFoundError
SERVICE_URL
VERSION

Attributes

adapter[RW]
api_key[RW]
auto_renew_session[RW]
service_url[RW]

Public Class Methods

build_connection(options = {}) click to toggle source
# File lib/checkout_ru.rb, line 108
def build_connection(options = {})
  url = options[:url] || service_url

  Faraday.new(:url => url) do |conn|
    conn.request :multi_json
    conn.response :raise_error
    conn.response :multi_json
    conn.adapter options[:adapter] || adapter
  end
end
camelize_keys!(obj) click to toggle source
# File lib/checkout_ru.rb, line 119
def camelize_keys!(obj)
  case obj
  when Hash
    obj.replace(Hash[
      obj.map do |key, value|
        [ key.to_s.downcase.gsub(/_([a-z\d]*)/) { "#{$1.capitalize}" },
          camelize_keys!(value) ]
      end
    ])
  when Array
    obj.map! {|el| camelize_keys!(el)}
  else
    obj
  end
end
create_order(order, options = {}) click to toggle source
# File lib/checkout_ru.rb, line 27
def create_order(order, options = {})
  args = {
    :via => :post,
    :params => { :order => order.to_hash }
  }.merge(options)

  make_request_with_key '/service/order/create', args
end
get_ticket(options = {}) click to toggle source
# File lib/checkout_ru.rb, line 22
def get_ticket(options = {})
  key = options.delete(:api_key) || api_key
  make_request("/service/login/ticket/#{key}", options)['ticket']
end
make_request(service, options = {}) click to toggle source
# File lib/checkout_ru.rb, line 83
def make_request(service, options = {})
  headers      = { 'Accept' => 'application/json' }
  conn         = options[:connection] || build_connection
  method       = options[:via]        || :get
  request_opts = options[:request]    || {}
  params       = options[:params].dup if options[:params]

  camelize_keys!(params)

  body = conn.public_send(method, service, params, headers) do |req|
    req.options.update(request_opts) unless request_opts.empty?
  end.body

  underscore_keys!(body)

  case body
  when Hash
    ::Hashie::Mash.new(body)
  when Array
    body.map{|el| ::Hashie::Mash.new(el)}
  else
    body
  end
end
make_request_with_key(service, options = {}) click to toggle source
# File lib/checkout_ru.rb, line 77
def make_request_with_key(service, options = {})
  key = options.delete(:api_key) || api_key
  params = (options[:params] || {}).merge(:api_key => key)
  make_request service, options.merge(:params => params)
end
parse_status(status) click to toggle source
# File lib/checkout_ru.rb, line 61
def parse_status(status)
  if status.is_a?(Symbol)
    unless Order::Status::MAP.keys.include?(status)
      raise Error, "Invalid order status: #{status}"
    end

    Order::Status::MAP[status]
  else
    unless Order::Status::MAP.values.include?(status)
      raise Error, "Invalid order status: #{status}"
    end

    status
  end
end
status(remote_id, status, options = {}) click to toggle source
# File lib/checkout_ru.rb, line 45
def status(remote_id, status, options = {})
  args = {
    :via => :post,
    :params => { :status => parse_status(status) }
  }.merge(options)

  make_request_with_key "/service/order/status/#{remote_id}", args
end
status_history(order_id, options = {}) click to toggle source
# File lib/checkout_ru.rb, line 54
def status_history(order_id, options = {})
  resp = make_request_with_key "/service/order/statushistory/#{order_id}",
    options
  resp.order.date = Date.parse(resp.order.date)
  resp
end
underscore_keys!(obj) click to toggle source
# File lib/checkout_ru.rb, line 135
def underscore_keys!(obj)
  case obj
  when Hash
    obj.replace(Hash[
      obj.map do |key, value|
        [ key.to_s.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase,
          underscore_keys!(value) ]
      end
    ])
  when Array
    obj.map! {|el| underscore_keys!(el)}
  else
    obj
  end
end
update_order(remote_id, order, options = {}) click to toggle source
# File lib/checkout_ru.rb, line 36
def update_order(remote_id, order, options = {})
  args = {
    :via => :post,
    :params => { :order => order.to_hash }
  }.merge(options)

  make_request_with_key "/service/order/#{remote_id}", args
end