Module: CheckoutRu

Defined in:
lib/checkout_ru.rb,
lib/checkout_ru/item.rb,
lib/checkout_ru/user.rb,
lib/checkout_ru/order.rb,
lib/checkout_ru/entity.rb,
lib/checkout_ru/address.rb,
lib/checkout_ru/session.rb,
lib/checkout_ru/version.rb,
lib/checkout_ru/delivery.rb

Defined Under Namespace

Classes: Address, Delivery, Entity, Item, Order, Session, User

Constant Summary collapse

SERVICE_URL =
'http://platform.checkout.ru'.freeze
Error =
Class.new(Faraday::Error::ClientError)
VERSION =
"0.4.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.adapterObject

Returns the value of attribute adapter.



17
18
19
# File 'lib/checkout_ru.rb', line 17

def adapter
  @adapter
end

.api_keyObject

Returns the value of attribute api_key.



17
18
19
# File 'lib/checkout_ru.rb', line 17

def api_key
  @api_key
end

.auto_renew_sessionObject

Returns the value of attribute auto_renew_session.



17
18
19
# File 'lib/checkout_ru.rb', line 17

def auto_renew_session
  @auto_renew_session
end

.service_urlObject

Returns the value of attribute service_url.



17
18
19
# File 'lib/checkout_ru.rb', line 17

def service_url
  @service_url
end

Class Method Details

.build_connection(options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/checkout_ru.rb', line 111

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) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/checkout_ru.rb', line 122

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 = {}) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/checkout_ru.rb', line 28

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 = {}) ⇒ Object



23
24
25
26
# File 'lib/checkout_ru.rb', line 23

def get_ticket(options = {})
  key = options.delete(:api_key) || api_key
  make_request("/service/login/ticket/#{key}", options)['ticket']
end

.make_request(service, options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/checkout_ru.rb', line 86

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 = {}) ⇒ Object



80
81
82
83
84
# File 'lib/checkout_ru.rb', line 80

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) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/checkout_ru.rb', line 62

def parse_status(status)
  status_map = Order::Status::MAP

  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 = {}) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/checkout_ru.rb', line 46

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 = {}) ⇒ Object



55
56
57
58
59
60
# File 'lib/checkout_ru.rb', line 55

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) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/checkout_ru.rb', line 138

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 = {}) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/checkout_ru.rb', line 37

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