besepa 0.2

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 (40) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +46 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +132 -0
  6. data/Rakefile +1 -0
  7. data/besepa-ruby.gemspec +31 -0
  8. data/lib/besepa.rb +14 -0
  9. data/lib/besepa/activity.rb +11 -0
  10. data/lib/besepa/api_calls/create.rb +26 -0
  11. data/lib/besepa/api_calls/destroy.rb +15 -0
  12. data/lib/besepa/api_calls/list.rb +36 -0
  13. data/lib/besepa/api_calls/update.rb +23 -0
  14. data/lib/besepa/bank_account.rb +63 -0
  15. data/lib/besepa/business_account.rb +37 -0
  16. data/lib/besepa/customer.rb +134 -0
  17. data/lib/besepa/debit.rb +64 -0
  18. data/lib/besepa/errors/besepa_error.rb +28 -0
  19. data/lib/besepa/errors/invalid_resource_error.rb +11 -0
  20. data/lib/besepa/errors/resource_not_found_error.rb +11 -0
  21. data/lib/besepa/group.rb +27 -0
  22. data/lib/besepa/mandate.rb +18 -0
  23. data/lib/besepa/product.rb +19 -0
  24. data/lib/besepa/remittance.rb +32 -0
  25. data/lib/besepa/resource.rb +96 -0
  26. data/lib/besepa/subscription.rb +50 -0
  27. data/lib/besepa/utils/config.rb +51 -0
  28. data/lib/besepa/utils/connection.rb +27 -0
  29. data/lib/besepa/utils/request.rb +60 -0
  30. data/lib/besepa/utils/version.rb +6 -0
  31. data/lib/besepa/webhook.rb +16 -0
  32. data/spec/besepa/customer_spec.rb +119 -0
  33. data/spec/fixtures/customer.json +17 -0
  34. data/spec/fixtures/customer_add_debit.json +54 -0
  35. data/spec/fixtures/customer_bank_accounts.json +22 -0
  36. data/spec/fixtures/customer_debits.json +71 -0
  37. data/spec/fixtures/customer_removed.json +17 -0
  38. data/spec/fixtures/customers.json +20 -0
  39. data/spec/helper.rb +46 -0
  40. metadata +174 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e725ca7cd7ea944a8c13d88162899de00290ba13
