checkout_ru 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fa9c6eba3494c7383ec333a3c72d02d4dba6675f
4
- data.tar.gz: 62b3cdf7e19e1881999ab6aca37ed5fc344a3d28
3
+ metadata.gz: b23edda19deacdfe9babd4cafcf0a15e7700d7ff
4
+ data.tar.gz: a147e7b78668a55c5a7fa02e5410e2ddee492311
5
5
  SHA512:
6
- metadata.gz: 780328c487265dbd1b45375ed5ec140bd693ed39d673a0600788687c2d3c6e163335cb156c1d5637beb16ed6e4e6389e068ff0f460a77e9b17f7f8effd7423d5
7
- data.tar.gz: 3c0c847e1e7bc4784533be58165de5bfd382842b482f102ed2e04718fcc45de78c9fe4a07a5bdaa3fdc10e798024cbde162947571971c8ea7dbd659da67597d9
6
+ metadata.gz: 2e4c620ab482da22d2db9e97248288fc2ae4cb5d80ef435262f400914569423dfa7d452e3bc8dc4dff02c34f833313316939d72eb6b264aba5491c8971de05e1
7
+ data.tar.gz: bedadeccfdcdfb72ee3bb6a729950c2d993b8502c3b35d372a189ac8f0320f09835e9e64789236667ed8a5dbd69cb2c32d2edcae59af2665ddf8cfb9a204ca59
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 0.3.0
2
+
3
+ - Make ticket auto-renewable (optional)
4
+ - Fix bug with order serialization
5
+ - Fix bug with recognizing expired ticket exception
6
+
7
+ ## 0.2.0
8
+
9
+ - Add ability to pass request options to api calls
10
+
1
11
  ## 0.1.0
2
12
 
3
13
  - Fix bug where CheckoutRu.api_key config didn't work
@@ -7,9 +7,10 @@ module CheckoutRu
7
7
  end
8
8
  end
9
9
 
10
+ attr_reader :ticket
11
+
10
12
  def initialize(ticket)
11
13
  @ticket = ticket
12
- @conn = CheckoutRu.build_connection
13
14
  end
14
15
 
15
16
  def get_places_by_query(params = {}, options = {})
@@ -34,12 +35,31 @@ module CheckoutRu
34
35
 
35
36
  private
36
37
  def get(service, params = {}, options = {})
37
- args = {
38
- :connection => @conn,
39
- :params => params.merge(:ticket => @ticket)
40
- }.merge(options)
41
-
38
+ session_renewal_count ||= 0
39
+ session_renewal_count += 1
40
+ args = {:params => params.merge(:ticket => @ticket)}.merge(options)
41
+ args[:connection] ||= build_connection
42
42
  CheckoutRu.make_request "/service/checkout/#{service}", args
43
+ rescue Faraday::Error::ClientError => e
44
+ if CheckoutRu.auto_renew_session &&
45
+ session_renewal_count < 2 && expired_ticket_exception?(e)
46
+
47
+ @ticket = CheckoutRu.get_ticket
48
+ retry
49
+ else
50
+ raise
51
+ end
52
+ end
53
+
54
+ def build_connection
55
+ @connection ||= CheckoutRu.build_connection
56
+ end
57
+
58
+ def expired_ticket_exception?(exception)
59
+ exception.respond_to?(:response) &&
60
+ exception.response.respond_to?(:[]) &&
61
+ exception.response[:status] == 500 &&
62
+ exception.response[:body] =~ /#{@ticket}\s+is\s+expired\s+or\s+invalid/
43
63
  end
44
64
  end
45
65
  end
@@ -1,3 +1,3 @@
1
1
  module CheckoutRu
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/checkout_ru.rb CHANGED
@@ -11,11 +11,14 @@ require 'checkout_ru/order'
11
11
 
12
12
  module CheckoutRu
13
13
  SERVICE_URL = 'http://platform.checkout.ru'.freeze
14
-
15
14
  Error = Class.new(Faraday::Error::ClientError)
16
15
 
17
16
  class << self
18
- attr_accessor :service_url, :api_key, :adapter
17
+ attr_accessor \
18
+ :service_url,
19
+ :api_key,
20
+ :adapter,
21
+ :auto_renew_session
19
22
 
20
23
  def get_ticket(options = {})
21
24
  key = options.delete(:api_key) || api_key
@@ -24,8 +27,8 @@ module CheckoutRu
24
27
 
25
28
  def create_order(order, options = {})
26
29
  args = {
27
- :via => :post,
28
- :params => { :order => order }
30
+ :via => :post,
31
+ :params => { :order => order.to_hash }
29
32
  }.merge(options)
30
33
 
31
34
  make_request_with_key '/service/order/create', args
@@ -34,7 +37,7 @@ module CheckoutRu
34
37
  def update_order(remote_id, order, options = {})
35
38
  args = {
36
39
  :via => :post,
37
- :params => { :order => order }
40
+ :params => { :order => order.to_hash }
38
41
  }.merge(options)
39
42
 
40
43
  make_request_with_key "/service/order/#{remote_id}", args
@@ -106,13 +109,13 @@ module CheckoutRu
106
109
  end
107
110
 
108
111
  def build_connection(options = {})
109
- url = options[:url] || service_url || SERVICE_URL
112
+ url = options[:url] || service_url
110
113
 
111
- Faraday.new(:url => url) do |faraday|
112
- faraday.request :multi_json
113
- faraday.response :raise_error
114
- faraday.response :multi_json
115
- faraday.adapter options[:adapter] || adapter || Faraday.default_adapter
114
+ Faraday.new(:url => url) do |conn|
115
+ conn.request :multi_json
116
+ conn.response :raise_error
117
+ conn.response :multi_json
118
+ conn.adapter options[:adapter] || adapter
116
119
  end
117
120
  end
118
121
 
@@ -148,4 +151,9 @@ module CheckoutRu
148
151
  end
149
152
  end
150
153
  end
154
+
155
+ # Defaults
156
+ self.adapter = Faraday.default_adapter
157
+ self.auto_renew_session = true
158
+ self.service_url = SERVICE_URL
151
159
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: checkout_ru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxim Chernyak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-06 00:00:00.000000000 Z
11
+ date: 2014-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday