mangopay 3.0.0 → 3.0.1

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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +26 -24
  3. data/.rspec +2 -2
  4. data/Gemfile +2 -2
  5. data/LICENSE +20 -20
  6. data/README.md +67 -67
  7. data/bin/mangopay +9 -9
  8. data/lib/generators/mangopay/install_generator.rb +60 -60
  9. data/lib/generators/templates/mangopay.rb +5 -5
  10. data/lib/mangopay.rb +118 -115
  11. data/lib/mangopay/bank_account.rb +21 -21
  12. data/lib/mangopay/card.rb +5 -0
  13. data/lib/mangopay/card_registration.rb +7 -0
  14. data/lib/mangopay/client.rb +17 -17
  15. data/lib/mangopay/errors.rb +4 -4
  16. data/lib/mangopay/http_calls.rb +53 -53
  17. data/lib/mangopay/json.rb +21 -21
  18. data/lib/mangopay/legal_user.rb +14 -14
  19. data/lib/mangopay/natural_user.rb +14 -14
  20. data/lib/mangopay/payin.rb +27 -17
  21. data/lib/mangopay/payout.rb +15 -15
  22. data/lib/mangopay/resource.rb +22 -22
  23. data/lib/mangopay/transaction.rb +11 -11
  24. data/lib/mangopay/transfer.rb +7 -7
  25. data/lib/mangopay/user.rb +7 -7
  26. data/lib/mangopay/version.rb +3 -3
  27. data/lib/mangopay/wallet.rb +7 -7
  28. data/mangopay.gemspec +33 -33
  29. data/spec/lib/mangopay/bank_account_spec.rb +26 -26
  30. data/spec/lib/mangopay/card_registration_spec.rb +40 -0
  31. data/spec/lib/mangopay/card_spec.rb +35 -0
  32. data/spec/lib/mangopay/client_spec.rb +28 -27
  33. data/spec/lib/mangopay/payin_spec.rb +34 -31
  34. data/spec/lib/mangopay/payout_spec.rb +34 -24
  35. data/spec/lib/mangopay/shared_resources.rb +193 -183
  36. data/spec/lib/mangopay/transaction_spec.rb +14 -14
  37. data/spec/lib/mangopay/transfer_spec.rb +35 -32
  38. data/spec/lib/mangopay/user_spec.rb +58 -58
  39. data/spec/lib/mangopay/wallet_spec.rb +32 -32
  40. data/spec/spec_helper.rb +13 -13
  41. metadata +8 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70750c7027d98926cc945a63e4a3ec94b72da433
4
- data.tar.gz: 7804bbb5e621985168b1956879efd2a05793708e
3
+ metadata.gz: 67c10c6356d088795e976707bb179e568a7f5368
4
+ data.tar.gz: dcd3cd6d68b8012d37752e00d6f6edf15b1b8223
5
5
  SHA512:
