checkout_ru 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +4 -1
- data/lib/checkout_ru/session.rb +16 -17
- data/lib/checkout_ru/version.rb +1 -1
- data/lib/checkout_ru.rb +41 -30
- data/test/checkout_ru/session_test.rb +7 -0
- data/test/checkout_ru_test.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa9c6eba3494c7383ec333a3c72d02d4dba6675f
|
4
|
+
data.tar.gz: 62b3cdf7e19e1881999ab6aca37ed5fc344a3d28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 780328c487265dbd1b45375ed5ec140bd693ed39d673a0600788687c2d3c6e163335cb156c1d5637beb16ed6e4e6389e068ff0f460a77e9b17f7f8effd7423d5
|
7
|
+
data.tar.gz: 3c0c847e1e7bc4784533be58165de5bfd382842b482f102ed2e04718fcc45de78c9fe4a07a5bdaa3fdc10e798024cbde162947571971c8ea7dbd659da67597d9
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -22,9 +22,12 @@ Or install it yourself as:
|
|
22
22
|
CheckoutRu.api_key = 'my-api-key'
|
23
23
|
|
24
24
|
session = CheckoutRu::Session.initiate
|
25
|
-
places = session.get_places_by_query('Москва')
|
25
|
+
places = session.get_places_by_query(place: 'Москва')
|
26
26
|
places[0].name # => "г. Москва"
|
27
27
|
|
28
|
+
# Pass request options to any request
|
29
|
+
places = session.get_places_by_query({place: 'Москва'}, request: {timeout: 5})
|
30
|
+
|
28
31
|
order = CheckoutRu::Order.new(
|
29
32
|
goods: [
|
30
33
|
CheckoutRu::Item.new(
|
data/lib/checkout_ru/session.rb
CHANGED
@@ -7,40 +7,39 @@ module CheckoutRu
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
def initialize(ticket
|
10
|
+
def initialize(ticket)
|
11
11
|
@ticket = ticket
|
12
12
|
@conn = CheckoutRu.build_connection
|
13
13
|
end
|
14
14
|
|
15
|
-
def get_places_by_query(options = {})
|
16
|
-
get('
|
15
|
+
def get_places_by_query(params = {}, options = {})
|
16
|
+
get('getPlacesByQuery', params, options).suggestions
|
17
17
|
end
|
18
18
|
|
19
|
-
def calculation(options = {})
|
20
|
-
params
|
21
|
-
get('checkout/calculation', params)
|
19
|
+
def calculation(params = {}, options = {})
|
20
|
+
get('calculation', params, options)
|
22
21
|
end
|
23
22
|
|
24
|
-
def get_streets_by_query(options = {})
|
25
|
-
params
|
26
|
-
get('checkout/getStreetsByQuery', params).suggestions
|
23
|
+
def get_streets_by_query(params = {}, options = {})
|
24
|
+
get('getStreetsByQuery', params, options).suggestions
|
27
25
|
end
|
28
26
|
|
29
|
-
def get_postal_code_by_address(options = {})
|
30
|
-
params
|
31
|
-
get('checkout/getPostalCodeByAddress', params).postindex
|
27
|
+
def get_postal_code_by_address(params = {}, options = {})
|
28
|
+
get('getPostalCodeByAddress', params, options).postindex
|
32
29
|
end
|
33
30
|
|
34
|
-
def get_place_by_postal_code(options = {})
|
35
|
-
get('
|
31
|
+
def get_place_by_postal_code(params = {}, options = {})
|
32
|
+
get('getPlaceByPostalCode', params, options)
|
36
33
|
end
|
37
34
|
|
38
35
|
private
|
39
|
-
def get(service, params = {})
|
40
|
-
|
41
|
-
"/service/#{service}",
|
36
|
+
def get(service, params = {}, options = {})
|
37
|
+
args = {
|
42
38
|
:connection => @conn,
|
43
39
|
:params => params.merge(:ticket => @ticket)
|
40
|
+
}.merge(options)
|
41
|
+
|
42
|
+
CheckoutRu.make_request "/service/checkout/#{service}", args
|
44
43
|
end
|
45
44
|
end
|
46
45
|
end
|
data/lib/checkout_ru/version.rb
CHANGED
data/lib/checkout_ru.rb
CHANGED
@@ -18,52 +18,60 @@ module CheckoutRu
|
|
18
18
|
attr_accessor :service_url, :api_key, :adapter
|
19
19
|
|
20
20
|
def get_ticket(options = {})
|
21
|
-
key = options
|
22
|
-
make_request("/service/login/ticket/#{key}")['ticket']
|
21
|
+
key = options.delete(:api_key) || api_key
|
22
|
+
make_request("/service/login/ticket/#{key}", options)['ticket']
|
23
23
|
end
|
24
24
|
|
25
25
|
def create_order(order, options = {})
|
26
|
-
|
26
|
+
args = {
|
27
27
|
:via => :post,
|
28
|
-
:params => { :order => order }
|
29
|
-
|
28
|
+
:params => { :order => order }
|
29
|
+
}.merge(options)
|
30
|
+
|
31
|
+
make_request_with_key '/service/order/create', args
|
30
32
|
end
|
31
33
|
|
32
34
|
def update_order(remote_id, order, options = {})
|
33
|
-
|
35
|
+
args = {
|
34
36
|
:via => :post,
|
35
|
-
:params => { :order => order }
|
36
|
-
|
37
|
+
:params => { :order => order }
|
38
|
+
}.merge(options)
|
39
|
+
|
40
|
+
make_request_with_key "/service/order/#{remote_id}", args
|
37
41
|
end
|
38
42
|
|
39
43
|
def status(remote_id, status, options = {})
|
44
|
+
args = {
|
45
|
+
:via => :post,
|
46
|
+
:params => { :status => parse_status(status) }
|
47
|
+
}.merge(options)
|
48
|
+
|
49
|
+
make_request_with_key "/service/order/status/#{remote_id}", args
|
50
|
+
end
|
51
|
+
|
52
|
+
def status_history(order_id, options = {})
|
53
|
+
resp = make_request_with_key "/service/order/statushistory/#{order_id}",
|
54
|
+
options
|
55
|
+
resp.order.date = Date.parse(resp.order.date)
|
56
|
+
resp
|
57
|
+
end
|
58
|
+
|
59
|
+
def parse_status(status)
|
40
60
|
status_map = Order::Status::MAP
|
41
61
|
|
42
|
-
|
43
|
-
unless
|
62
|
+
if status.is_a?(Symbol)
|
63
|
+
unless Order::Status::MAP.keys.include?(status)
|
44
64
|
raise Error, "Invalid order status: #{status}"
|
45
65
|
end
|
46
66
|
|
47
|
-
|
67
|
+
Order::Status::MAP[status]
|
48
68
|
else
|
49
|
-
unless
|
69
|
+
unless Order::Status::MAP.values.include?(status)
|
50
70
|
raise Error, "Invalid order status: #{status}"
|
51
71
|
end
|
52
72
|
|
53
73
|
status
|
54
74
|
end
|
55
|
-
|
56
|
-
make_request_with_key "/service/order/status/#{remote_id}",
|
57
|
-
:via => :post,
|
58
|
-
:params => { :status => status_string },
|
59
|
-
:api_key => options[:api_key]
|
60
|
-
end
|
61
|
-
|
62
|
-
def status_history(order_id, options = {})
|
63
|
-
resp = make_request_with_key "/service/order/statushistory/#{order_id}",
|
64
|
-
:api_key => options[:api_key]
|
65
|
-
resp.order.date = Date.parse(resp.order.date)
|
66
|
-
resp
|
67
75
|
end
|
68
76
|
|
69
77
|
def make_request_with_key(service, options = {})
|
@@ -73,14 +81,17 @@ module CheckoutRu
|
|
73
81
|
end
|
74
82
|
|
75
83
|
def make_request(service, options = {})
|
76
|
-
|
77
|
-
|
78
|
-
|
84
|
+
headers = { 'Accept' => 'application/json' }
|
85
|
+
conn = options[:connection] || build_connection
|
86
|
+
method = options[:via] || :get
|
87
|
+
request_opts = options[:request] || {}
|
88
|
+
params = options[:params].dup if options[:params]
|
89
|
+
|
79
90
|
camelize_keys!(params)
|
80
91
|
|
81
|
-
body = conn.public_send(method, service, params,
|
82
|
-
|
83
|
-
|
92
|
+
body = conn.public_send(method, service, params, headers) do |req|
|
93
|
+
req.options.update(request_opts) unless request_opts.empty?
|
94
|
+
end.body
|
84
95
|
|
85
96
|
underscore_keys!(body)
|
86
97
|
|
@@ -5,6 +5,13 @@ class CheckoutRu::SessionTest < MiniTest::Test
|
|
5
5
|
@session = CheckoutRu::Session.new('valid-ticket')
|
6
6
|
end
|
7
7
|
|
8
|
+
def test_request_options_are_passed_through_to_faraday
|
9
|
+
CheckoutRu.expects(:make_request).once.with(
|
10
|
+
'/service/checkout/calculation', has_entry(:request, { :timeout => 5 })
|
11
|
+
)
|
12
|
+
@session.calculation({}, :request => { :timeout => 5 })
|
13
|
+
end
|
14
|
+
|
8
15
|
def test_get_places_by_query
|
9
16
|
VCR.use_cassette('get_places_by_query') do
|
10
17
|
places = @session.get_places_by_query(:place => 'москва')
|
data/test/checkout_ru_test.rb
CHANGED
@@ -39,7 +39,7 @@ class CheckoutRuTest < MiniTest::Test
|
|
39
39
|
|
40
40
|
def test_configured_api_key_is_used_to_make_request
|
41
41
|
CheckoutRu.stubs(:api_key).returns('foobar')
|
42
|
-
CheckoutRu.expects(:make_request).with(
|
42
|
+
CheckoutRu.expects(:make_request).with('/service/login/ticket/foobar', {}).
|
43
43
|
once.returns({})
|
44
44
|
CheckoutRu.get_ticket
|
45
45
|
end
|