checkout_ru 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 24682ee5c93e589612160abcb3f699cd34d1fd84
4
- data.tar.gz: ca9124080594ecdbb1640350c8133daf0936103a
3
+ metadata.gz: fa9c6eba3494c7383ec333a3c72d02d4dba6675f
4
+ data.tar.gz: 62b3cdf7e19e1881999ab6aca37ed5fc344a3d28
5
5
  SHA512:
6
- metadata.gz: 6c45a9296349bf629a3834ada08a6521e334fdf888df0570ca54e9b39501ffeabcef9af763443e6271ef63e666333e2d8c2b1fd3839d318265251c364f0aebf1
7
- data.tar.gz: fac3ed5091be006bcf7ce0ce814818235e62ce573f7657e0045c05a69c800a6bc8cac2be7a94fe13808f4329d7ad00b60184aeb6107509e6187584b6fe5c42c0
6
+ metadata.gz: 780328c487265dbd1b45375ed5ec140bd693ed39d673a0600788687c2d3c6e163335cb156c1d5637beb16ed6e4e6389e068ff0f460a77e9b17f7f8effd7423d5
7
+ data.tar.gz: 3c0c847e1e7bc4784533be58165de5bfd382842b482f102ed2e04718fcc45de78c9fe4a07a5bdaa3fdc10e798024cbde162947571971c8ea7dbd659da67597d9
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.1.0
2
+
3
+ - Fix bug where CheckoutRu.api_key config didn't work
4
+ - Stop rescuing Faraday errors, instead pass them through
5
+
1
6
  ## 0.0.2
2
7
 
3
8
  - Fix Faraday dependency issue
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(
@@ -7,40 +7,39 @@ module CheckoutRu
7
7
  end
8
8
  end
9
9
 
10
- def initialize(ticket, options = {})
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('checkout/getPlacesByQuery', options).suggestions
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 = options.dup
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 = options.dup
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 = options.dup
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('checkout/getPlaceByPostalCode', options)
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
- CheckoutRu.make_request \
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
@@ -1,3 +1,3 @@
1
1
  module CheckoutRu
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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[:api_key] || api_key
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
- make_request_with_key '/service/order/create',
26
+ args = {
27
27
  :via => :post,
28
- :params => { :order => order },
29
- :api_key => options[:api_key]
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
- make_request_with_key "/service/order/#{remote_id}",
35
+ args = {
34
36
  :via => :post,
35
- :params => { :order => order },
36
- :api_key => options[:api_key]
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
- status_string = if status.is_a?(Symbol)
43
- unless status_map.keys.include?(status)
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
- status_map[status]
67
+ Order::Status::MAP[status]
48
68
  else
49
- unless status_map.values.include?(status)
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
- conn = options[:connection] || build_connection
77
- method = options[:via] || :get
78
- params = options[:params].dup if options[:params]
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
- { 'Accept' => 'application/json' }
83
- ).body
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 => 'москва')
@@ -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("/service/login/ticket/foobar").
42
+ CheckoutRu.expects(:make_request).with('/service/login/ticket/foobar', {}).
43
43
  once.returns({})
44
44
  CheckoutRu.get_ticket
45
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: checkout_ru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxim Chernyak