6
- metadata.gz: a771d8de26c6dbec506734008b57bbd7ad133a48ec8feba3af8a90d545f209a0574d64723460157ea742a69c82c5be0c8980e7ca40beef7dfe0c6053f329cb80
7
- data.tar.gz: 65b199274b1b0f8c975fc6021a03f9a9c99da6d5ef3c94fe4ef7fae95149474c995c83c189dd4df1c93fabd971c99e2bef326f4f2c4f5850990b454187230b4c
6
+ metadata.gz: 841c0922c44245dfe8505dc25756fb3f82621b2823e4993a0ccd3c19248972068102508491ef391c19b6918de7b504746de71d0d61998635552d854628e4aa8c
7
+ data.tar.gz: 4497c0d2305ac47148712956bcf071f56b69ce2c73e2ca287296c42404cf020967372130b4b92e9ae13d4d82b05663e5c06da50f595e01c43333374b1dae2815
data/.gitignore CHANGED
@@ -1,24 +1,26 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- coverage
6
- InstalledFiles
7
- lib/bundler/man
8
- pkg
9
- rdoc
10
- spec/reports
11
- test/tmp
12
- test/version_tmp
13
- tmp
14
- tags
15
-
16
- # YARD artifacts
17
- .yardoc
18
- _yardoc
19
- doc/
20
-
21
- .DS_Store
22
- .rvmrc
23
- /Gemfile.lock
24
- /mangopay-*.gem
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ tags
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
20
+
21
+ .DS_Store
22
+ .rvmrc
23
+ /Gemfile.lock
24
+ /mangopay-*.gem
25
+
26
+ /nbproject/
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
- --color
2
- --profile
1
+ --color
2
+ --profile
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source "https://rubygems.org"
2
- gemspec
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE CHANGED
@@ -1,20 +1,20 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2013 Mangopay
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of
6
- this software and associated documentation files (the "Software"), to deal in
7
- the Software without restriction, including without limitation the rights to
8
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
- the Software, and to permit persons to whom the Software is furnished to do so,
10
- subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Mangopay
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,67 +1,67 @@
1
- # MangoPay2 Ruby SDK
2
-
3
- The gem for interacting with the version 2 of the MangoPay API.
4
- See the [API documentation]() for more details on the API.
5
-
6
- Tested on the following versions of Ruby: 1.9.2, 1.9.3, 2.0.0
7
-
8
- ## Code Status
9
- [![Build Status]()]()
10
- [![Code Climate]()]()
11
- [![Dependency Status]()]()
12
-
13
- ## NEWS
14
-
15
- ### Version 3.0.0
16
- ** BREAKING CHANGES **
17
- This version of the gem is targeting the MangoPay API Version 2.
18
- It has a brand new structure to make the api calls easier to use
19
-
20
- ## Usage
21
-
22
- ### Install
23
- * Install the gem by either running ```gem install mangopay``` or by adding it to your Gemfile ```gem 'mangopay'```
24
-
25
- * The Rails users will be happy to know that there is a new generator script that will help you configure your access to the MangoPay API version 2.
26
- Simply run ``rails generate mangopay:install CLIENT_ID CLIENT_NAME`` where CLIENT_ID is the id you will use to connect to the api and CLIENT_NAME is a full name that will be use to identify all communications between you and the MangoPay Team.
27
-
28
- ### Examples
29
-
30
-
31
- ### Tests
32
- Make sure that you have run: ```bundle install```
33
- Then you just have to run rspec ```rspec``` to run all the test suite.
34
- Feel free to report any test failure by creating an issue on the [Gem's Github](https://github.com/MangoPay/mangopay2-ruby-sdk/issues)
35
-
36
- ## Contributing
37
-
38
- 1. Fork the repo.
39
-
40
- 2. Run the tests. We only take pull requests with passing tests, and it's great
41
- to know that you have a clean slate: `bundle && bundle exec rake`
42
-
43
- 3. Add a test for your change. Only refactoring and documentation changes
44
- require no new tests. If you are adding functionality or fixing a bug, we need
45
- a test!
46
-
47
- 4. Make the test pass.
48
-
49
- 5. Push to your fork and submit a pull request.
50
-
51
- At this point you're waiting on us. We like to at least comment on, if not
52
- accept, pull requests within three business days (and, typically, one business
53
- day). We may suggest some changes or improvements or alternatives.
54
-
55
- Syntax:
56
-
57
- * Two spaces, no tabs.
58
- * No trailing whitespace. Blank lines should not have any space.
59
- * Prefer &&/|| over and/or.
60
- * MyClass.my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
61
- * a = b and not a=b.
62
- * Follow the conventions you see used in the source already.
63
-
64
- A contribution can also be as simple as a +1 on issues tickets to show us what you would like to see in this gem.
65
-
66
- That's it for now. Good Hacking...
67
- >>>>>>> dev
1
+ # MangoPay2 Ruby SDK
2
+
3
+ The gem for interacting with the version 2 of the MangoPay API.
4
+ See the [API documentation]() for more details on the API.
5
+
6
+ Tested on the following versions of Ruby: 1.9.2, 1.9.3, 2.0.0
7
+
8
+ ## Code Status
9
+ [![Build Status]()]()
10
+ [![Code Climate]()]()
11
+ [![Dependency Status]()]()
12
+
13
+ ## NEWS
14
+
15
+ ### Version 3.0.0
16
+ ** BREAKING CHANGES **
17
+ This version of the gem is targeting the MangoPay API Version 2.
18
+ It has a brand new structure to make the api calls easier to use
19
+
20
+ ## Usage
21
+
22
+ ### Install
23
+ * Install the gem by either running ```gem install mangopay``` or by adding it to your Gemfile ```gem 'mangopay'```
24
+
25
+ * The Rails users will be happy to know that there is a new generator script that will help you configure your access to the MangoPay API version 2.
26
+ Simply run ``rails generate mangopay:install CLIENT_ID CLIENT_NAME`` where CLIENT_ID is the id you will use to connect to the api and CLIENT_NAME is a full name that will be use to identify all communications between you and the MangoPay Team.
27
+
28
+ ### Examples
29
+
30
+
31
+ ### Tests
32
+ Make sure that you have run: ```bundle install```
33
+ Then you just have to run rspec ```rspec``` to run all the test suite.
34
+ Feel free to report any test failure by creating an issue on the [Gem's Github](https://github.com/MangoPay/mangopay2-ruby-sdk/issues)
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork the repo.
39
+
40
+ 2. Run the tests. We only take pull requests with passing tests, and it's great
41
+ to know that you have a clean slate: `bundle && bundle exec rake`
42
+
43
+ 3. Add a test for your change. Only refactoring and documentation changes
44
+ require no new tests. If you are adding functionality or fixing a bug, we need
45
+ a test!
46
+
47
+ 4. Make the test pass.
48
+
49
+ 5. Push to your fork and submit a pull request.
50
+
51
+ At this point you're waiting on us. We like to at least comment on, if not
52
+ accept, pull requests within three business days (and, typically, one business
53
+ day). We may suggest some changes or improvements or alternatives.
54
+
55
+ Syntax:
56
+
57
+ * Two spaces, no tabs.
58
+ * No trailing whitespace. Blank lines should not have any space.
59
+ * Prefer &&/|| over and/or.
60
+ * MyClass.my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
61
+ * a = b and not a=b.
62
+ * Follow the conventions you see used in the source already.
63
+
64
+ A contribution can also be as simple as a +1 on issues tickets to show us what you would like to see in this gem.
65
+
66
+ That's it for now. Good Hacking...
67
+ >>>>>>> dev
@@ -1,9 +1,9 @@
1
- #!/usr/bin/env ruby
2
-
3
- irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
-
5
- libs = " -r irb/completion"
6
- libs << " -r #{File.dirname(__FILE__) + '/../lib/mangopay'}"
7
-
8
- puts "Loading MangoPay gem"
9
- exec "#{irb} #{libs}"
1
+ #!/usr/bin/env ruby
2
+
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/mangopay'}"
7
+
8
+ puts "Loading MangoPay gem"
9
+ exec "#{irb} #{libs}"
@@ -1,60 +1,60 @@
1
- require 'rails/generators/base'
2
- require 'mangopay'
3
-
4
- module Mangopay
5
- module Generators
6
- class InstallGenerator < Rails::Generators::Base
7
- source_root File.expand_path('../../templates', __FILE__)
8
- argument :client_id, type: :string,
9
- desc: 'The id you want to use to query the MangoPay API (must match with the regex ^[a-z0-9_-]{4,20}$)'
10
- argument :client_name, type: :string, desc: "Full name of you're organization"
11
- argument :client_email, type: :string, desc: "An email for future contacts"
12
- class_option :preproduction, type: :boolean, default: true, desc: 'Whether or not use the preproduction environment'
13
-
14
- desc 'Installs all the basic configuration of the mangopay gem'
15
- def setup
16
- begin
17
- client = client_id_valid?
18
- remove_file 'config/initializers/mangopay.rb'
19
- @client_id = client_id
20
- @client_passphrase = client['Passphrase']
21
- template 'mangopay.rb', 'config/initializers/mangopay.rb'
22
- rescue => e
23
- puts e.message
24
- end
25
- end
26
-
27
- protected
28
-
29
- def client_id_valid?
30
- check_client_id_validity
31
- check_client_id_availablility
32
- end
33
-
34
- def check_client_id_validity
35
- if (/^[a-z0-9_-]{4,20}$/ =~ client_id).nil?
36
- raise "The client_id must match the regexp ^[a-z0-9_-]{4,20}$"
37
- end
38
- end
39
-
40
- def check_client_id_availablility
41
- client = create_client
42
- if client['Type'] == 'ClientID_already_exist'
43
- raise client['Message']
44
- end
45
- client
46
- end
47
-
48
- def create_client
49
- MangoPay.configure do |c|
50
- c.preproduction = options[:preproduction]
51
- end
52
- MangoPay::Client.create({
53
- ClientID: client_id,
54
- Name: client_name,
55
- Email: client_email
56
- })
57
- end
58
- end
59
- end
60
- end
1
+ require 'rails/generators/base'
2
+ require 'mangopay'
3
+
4
+ module Mangopay
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../../templates', __FILE__)
8
+ argument :client_id, type: :string,
9
+ desc: 'The id you want to use to query the MangoPay API (must match with the regex ^[a-z0-9_-]{4,20}$)'
10
+ argument :client_name, type: :string, desc: "Full name of you're organization"
11
+ argument :client_email, type: :string, desc: "An email for future contacts"
12
+ class_option :preproduction, type: :boolean, default: true, desc: 'Whether or not use the preproduction environment'
13
+
14
+ desc 'Installs all the basic configuration of the mangopay gem'
15
+ def setup
16
+ begin
17
+ client = client_id_valid?
18
+ remove_file 'config/initializers/mangopay.rb'
19
+ @client_id = client_id
20
+ @client_passphrase = client['Passphrase']
21
+ template 'mangopay.rb', 'config/initializers/mangopay.rb'
22
+ rescue => e
23
+ puts e.message
24
+ end
25
+ end
26
+
27
+ protected
28
+
29
+ def client_id_valid?
30
+ check_client_id_validity
31
+ check_client_id_availablility
32
+ end
33
+
34
+ def check_client_id_validity
35
+ if (/^[a-z0-9_-]{4,20}$/ =~ client_id).nil?
36
+ raise "The client_id must match the regexp ^[a-z0-9_-]{4,20}$"
37
+ end
38
+ end
39
+
40
+ def check_client_id_availablility
41
+ client = create_client
42
+ if client['Type'] == 'ClientID_already_exist'
43
+ raise client['Message']
44
+ end
45
+ client
46
+ end
47
+
48
+ def create_client
49
+ MangoPay.configure do |c|
50
+ c.preproduction = options[:preproduction]
51
+ end
52
+ MangoPay::Client.create({
53
+ ClientID: client_id,
54
+ Name: client_name,
55
+ Email: client_email
56
+ })
57
+ end
58
+ end
59
+ end
60
+ end
@@ -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
@@ -1,115 +1,118 @@
1
- require 'net/http'
2
- require 'multi_json'
3
-
4
- # Version
5
- require 'mangopay/version'
6
-
7
- # JSON
8
- require 'mangopay/json'
9
-
10
- # Resources
11
- require 'mangopay/http_calls'
12
- require 'mangopay/resource'
13
- require 'mangopay/client'
14
- require 'mangopay/user'
15
- require 'mangopay/natural_user'
16
- require 'mangopay/legal_user'
17
- require 'mangopay/payin'
18
- require 'mangopay/payout'
19
- require 'mangopay/transfer'
20
- require 'mangopay/transaction'
21
- require 'mangopay/wallet'
22
- require 'mangopay/bank_account'
23
-
24
- # Errors
25
- require 'mangopay/errors'
26
-
27
- module MangoPay
28
-
29
- class Configuration
30
- attr_accessor :root_url, :client_id, :client_passphrase, :preproduction
31
-
32
- def preproduction
33
- @preproduction || false
34
- end
35
-
36
- def root_url
37
- @root_url || (@preproduction == true ? "https://mangopay-api-inte.leetchi.com" : "https://api.leetchi.com")
38
- end
39
- end
40
-
41
- class << self
42
- attr_accessor :configuration
43
- end
44
-
45
- def self.configure
46
- self.configuration ||= Configuration.new
47
- yield configuration
48
- end
49
-
50
- def self.api_uri(url='')
51
- URI(configuration.root_url + url)
52
- end
53
-
54
- def self.request(method, url, params={}, headers={})
55
- uri = api_uri(url)
56
-
57
- res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
58
- request = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, request_headers)
59
- request.body = MangoPay::JSON.dump(params)
60
- http.request request
61
- end
62
- MangoPay::JSON.load(res.body)
63
- end
64
-
65
- def self.user_agent
66
- @uname ||= get_uname
67
-
68
- {
69
- bindings_version: MangoPay::VERSION,
70
- lang: 'ruby',
71
- lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
72
- platform: RUBY_PLATFORM,
73
- uname: @uname
74
- }
75
- end
76
-
77
- def self.get_uname
78
- `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
79
- rescue Errno::ENOMEM
80
- 'uname lookup failed'
81
- end
82
-
83
- def self.get_oauth_token
84
- if @auth_timestamp.nil? || @auth_timestamp <= Time.now || @auth_token.nil?
85
- uri = api_uri('/api/oauth/token')
86
- res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
87
- req = Net::HTTP::Post.new(uri.request_uri)
88
- req.basic_auth configuration.client_id, configuration.client_passphrase
89
- req.body = 'grant_type=client_credentials'
90
- http.request req
91
- end
92
- @auth_token = MangoPay::JSON.load(res.body)
93
- @auth_timestamp = Time.now + @auth_token['expires_in'].to_i
94
- end
95
- @auth_token
96
- end
97
-
98
- def self.oauth_token
99
- oauth = get_oauth_token
100
- "#{oauth['token_type']} #{oauth['access_token']}"
101
- end
102
-
103
- def self.request_headers
104
- headers = {
105
- 'user_agent' => "MangoPay V1 RubyBindings/#{MangoPay::VERSION}",
106
- 'Authorization' => oauth_token,
107
- 'Content-Type' => 'application/json'
108
- }
109
- begin
110
- headers.update('x_mangopay_client_user_agent' => MangoPay::JSON.dump(user_agent))
111
- rescue => e
112
- headers.update('x_mangopay_client_raw_user_agent' => user_agent.inspect, error: "#{e} (#{e.class})")
113
- end
114
- end
115
- end
1
+ require 'net/http'
2
+ require 'cgi/util'
3
+ require 'multi_json'
4
+
5
+ # Version
6
+ require 'mangopay/version'
7
+
8
+ # JSON
9
+ require 'mangopay/json'
10
+
11
+ # Resources
12
+ require 'mangopay/http_calls'
13
+ require 'mangopay/resource'
14
+ require 'mangopay/client'
15
+ require 'mangopay/user'
16
+ require 'mangopay/natural_user'
17
+ require 'mangopay/legal_user'
18
+ require 'mangopay/payin'
19
+ require 'mangopay/payout'
20
+ require 'mangopay/transfer'
21
+ require 'mangopay/transaction'
22
+ require 'mangopay/wallet'
23
+ require 'mangopay/bank_account'
24
+ require 'mangopay/card_registration'
25
+ require 'mangopay/card'
26
+
27
+ # Errors
28
+ require 'mangopay/errors'
29
+
30
+ module MangoPay
31
+
32
+ class Configuration
33
+ attr_accessor :root_url, :client_id, :client_passphrase, :preproduction
34
+
35
+ def preproduction
36
+ @preproduction || false
37
+ end
38
+
39
+ def root_url
40
+ @root_url || (@preproduction == true ? "https://mangopay-api-inte.leetchi.com" : "https://api.leetchi.com")
41
+ end
42
+ end
43
+
44
+ class << self
45
+ attr_accessor :configuration
46
+ end
47
+
48
+ def self.configure
49
+ self.configuration ||= Configuration.new
50
+ yield configuration
51
+ end
52
+
53
+ def self.api_uri(url='')
54
+ URI(configuration.root_url + url)
55
+ end
56
+
57
+ def self.request(method, url, params={}, headers={})
58
+ uri = api_uri(url)
59
+
60
+ res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
61
+ request = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, request_headers)
62
+ request.body = MangoPay::JSON.dump(params)
63
+ http.request request
64
+ end
65
+ MangoPay::JSON.load(res.body)
66
+ end
67
+
68
+ def self.user_agent
69
+ @uname ||= get_uname
70
+
71
+ {
72
+ bindings_version: MangoPay::VERSION,
73
+ lang: 'ruby',
74
+ lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
75
+ platform: RUBY_PLATFORM,
76
+ uname: @uname
77
+ }
78
+ end
79
+
80
+ def self.get_uname
81
+ `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
82
+ rescue Errno::ENOMEM
83
+ 'uname lookup failed'
84
+ end
85
+
86
+ def self.get_oauth_token
87
+ if @auth_timestamp.nil? || @auth_timestamp <= Time.now || @auth_token.nil?
88
+ uri = api_uri('/api/oauth/token')
89
+ res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
90
+ req = Net::HTTP::Post.new(uri.request_uri)
91
+ req.basic_auth configuration.client_id, configuration.client_passphrase
92
+ req.body = 'grant_type=client_credentials'
93
+ http.request req
94
+ end
95
+ @auth_token = MangoPay::JSON.load(res.body)
96
+ @auth_timestamp = Time.now + @auth_token['expires_in'].to_i
97
+ end
98
+ @auth_token
99
+ end
100
+
101
+ def self.oauth_token
102
+ oauth = get_oauth_token
103
+ "#{oauth['token_type']} #{oauth['access_token']}"
104
+ end
105
+
106
+ def self.request_headers
107
+ headers = {
108
+ 'user_agent' => "MangoPay V1 RubyBindings/#{MangoPay::VERSION}",
109
+ 'Authorization' => oauth_token,
110
+ 'Content-Type' => 'application/json'
111
+ }
112
+ begin
113
+ headers.update('x_mangopay_client_user_agent' => MangoPay::JSON.dump(user_agent))
114
+ rescue => e
115
+ headers.update('x_mangopay_client_raw_user_agent' => user_agent.inspect, error: "#{e} (#{e.class})")
116
+ end
117
+ end
118
+ end