mangopay 3.0.23 → 3.0.25.pre.alpha.pre.20

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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +28 -26
  3. data/.rspec +2 -2
  4. data/.travis.yml +13 -0
  5. data/Gemfile +2 -2
  6. data/LICENSE +20 -20
  7. data/README.md +126 -123
  8. data/bin/mangopay +9 -9
  9. data/lib/generators/mangopay/install_generator.rb +60 -60
  10. data/lib/generators/templates/mangopay.rb.erb +5 -5
  11. data/lib/mangopay.rb +225 -158
  12. data/lib/mangopay/authorization_token.rb +88 -88
  13. data/lib/mangopay/bank_account.rb +38 -38
  14. data/lib/mangopay/card.rb +8 -8
  15. data/lib/mangopay/card_registration.rb +9 -9
  16. data/lib/mangopay/client.rb +74 -74
  17. data/lib/mangopay/dispute.rb +130 -130
  18. data/lib/mangopay/errors.rb +61 -61
  19. data/lib/mangopay/event.rb +18 -18
  20. data/lib/mangopay/filter_parameters.rb +46 -0
  21. data/lib/mangopay/hook.rb +9 -9
  22. data/lib/mangopay/http_calls.rb +85 -85
  23. data/lib/mangopay/json.rb +14 -14
  24. data/lib/mangopay/kyc_document.rb +70 -70
  25. data/lib/mangopay/legal_user.rb +15 -15
  26. data/lib/mangopay/mandate.rb +32 -32
  27. data/lib/mangopay/natural_user.rb +14 -14
  28. data/lib/mangopay/pay_in.rb +85 -73
  29. data/lib/mangopay/pay_out.rb +14 -14
  30. data/lib/mangopay/pre_authorization.rb +13 -13
  31. data/lib/mangopay/refund.rb +7 -7
  32. data/lib/mangopay/report.rb +17 -17
  33. data/lib/mangopay/resource.rb +21 -21
  34. data/lib/mangopay/temp.rb +74 -74
  35. data/lib/mangopay/transaction.rb +24 -24
  36. data/lib/mangopay/transfer.rb +9 -9
  37. data/lib/mangopay/user.rb +43 -35
  38. data/lib/mangopay/version.rb +3 -3
  39. data/lib/mangopay/wallet.rb +17 -17
  40. data/mangopay.gemspec +31 -30
  41. data/spec/mangopay/authorization_token_spec.rb +70 -70
  42. data/spec/mangopay/bank_account_spec.rb +97 -97
  43. data/spec/mangopay/card_registration_spec.rb +73 -73
  44. data/spec/mangopay/client_spec.rb +110 -110
  45. data/spec/mangopay/configuration_spec.rb +95 -18
  46. data/spec/mangopay/dispute_spec.rb +262 -262
  47. data/spec/mangopay/event_spec.rb +31 -31
  48. data/spec/mangopay/fetch_filters_spec.rb +63 -63
  49. data/spec/mangopay/hook_spec.rb +37 -37
  50. data/spec/mangopay/idempotency_spec.rb +41 -41
  51. data/spec/mangopay/kyc_document_spec.rb +103 -103
  52. data/spec/mangopay/log_requests_filter_spec.rb +25 -0
  53. data/spec/mangopay/mandate_spec.rb +92 -92
  54. data/spec/mangopay/payin_bankwire_direct_spec.rb +74 -74
  55. data/spec/mangopay/payin_card_direct_spec.rb +68 -68
  56. data/spec/mangopay/payin_card_web_spec.rb +38 -38
  57. data/spec/mangopay/payin_directdebit_direct_spec.rb +37 -37
  58. data/spec/mangopay/payin_directdebit_web_spec.rb +38 -38
  59. data/spec/mangopay/payin_paypal_web_spec.rb +38 -0
  60. data/spec/mangopay/payin_preauthorized_direct_spec.rb +68 -68
  61. data/spec/mangopay/payout_bankwire_spec.rb +54 -54
  62. data/spec/mangopay/preauthorization_spec.rb +42 -42
  63. data/spec/mangopay/refund_spec.rb +21 -21
  64. data/spec/mangopay/report_spec.rb +39 -39
  65. data/spec/mangopay/shared_resources.rb +381 -365
  66. data/spec/mangopay/temp_paymentcard_spec.rb +31 -31
  67. data/spec/mangopay/transaction_spec.rb +54 -54
  68. data/spec/mangopay/transfer_spec.rb +69 -69
  69. data/spec/mangopay/user_spec.rb +137 -122
  70. data/spec/mangopay/wallet_spec.rb +80 -80
  71. data/spec/spec_helper.rb +31 -31
  72. metadata +11 -5
