Module: CheckoutRu

Defined in:
lib/checkout_ru.rb,
lib/checkout_ru/item.rb,
lib/checkout_ru/user.rb,
lib/checkout_ru/error.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,
lib/checkout_ru/expired_ticket_response.rb

Defined Under Namespace

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

Constant Summary collapse

SERVICE_URL =
'http://platform.checkout.ru'.freeze
NoDeliveryFoundError =
Class.new(Error)
VERSION =
"0.6.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.adapterObject

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

.api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

.auto_renew_sessionObject

Returns the value of attribute auto_renew_session.



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

def auto_renew_session
  @auto_renew_session
end

.service_urlObject

Returns the value of attribute service_url.



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

def service_url
  @service_url
end

Class Method Details

.build_connection(options = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# 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) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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 = {}) ⇒ Object



27
28
29
30
31
32
33
34
# 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 = {}) ⇒ Object



22
23
24
25
# 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 = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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 = {}) ⇒ Object



77
78
79
80
81
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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 = {}) ⇒ Object



45
46
47
48
49
50
51
52
# 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 = {}) ⇒ Object



54
55
56
57
58
59
# 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) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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 = {}) ⇒ Object



36
37
38
39
40
41
42
43
# 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