4
+ data.tar.gz: 7902ef96eef5551d51a360dac0954222511e4e29
5
+ SHA512:
6
+ metadata.gz: becde3ce5f9e41fa12b887b8abe3f5b8ae52fe7b51be1069a98f0f9b6ecabbba2f37c88788aee846fc160442f3f433b7d961cebfa1e6d025af538f42dbac20ac
7
+ data.tar.gz: ff2577c252329c1071e03589d1e065a3c70a8b26220a6e06a29e63cc30d17bf8279c151bdfadac62d82fd5ded4a6c85075b4908626f7518adc219556bd13d178
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in besepa-ruby.gemspec
4
+ gemspec
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ besepa (0.2)
5
+ faraday (~> 0.9)
6
+ faraday_middleware (~> 0.9)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.3.6)
12
+ crack (0.4.2)
13
+ safe_yaml (~> 1.0.0)
14
+ diff-lcs (1.2.5)
15
+ faraday (0.9.1)
16
+ multipart-post (>= 1.2, < 3)
17
+ faraday_middleware (0.9.1)
18
+ faraday (>= 0.7.4, < 0.10)
19
+ multipart-post (2.0.0)
20
+ rake (10.3.2)
21
+ rspec (3.1.0)
22
+ rspec-core (~> 3.1.0)
23
+ rspec-expectations (~> 3.1.0)
24
+ rspec-mocks (~> 3.1.0)
25
+ rspec-core (3.1.7)
26
+ rspec-support (~> 3.1.0)
27
+ rspec-expectations (3.1.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.1.0)
30
+ rspec-mocks (3.1.3)
31
+ rspec-support (~> 3.1.0)
32
+ rspec-support (3.1.2)
33
+ safe_yaml (1.0.4)
34
+ webmock (1.20.4)
35
+ addressable (>= 2.3.6)
36
+ crack (>= 0.3.2)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ besepa!
43
+ bundler (~> 1.3)
44
+ rake
45
+ rspec (~> 3.1)
46
+ webmock (~> 1.20)
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alberto Molpeceres
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,132 @@
1
+ # Besepa::Ruby
2
+
3
+ A Ruby gem to Besepa.com's API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'besepa-ruby'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install besepa-ruby
18
+
19
+ ## Documentation
20
+
21
+ http://apidocs.besepa.com
22
+
23
+
24
+ ## Configuration
25
+
26
+ ```ruby
27
+ Besepa.configure do |config|
28
+ config.api_key = API_KEY
29
+ config.endpoint = API_END_POINT
30
+ end
31
+ ```
32
+
33
+ By default, this gem points to Besepa's sandbox (https://sandbox.besepa.com). If you want to point to Besepa's production environment, use https://api.besepa.com as endpoint.
34
+
35
+ Remeber that API_KEY changes from one environment to the other.
36
+
37
+ ## Usage
38
+
39
+ **Get all customers**
40
+
41
+ ```ruby
42
+ Besepa::Customer.all
43
+ ```
44
+
45
+ **Get one customer's information**
46
+
47
+ ```ruby
48
+ Besepa::Customer.find( customer_id )
49
+ ```
50
+
51
+ **Update customer**
52
+
53
+ ```ruby
54
+ c = Besepa::Customer.find( customer_id )
55
+ c.name = 'New name'
56
+ c.save
57
+ ```
58
+
59
+ **Remove customer**
60
+
61
+ ```ruby
62
+ c = Besepa::Customer.find( customer_id )
63
+ c.destroy #customer's state is change to 'REMOVED'
64
+ ```
65
+
66
+ Note all subscriptions, upcoming debits and mandates for this customer will be cancelled
67
+
68
+
69
+ **Get customer's bank accounts**
70
+
71
+ ```ruby
72
+ Besepa::Customer.find( customer_id ).bank_accounts
73
+ ```
74
+
75
+ In case you don't have a Customer yet, just create one Customer object with it's id.
76
+ ```ruby
77
+ Besepa::Customer.new( id: customer_id ).bank_accounts
78
+ ```
79
+
80
+ **Get customer's subscriptions**
81
+
82
+ ```ruby
83
+ Besepa::Customer.new( id: customer_id ).subscriptions
84
+ ```
85
+
86
+ **Get customer's debits**
87
+
88
+ ```ruby
89
+ Besepa::Customer.new( id: customer_id ).debits
90
+ ```
91
+
92
+ **Add a bank account to a customer**
93
+
94
+ ```ruby
95
+ Besepa::Customer.new( id: customer_id ).add_bank_account(iban, bic, bank_name)
96
+
97
+ ```
98
+ bank_name is optional.
99
+
100
+ **Create a debit for a customer**
101
+
102
+ ```ruby
103
+ Besepa::Customer.new( id: customer_id ).add_debit(bank_account_id, reference, description, amount, collect_at, creditor_account_id, metadata)
104
+ ```
105
+
106
+ Amount is with two decimals, without separation character (1000 == 10.00)
107
+ Metadata can be a hash that we will store associated to this debit. creditor_account_id is optional, if none passed, we will use account's default one.
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+ ## Supported Ruby Versions
118
+
119
+ Right now, this gem has been tested with Ruby 2 only. More coming.
120
+
121
+ ## Contributing
122
+
123
+ 1. Fork it
124
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
125
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
126
+ 4. Push to the branch (`git push origin my-new-feature`)
127
+ 5. Create new Pull Request
128
+
129
+
130
+ ## Copyright
131
+
132
+ Copyright (c) 2015 Besepa Technologies S.L.. See LICENSE for details.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'besepa/utils/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+
8
+ spec.name = "besepa"
9
+ spec.version = Besepa::Utils::VERSION
10
+ spec.authors = ["besepa.com"]
11
+ spec.email = ["alberto@besepa.com"]
12
+ spec.description = %q{Ruby client for besepa.com}
13
+ spec.summary = %q{Ruby client for besepa.com}
14
+ spec.homepage = "http://www.besepa.com"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency('faraday', '~> 0.9')
23
+ spec.add_dependency('faraday_middleware', '~> 0.9')
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+
27
+ spec.add_development_dependency 'rspec', '~> 3.1'
28
+ spec.add_development_dependency 'webmock', '~> 1.20'
29
+ spec.add_development_dependency "rake"
30
+
31
+ end
@@ -0,0 +1,14 @@
1
+ libdir = File.dirname(__FILE__)
2
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
3
+ Dir.glob(File.join(libdir, 'besepa', 'utils','*.rb')).each {|f| require f }
4
+ Dir.glob(File.join(libdir, 'besepa', 'api_calls','*.rb')).each {|f| require f }
5
+ require File.join(libdir, 'besepa', 'errors', 'besepa_error.rb')
6
+ Dir.glob(File.join(libdir, 'besepa', 'errors','*.rb')).each {|f| require f }
7
+ require File.join(libdir, 'besepa', 'resource.rb')
8
+ Dir.glob(File.join(libdir, 'besepa', '*.rb')).each {|f| require f }
9
+
10
+ module Besepa
11
+
12
+ extend Utils::ApiConfig
13
+
14
+ end
@@ -0,0 +1,11 @@
1
+ module Besepa
2
+
3
+ class Activity < Besepa::Resource
4
+
5
+ FIELDS = [:id, :key, :created_at, :owner]
6
+ FIELDS.each do |f|
7
+ attr_accessor f
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,26 @@
1
+ module Besepa
2
+
3
+ module ApiCalls
4
+
5
+ module Create
6
+
7
+ module ClassMethods
8
+
9
+ def create(params, filters={})
10
+ payload = {}
11
+ payload[klass_name] = params
12
+ response = post "/#{api_path(filters)}", payload
13
+ self.new(response['response'])
14
+ end
15
+
16
+ end
17
+
18
+ def self.included(base)
19
+ base.extend(ClassMethods)
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,15 @@
1
+ module Besepa
2
+
3
+ module ApiCalls
4
+
5
+ module Destroy
6
+
7
+ def destroy(filters={})
8
+ response = delete "/#{self.class.api_path(filters)}/#{id}"
9
+ process_attributes(response['response']) if response['response']
10
+ self
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ module Besepa
2
+
3
+ module ApiCalls
4
+
5
+ module List
6
+
7
+ module ClassMethods
8
+
9
+ def all(filters={})
10
+ response = get "/#{api_path(filters)}"
11
+ objects = Array.new
12
+ if response['count'] > 0
13
+ response['response'].each do |c|
14
+ objects << self.new(c)
15
+ end
16
+ end
17
+ objects
18
+ end
19
+
20
+ def find(id, filters={})
21
+ response = get "/#{api_path(filters)}/#{id}"
22
+ c = self.new(response['response'])
23
+ c
24
+ end
25
+
26
+ end
27
+
28
+ def self.included(base)
29
+ base.extend(ClassMethods)
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,23 @@
1
+ module Besepa
2
+
3
+ module ApiCalls
4
+
5
+ module Update
6
+
7
+ def save(filters={})
8
+ h = self.to_hash
9
+ #id and status should not be send back to the server
10
+ h.delete(:status)
11
+ h.delete(:id)
12
+ #remove all nil values. Not updated.
13
+ h.delete_if {|key, value| value.nil? }
14
+ payload = {}
15
+ payload[self.class.klass_name] = h
16
+ response = put "/#{self.class.api_path(filters)}/#{id}", payload
17
+ process_attributes(response['response'])
18
+ self
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,63 @@
1
+ module Besepa
2
+
3
+ class BankAccount < Besepa::Resource
4
+
5
+ include Besepa::ApiCalls::List
6
+ include Besepa::ApiCalls::Create
7
+ include Besepa::ApiCalls::Update
8
+ include Besepa::ApiCalls::Destroy
9
+
10
+ FIELDS = [:id, :iban, :bic, :bank_name, :status, :customer_id]
11
+
12
+ FIELDS.each do |f|
13
+ attr_accessor f
14
+ end
15
+
16
+ attr_accessor :mandate
17
+
18
+ def self.klass_name
19
+ "bank_account"
20
+ end
21
+
22
+ def set_as_default
23
+ response = put "/#{api_path({customer_id: customer_id})}/set_as_default"
24
+ process_attributes(response['response'])
25
+ self
26
+ end
27
+
28
+ def generate_signature_request
29
+ response = put "/#{api_path({customer_id: customer_id})}/generate_signature_request"
30
+ process_attributes(response['response'])
31
+ self
32
+ end
33
+
34
+ def to_hash
35
+ values = {}
36
+ self.class::FIELDS.each do |key|
37
+ values[key] = self.send("#{key.to_s}")
38
+ end
39
+ values[:mandate] = mandate.to_hash if mandate
40
+ values
41
+ end
42
+
43
+ protected
44
+
45
+ def self.api_path(filters={})
46
+ "#{Customer.api_path}/#{CGI.escape(filters[:customer_id])}/bank_accounts"
47
+ end
48
+
49
+ def api_path(filters={})
50
+ "#{Customer.api_path}/#{CGI.escape(filters[:customer_id]||customer_id)}/bank_accounts/#{CGI.escape(id)}"
51
+ end
52
+
53
+ def process_attributes(attrs)
54
+ self.class::FIELDS.each do |key|
55
+ self.send("#{key.to_s}=", attrs[key.to_s] || attrs[key.to_sym])
56
+ end
57
+ self.mandate = Besepa::Mandate.new(attrs['mandate']) if attrs['mandate']
58
+ process_activities(attrs)
59
+ self
60
+ end
61
+
62
+ end
63
+ end