@@ -1,5 +1,5 @@
1
- MangoPay.configure do |c|
2
- c.preproduction = <%= options[:preproduction] %>
3
- c.client_id = '<%= @client_id %>'
4
- c.client_passphrase = '<%= @client_passphrase %>'
5
- end
1
+ MangoPay.configure do |c|
2
+ c.preproduction = <%= options[:preproduction] %>
3
+ c.client_id = '<%= @client_id %>'
4
+ c.client_passphrase = '<%= @client_passphrase %>'
5
+ end
data/lib/mangopay.rb CHANGED
@@ -1,158 +1,225 @@
1
- require 'net/http'
2
- require 'cgi/util'
3
- require 'digest/md5'
4
- require 'multi_json'
5
-
6
- # helpers
7
- require 'mangopay/version'
8
- require 'mangopay/errors'
9
-
10
-
11
- module MangoPay
12
-
13
- autoload :HTTPCalls, 'mangopay/http_calls'
14
- autoload :Resource, 'mangopay/resource'
15
- autoload :Client, 'mangopay/client'
16
- autoload :User, 'mangopay/user'
17
- autoload :NaturalUser, 'mangopay/natural_user'
18
- autoload :LegalUser, 'mangopay/legal_user'
19
- autoload :PayIn, 'mangopay/pay_in'
20
- autoload :PayOut, 'mangopay/pay_out'
21
- autoload :Transfer, 'mangopay/transfer'
22
- autoload :Transaction, 'mangopay/transaction'
23
- autoload :Wallet, 'mangopay/wallet'
24
- autoload :BankAccount, 'mangopay/bank_account'
25
- autoload :CardRegistration, 'mangopay/card_registration'
26
- autoload :PreAuthorization, 'mangopay/pre_authorization'
27
- autoload :Card, 'mangopay/card'
28
- autoload :Event, 'mangopay/event'
29
- autoload :KycDocument, 'mangopay/kyc_document'
30
- autoload :Hook, 'mangopay/hook'
31
- autoload :Refund, 'mangopay/refund'
32
- autoload :Dispute, 'mangopay/dispute'
33
- autoload :Mandate, 'mangopay/mandate'
34
- autoload :Report, 'mangopay/report'
35
- autoload :JSON, 'mangopay/json'
36
- autoload :AuthorizationToken, 'mangopay/authorization_token'
37
-
38
- # temporary
39
- autoload :Temp, 'mangopay/temp'
40
-
41
- class Configuration
42
- attr_accessor :preproduction, :root_url,
43
- :client_id, :client_passphrase,
44
- :temp_dir
45
-
46
- def preproduction
47
- @preproduction || false
48
- end
49
-
50
- def root_url
51
- @root_url || (@preproduction == true ? "https://api.sandbox.mangopay.com" : "https://api.mangopay.com")
52
- end
53
- end
54
-
55
- class << self
56
- attr_accessor :configuration
57
-
58
- def version_code
59
- "v2.01"
60
- end
61
-
62
- def api_path
63
- "/#{version_code}/#{MangoPay.configuration.client_id}"
64
- end
65
-
66
- def configure
67
- self.configuration ||= Configuration.new
68
- yield configuration
69
- end
70
-
71
- def api_uri(url='')
72
- URI(configuration.root_url + url)
73
- end
74
-
75
- #
76
- # - +method+: HTTP method; lowercase symbol, e.g. :get, :post etc.
77
- # - +url+: the part after Configuration#root_url
78
- # - +params+: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body
79
- # - +filters+: hash; pagination params etc.; will encode it by URI and assign to URI#query
80
- # - +headers_or_idempotency_key+: hash of headers; or replaced by request_headers if nil; or added to request_headers as idempotency key otherwise (see https://docs.mangopay.com/api-references/idempotency-support/)
81
- # - +before_request_proc+: optional proc; will call it passing the Net::HTTPRequest instance just before Net::HTTPRequest#request
82
- #
83
- # Raises MangoPay::ResponseError if response code != 200.
84
- #
85
- def request(method, url, params={}, filters={}, headers_or_idempotency_key = nil, before_request_proc = nil)
86
- uri = api_uri(url)
87
- uri.query = URI.encode_www_form(filters) unless filters.empty?
88
-
89
- if headers_or_idempotency_key.is_a?(Hash)
90
- headers = headers_or_idempotency_key
91
- else
92
- headers = request_headers
93
- headers['Idempotency-Key'] = headers_or_idempotency_key if headers_or_idempotency_key != nil
94
- end
95
-
96
- res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
97
- req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
98
- req.body = JSON.dump(params)
99
- before_request_proc.call(req) if before_request_proc
100
- http.request req
101
- end
102
-
103
- # decode json data
104
- data = JSON.load(res.body.to_s) rescue {}
105
-
106
- unless res.is_a?(Net::HTTPOK)
107
- raise MangoPay::ResponseError.new(uri, res.code, data)
108
- end
109
-
110
- # copy pagination info if any
111
- ['x-number-of-pages', 'x-number-of-items'].each { |k|
112
- filters[k.gsub('x-number-of-', 'total_')] = res[k].to_i if res[k]
113
- }
114
-
115
- data
116
- end
117
-
118
- # Retrieve a previous response by idempotency_key
119
- # See https://docs.mangopay.com/api-references/idempotency-support/
120
- def fetch_response(idempotency_key)
121
- url = "#{api_path}/responses/#{idempotency_key}"
122
- request(:get, url)
123
- end
124
-
125
- private
126
-
127
- def user_agent
128
- {
129
- bindings_version: VERSION,
130
- lang: 'ruby',
131
- lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
132
- platform: RUBY_PLATFORM,
133
- uname: get_uname
134
- }
135
- end
136
-
137
- def get_uname
138
- `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
139
- rescue Errno::ENOMEM
140
- 'uname lookup failed'
141
- end
142
-
143
- def request_headers
144
- auth_token = AuthorizationToken::Manager.get_token
145
- headers = {
146
- 'user_agent' => "MangoPay V2 RubyBindings/#{VERSION}",
147
- 'Authorization' => "#{auth_token['token_type']} #{auth_token['access_token']}",
148
- 'Content-Type' => 'application/json'
149
- }
150
- begin
151
- headers.update('x_mangopay_client_user_agent' => JSON.dump(user_agent))
152
- rescue => e
153
- headers.update('x_mangopay_client_raw_user_agent' => user_agent.inspect, error: "#{e} (#{e.class})")
154
- end
155
- end
156
-
157
- end
158
- end
1
+ require 'net/http'
2
+ require 'cgi/util'
3
+ require 'digest/md5'
4
+ require 'multi_json'
5
+ require 'benchmark'
6
+ require 'logger'
7
+
8
+ # helpers
9
+ require 'mangopay/version'
10
+ require 'mangopay/errors'
11
+
12
+
13
+ module MangoPay
14
+
15
+ autoload :HTTPCalls, 'mangopay/http_calls'
16
+ autoload :Resource, 'mangopay/resource'
17
+ autoload :Client, 'mangopay/client'
18
+ autoload :User, 'mangopay/user'
19
+ autoload :NaturalUser, 'mangopay/natural_user'
20
+ autoload :LegalUser, 'mangopay/legal_user'
21
+ autoload :PayIn, 'mangopay/pay_in'
22
+ autoload :PayOut, 'mangopay/pay_out'
23
+ autoload :Transfer, 'mangopay/transfer'
24
+ autoload :Transaction, 'mangopay/transaction'
25
+ autoload :Wallet, 'mangopay/wallet'
26
+ autoload :BankAccount, 'mangopay/bank_account'
27
+ autoload :CardRegistration, 'mangopay/card_registration'
28
+ autoload :PreAuthorization, 'mangopay/pre_authorization'
29
+ autoload :Card, 'mangopay/card'
30
+ autoload :Event, 'mangopay/event'
31
+ autoload :KycDocument, 'mangopay/kyc_document'
32
+ autoload :Hook, 'mangopay/hook'
33
+ autoload :Refund, 'mangopay/refund'
34
+ autoload :Dispute, 'mangopay/dispute'
35
+ autoload :Mandate, 'mangopay/mandate'
36
+ autoload :Report, 'mangopay/report'
37
+ autoload :JSON, 'mangopay/json'
38
+ autoload :AuthorizationToken, 'mangopay/authorization_token'
39
+ autoload :FilterParameters, 'mangopay/filter_parameters'
40
+
41
+ # temporary
42
+ autoload :Temp, 'mangopay/temp'
43
+
44
+ class Configuration
45
+ attr_accessor :preproduction, :root_url,
46
+ :client_id, :client_passphrase,
47
+ :temp_dir, :log_file
48
+
49
+ def preproduction
50
+ @preproduction || false
51
+ end
52
+
53
+ def root_url
54
+ @root_url || (@preproduction == true ? "https://api.sandbox.mangopay.com" : "https://api.mangopay.com")
55
+ end
56
+ end
57
+
58
+ class << self
59
+ def version_code
60
+ "v2.01"
61
+ end
62
+
63
+ def api_path
64
+ "/#{version_code}/#{MangoPay.configuration.client_id}"
65
+ end
66
+
67
+ def api_uri(url='')
68
+ URI(configuration.root_url + url)
69
+ end
70
+
71
+ def configuration=(value)
72
+ Thread.current[:mangopay_configuration] = value
73
+ @last_configuration_set = value
74
+ end
75
+
76
+ def configuration
77
+ config = Thread.current[:mangopay_configuration]
78
+
79
+ config ||
80
+ ( @last_configuration_set &&
81
+ (self.configuration = @last_configuration_set.dup)) ||
82
+ (self.configuration = MangoPay::Configuration.new)
83
+ end
84
+
85
+ def configure
86
+ config = self.configuration
87
+ yield config
88
+ self.configuration = config
89
+ end
90
+
91
+ def with_configuration(config)
92
+ original_config = MangoPay.configuration
93
+ MangoPay.configuration = config
94
+ yield
95
+ ensure
96
+ MangoPay.configuration = original_config
97
+ end
98
+
99
+ #
100
+ # - +method+: HTTP method; lowercase symbol, e.g. :get, :post etc.
101
+ # - +url+: the part after Configuration#root_url
102
+ # - +params+: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body
103
+ # - +filters+: hash; pagination params etc.; will encode it by URI and assign to URI#query
104
+ # - +headers_or_idempotency_key+: hash of headers; or replaced by request_headers if nil; or added to request_headers as idempotency key otherwise (see https://docs.mangopay.com/api-references/idempotency-support/)
105
+ # - +before_request_proc+: optional proc; will call it passing the Net::HTTPRequest instance just before Net::HTTPRequest#request
106
+ #
107
+ # Raises MangoPay::ResponseError if response code != 200.
108
+ #
109
+ def request(method, url, params={}, filters={}, headers_or_idempotency_key = nil, before_request_proc = nil)
110
+ uri = api_uri(url)
111
+ uri.query = URI.encode_www_form(filters) unless filters.empty?
112
+
113
+ if headers_or_idempotency_key.is_a?(Hash)
114
+ headers = headers_or_idempotency_key
115
+ else
116
+ headers = request_headers
117
+ headers['Idempotency-Key'] = headers_or_idempotency_key if headers_or_idempotency_key != nil
118
+ end
119
+
120
+ res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
121
+ req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
122
+ req.body = JSON.dump(params)
123
+ before_request_proc.call(req) if before_request_proc
124
+ do_request(http, req, uri)
125
+ end
126
+
127
+ # decode json data
128
+ data = res.body.to_s.empty? ? {} : JSON.load(res.body.to_s)
129
+
130
+ unless res.is_a?(Net::HTTPOK)
131
+ raise MangoPay::ResponseError.new(uri, res.code, data)
132
+ end
133
+
134
+ # copy pagination info if any
135
+ ['x-number-of-pages', 'x-number-of-items'].each { |k|
136
+ filters[k.gsub('x-number-of-', 'total_')] = res[k].to_i if res[k]
137
+ }
138
+
139
+ data
140
+ end
141
+
142
+ # Retrieve a previous response by idempotency_key
143
+ # See https://docs.mangopay.com/api-references/idempotency-support/
144
+ def fetch_response(idempotency_key)
145
+ url = "#{api_path}/responses/#{idempotency_key}"
146
+ request(:get, url)
147
+ end
148
+
149
+ private
150
+
151
+ def user_agent
152
+ {
153
+ bindings_version: VERSION,
154
+ lang: 'ruby',
155
+ lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
156
+ platform: RUBY_PLATFORM,
157
+ uname: get_uname
158
+ }
159
+ end
160
+
161
+ def get_uname
162
+ `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
163
+ rescue Errno::ENOMEM
164
+ 'uname lookup failed'
165
+ end
166
+
167
+ def request_headers
168
+ auth_token = AuthorizationToken::Manager.get_token
169
+ headers = {
170
+ 'user_agent' => "MangoPay V2 RubyBindings/#{VERSION}",
171
+ 'Authorization' => "#{auth_token['token_type']} #{auth_token['access_token']}",
172
+ 'Content-Type' => 'application/json'
173
+ }
174
+ begin
175
+ headers.update('x_mangopay_client_user_agent' => JSON.dump(user_agent))
176
+ rescue => e
177
+ headers.update('x_mangopay_client_raw_user_agent' => user_agent.inspect, error: "#{e} (#{e.class})")
178
+ end
179
+ end
180
+
181
+ def do_request(http, req, uri)
182
+ if configuration.log_file.nil?
183
+ do_request_without_log(http, req)
184
+ else
185
+ do_request_with_log(http, req, uri)
186
+ end
187
+ end
188
+
189
+ def do_request_with_log(http, req, uri)
190
+ res, time = nil, nil
191
+ params = FilterParameters.request(req.body)
192
+ line = "[#{Time.now.iso8601}] #{req.method.upcase} \"#{uri.to_s}\" #{params}"
193
+ begin
194
+ time = Benchmark.realtime { res = do_request_without_log(http, req) }
195
+ res
196
+ ensure
197
+ params = FilterParameters.response(res.body)
198
+ line = "#{log_severity(res)} #{line}"
199
+ line += "\n [#{(time * 1000).round(1)}ms] #{res.code} #{params}\n"
200
+ logger.info { line }
201
+ end
202
+ end
203
+
204
+ def do_request_without_log(http, req)
205
+ http.request(req)
206
+ end
207
+
208
+ def log_severity(res)
209
+ errors = [Net::HTTPClientError, Net::HTTPServerError, Net::HTTPUnknownResponse]
210
+ errors.any? { |klass| res.is_a?(klass) } ? 'E' : 'I'
211
+ end
212
+
213
+ def logger
214
+ raise NotImplementedError if configuration.log_file.nil?
215
+ if @logger.nil?
216
+ @logger = Logger.new(configuration.log_file)
217
+ @logger.formatter = proc do |severity, datetime, progname, msg|
218
+ "#{msg}\n"
219
+ end
220
+ end
221
+ @logger
222
+ end
223
+
224
+ end
225
+ end
@@ -1,88 +1,88 @@
1
- module MangoPay
2
- module AuthorizationToken
3
-
4
- # See http://docs.mangopay.com/api-references/authenticating/
5
- class Manager
6
-
7
- class << self
8
- def storage
9
- @@storage ||= StaticStorage.new
10
- end
11
-
12
- def storage= (storage)
13
- @@storage = storage
14
- end
15
-
16
- def get_token
17
- token = storage.get
18
- env_key = get_environment_key_for_token
19
- if token.nil? || token['timestamp'].nil? || token['timestamp'] <= Time.now || token['environment_key'] != env_key
20
- token = MangoPay.request(:post, "/#{MangoPay.version_code}/oauth/token", {}, {}, {}, Proc.new do |req|
21
- cfg = MangoPay.configuration
22
- req.basic_auth cfg.client_id, cfg.client_passphrase
23
- req.body = 'grant_type=client_credentials'
24
- end)
25
- token['timestamp'] = Time.now + token['expires_in'].to_i
26
- token['environment_key'] = env_key
27
- storage.store token
28
- end
29
- token
30
- end
31
-
32
- def get_environment_key_for_token
33
- cfg = MangoPay.configuration
34
- key = "#{cfg.root_url}|#{cfg.client_id}|#{cfg.client_passphrase}"
35
- key = Digest::MD5.hexdigest(key)
36
- key
37
- end
38
- end
39
- end
40
-
41
- class StaticStorage
42
- def get
43
- @@token ||= nil
44
- end
45
-
46
- def store(token)
47
- @@token = token
48
- end
49
- end
50
-
51
- class FileStorage
52
- require 'yaml'
53
- @temp_dir
54
-
55
- def initialize(temp_dir = nil)
56
- @temp_dir = temp_dir || MangoPay.configuration.temp_dir
57
- if !@temp_dir
58
- raise "Path to temporary folder is not defined"
59
- end
60
- end
61
-
62
- def get
63
- begin
64
- f = File.open(file_path, File::RDONLY)
65
- f.flock(File::LOCK_SH)
66
- txt = f.read
67
- f.close
68
- YAML.load(txt) || nil
69
- rescue Errno::ENOENT
70
- nil
71
- end
72
- end
73
-
74
- def store(token)
75
- File.open(file_path, File::RDWR|File::CREAT, 0644) do |f|
76
- f.flock(File::LOCK_EX)
77
- f.truncate(0)
78
- f.rewind
79
- f.puts(YAML.dump(token))
80
- end
81
- end
82
-
83
- def file_path
84
- File.join(@temp_dir, "MangoPay.AuthorizationToken.FileStore.tmp")
85
- end
86
- end
87
- end
88
- end
1
+ module MangoPay
2
+ module AuthorizationToken
3
+
4
+ # See http://docs.mangopay.com/api-references/authenticating/
5
+ class Manager
6
+
7
+ class << self
8
+ def storage
9
+ @@storage ||= StaticStorage.new
10
+ end
11
+
12
+ def storage= (storage)
13
+ @@storage = storage
14
+ end
15
+
16
+ def get_token
17
+ token = storage.get
18
+ env_key = get_environment_key_for_token
19
+ if token.nil? || token['timestamp'].nil? || token['timestamp'] <= Time.now || token['environment_key'] != env_key
20
+ token = MangoPay.request(:post, "/#{MangoPay.version_code}/oauth/token", {}, {}, {}, Proc.new do |req|
21
+ cfg = MangoPay.configuration
22
+ req.basic_auth cfg.client_id, cfg.client_passphrase
23
+ req.body = 'grant_type=client_credentials'
24
+ end)
25
+ token['timestamp'] = Time.now + (token['expires_in'].to_i - 10)
26
+ token['environment_key'] = env_key
27
+ storage.store token
28
+ end
29
+ token
30
+ end
31
+
32
+ def get_environment_key_for_token
33
+ cfg = MangoPay.configuration
34
+ key = "#{cfg.root_url}|#{cfg.client_id}|#{cfg.client_passphrase}"
35
+ key = Digest::MD5.hexdigest(key)
36
+ key
37
+ end
38
+ end
39
+ end
40
+
41
+ class StaticStorage
42
+ def get
43
+ @@token ||= nil
44
+ end
45
+
46
+ def store(token)
47
+ @@token = token
48
+ end
49
+ end
50
+
51
+ class FileStorage
52
+ require 'yaml'
53
+ @temp_dir
54
+
55
+ def initialize(temp_dir = nil)
56
+ @temp_dir = temp_dir || MangoPay.configuration.temp_dir
57
+ if !@temp_dir
58
+ raise "Path to temporary folder is not defined"
59
+ end
60
+ end
61
+
62
+ def get
63
+ begin
64
+ f = File.open(file_path, File::RDONLY)
65
+ f.flock(File::LOCK_SH)
66
+ txt = f.read
67
+ f.close
68
+ YAML.load(txt) || nil
69
+ rescue Errno::ENOENT
70
+ nil
71
+ end
72
+ end
73
+
74
+ def store(token)
75
+ File.open(file_path, File::RDWR|File::CREAT, 0644) do |f|
76
+ f.flock(File::LOCK_EX)
77
+ f.truncate(0)
78
+ f.rewind
79
+ f.puts(YAML.dump(token))
80
+ end
81
+ end
82
+
83
+ def file_path
84
+ File.join(@temp_dir, "MangoPay.AuthorizationToken.FileStore.tmp")
85
+ end
86
+ end
87
+ end
88
+ end