myfinance 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +23 -0
  4. data/.hound.yml +1063 -0
  5. data/.rspec +2 -0
  6. data/.travis.yml +7 -0
  7. data/Gemfile +4 -0
  8. data/Gemfile.lock +94 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +209 -0
  11. data/Rakefile +6 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +7 -0
  14. data/lib/myfinance.rb +35 -0
  15. data/lib/myfinance/client.rb +31 -0
  16. data/lib/myfinance/configuration.rb +12 -0
  17. data/lib/myfinance/entities/base.rb +9 -0
  18. data/lib/myfinance/entities/collection.rb +57 -0
  19. data/lib/myfinance/entities/entity.rb +16 -0
  20. data/lib/myfinance/entities/entity_collection.rb +14 -0
  21. data/lib/myfinance/entities/financial_account.rb +30 -0
  22. data/lib/myfinance/entities/payable_account.rb +6 -0
  23. data/lib/myfinance/entities/receivable_account.rb +6 -0
  24. data/lib/myfinance/exception.rb +11 -0
  25. data/lib/myfinance/http.rb +33 -0
  26. data/lib/myfinance/request.rb +53 -0
  27. data/lib/myfinance/resources/base.rb +28 -0
  28. data/lib/myfinance/resources/entity.rb +39 -0
  29. data/lib/myfinance/resources/financial_account.rb +70 -0
  30. data/lib/myfinance/resources/payable_account.rb +33 -0
  31. data/lib/myfinance/resources/receivable_account.rb +33 -0
  32. data/lib/myfinance/response.rb +41 -0
  33. data/lib/myfinance/version.rb +3 -0
  34. data/myfinance.gemspec +45 -0
  35. data/spec/lib/myfinance/client_spec.rb +49 -0
  36. data/spec/lib/myfinance/configuration_spec.rb +26 -0
  37. data/spec/lib/myfinance/entities/base_spec.rb +28 -0
  38. data/spec/lib/myfinance/entities/collection_spec.rb +70 -0
  39. data/spec/lib/myfinance/entities/entity_collection_spec.rb +20 -0
  40. data/spec/lib/myfinance/entities/entity_spec.rb +11 -0
  41. data/spec/lib/myfinance/entities/payable_account_spec.rb +13 -0
  42. data/spec/lib/myfinance/entities/receivable_account_spec.rb +13 -0
  43. data/spec/lib/myfinance/http_spec.rb +37 -0
  44. data/spec/lib/myfinance/request_spec.rb +29 -0
  45. data/spec/lib/myfinance/resources/entity_spec.rb +63 -0
  46. data/spec/lib/myfinance/resources/payable_account_spec.rb +146 -0
  47. data/spec/lib/myfinance/resources/receivable_account_spec.rb +146 -0
  48. data/spec/lib/myfinance/response_spec.rb +58 -0
  49. data/spec/myfinance_spec.rb +25 -0
  50. data/spec/spec_helper.rb +39 -0
  51. data/spec/support/client.rb +3 -0
  52. data/spec/support/matchers/have_attr_accessor.rb +18 -0
  53. data/spec/support/shared_examples/entity_attributes.rb +9 -0
  54. data/spec/support/shared_examples/http_request_methods.rb +29 -0
  55. data/spec/support/vcr.rb +7 -0
  56. metadata +306 -0
@@ -0,0 +1,146 @@
1
+ require "spec_helper"
2
+
3
+ describe Myfinance::Resources::PayableAccount do
4
+ let(:entity_id) { 3798 }
5
+
6
+ describe "#create", vcr: true do
7
+ let(:params) { { due_date: '2015-08-15', amount: 150.99 } }
8
+ subject { client.payable_accounts.create(entity_id, params) }
9
+
10
+ context "with success" do
11
+ it "creates a new object" do
12
+ expect(subject.id).to eq(1215631)
13
+ expect(subject.due_date).to eq(Date.new(2015, 8, 15))
14
+ expect(subject.entity_id).to eq(entity_id)
15
+ expect(subject.status).to eq(1)
16
+ expect(subject.status_name).to eq('unpaid')
17
+ expect(subject.occurred_at).to be_nil
18
+ expect(subject.amount).to eq(150.99)
19
+ expect(subject.ticket_amount).to be_nil
20
+ expect(subject.interest_amount).to be_nil
21
+ expect(subject.discount_amount).to be_nil
22
+ expect(subject.total_amount).to be_nil
23
+ expect(subject.description).to be_nil
24
+ expect(subject.document).to be_nil
25
+ expect(subject.document_emission_date).to be_nil
26
+ expect(subject.observation).to be_nil
27
+ expect(subject.remind).to be_falsy
28
+ expect(subject.reminded_at).to be_nil
29
+ expect(subject.income_tax_relevant).to be_falsy
30
+ expect(subject.category_id).to be_nil
31
+ expect(subject.classification_center_id).to be_nil
32
+ expect(subject.expected_deposit_account_id).to be_nil
33
+ expect(subject.recurrence_id).to be_nil
34
+ expect(subject.person_id).to be_nil
35
+ expect(subject.created_at).to eq(DateTime.parse("2015-08-05T10:31:28-03:00"))
36
+ expect(subject.updated_at).to eq(DateTime.parse("2015-08-05T10:31:28-03:00"))
37
+ expect(subject.recurrent).to be_falsy
38
+ expect(subject.parcelled).to be_falsy
39
+ expect(subject.recurrence_period).to be_nil
40
+ expect(subject.number_of_parcels).to be_nil
41
+ expect(subject.current_parcel).to be_nil
42
+ expect(subject.competency_month).to eq("2015-08")
43
+ expect(subject.financial_account_taxes_attributes).to be_empty
44
+ end
45
+
46
+ context "when the payable_account has occurred_at value defined" do
47
+ let(:params) { { due_date: '2015-08-01', amount: 150.99, occurred_at: '2015-08-05', total_amount: 150.99 } }
48
+
49
+ it "creates a new payable_account as paid" do
50
+ expect(subject.id).to eq(1215632)
51
+ expect(subject.due_date).to eq(Date.new(2015, 8, 1))
52
+ expect(subject.occurred_at).to eq(Date.new(2015, 8, 5))
53
+ expect(subject.amount).to eq(150.99)
54
+ expect(subject.total_amount).to eq(150.99)
55
+ expect(subject.status).to eq(2)
56
+ expect(subject.status_name).to eq('paid')
57
+ end
58
+ end
59
+ end
60
+
61
+ context "when any data is invalid" do
62
+ let(:params) { { due_date: '', amount: 150.99 } }
63
+
64
+ it "raises Myfinance::RequestError" do
65
+ expect { subject }.to raise_error(Myfinance::RequestError)
66
+ end
67
+
68
+ it "adds information on request error object" do
69
+ expect(Myfinance::RequestError).to receive(:new).with(code: 422, message: "", body: "{\"competency_month\":[\"n\\u00e3o pode ser vazio\"],\"due_date\":[\"n\\u00e3o \\u00e9 uma data v\\u00e1lida\"]}").and_call_original
70
+ expect { subject }.to raise_error(Myfinance::RequestError)
71
+ end
72
+ end
73
+
74
+ context "when entity does not exist" do
75
+ subject { client.payable_accounts.create(555, params) }
76
+
77
+ it "raises Myfinance::RequestError" do
78
+ expect { subject }.to raise_error(Myfinance::RequestError)
79
+ end
80
+
81
+ it "adds information on request error object" do
82
+ expect(Myfinance::RequestError).to receive(:new).with(code: 403, message: "Forbidden", body: "{\"error\":\"Voc\\u00ea n\\u00e3o tem permiss\\u00e3o para acessar este recurso.\"}").and_call_original
83
+ expect { subject }.to raise_error(Myfinance::RequestError)
84
+ end
85
+ end
86
+ end
87
+
88
+ describe "#pay", vcr: true do
89
+ let(:params) { { total_amount: 150.99, occurred_at: '2015-08-05', amount: 150.99 } }
90
+ subject { client.payable_accounts.pay(1215631, entity_id, params) }
91
+
92
+ context "with success" do
93
+ it "pays payable account" do
94
+ expect(subject.id).to eq(1215631)
95
+ expect(subject.status).to eq(2)
96
+ expect(subject.status_name).to eq('paid')
97
+ expect(subject.amount).to eq(150.99)
98
+ expect(subject.total_amount).to eq(150.99)
99
+ expect(subject.ticket_amount).to be_nil
100
+ expect(subject.interest_amount).to be_nil
101
+ expect(subject.discount_amount).to be_nil
102
+ end
103
+ end
104
+
105
+ context "when any parameter is invalid" do
106
+ let(:params) { { total_amount: nil, occurred_at: '2015-08-05', amount: 150.99 } }
107
+
108
+ it "raises request error" do
109
+ expect { subject }.to raise_error(Myfinance::RequestError)
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "#undo_payment", vcr: true do
115
+ subject { client.payable_accounts.undo_payment(1215631, entity_id) }
116
+
117
+ it "undoes receivement of the receivable account" do
118
+ expect(subject.id).to eq(1215631)
119
+ expect(subject.status).to eq(1)
120
+ expect(subject.status_name).to eq('unpaid')
121
+ expect(subject.amount).to eq(150.99)
122
+ expect(subject.total_amount).to be_nil
123
+ expect(subject.ticket_amount).to be_nil
124
+ expect(subject.interest_amount).to be_nil
125
+ expect(subject.discount_amount).to be_nil
126
+ end
127
+ end
128
+
129
+ describe "#destroy", vcr: true do
130
+ subject { client.payable_accounts.destroy(1215631, entity_id) }
131
+
132
+ context "when payable account exists" do
133
+ it "returns true" do
134
+ expect(subject).to be_truthy
135
+ end
136
+ end
137
+
138
+ context "when payable account does not exist" do
139
+ subject { client.payable_accounts.destroy(1215631099, entity_id) }
140
+
141
+ it "raises request error" do
142
+ expect { subject }.to raise_error(Myfinance::RequestError)
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,146 @@
1
+ require "spec_helper"
2
+
3
+ describe Myfinance::Resources::ReceivableAccount do
4
+ let(:entity_id) { 3798 }
5
+
6
+ describe "#create", vcr: true do
7
+ let(:params) { { due_date: '2015-08-15', amount: 150.99 } }
8
+ subject { client.receivable_accounts.create(entity_id, params) }
9
+
10
+ context "with success" do
11
+ it "creates a new object" do
12
+ expect(subject.id).to eq(1215634)
13
+ expect(subject.due_date).to eq(Date.new(2015, 8, 15))
14
+ expect(subject.entity_id).to eq(entity_id)
15
+ expect(subject.status).to eq(1)
16
+ expect(subject.status_name).to eq('unreceived')
17
+ expect(subject.occurred_at).to be_nil
18
+ expect(subject.amount).to eq(150.99)
19
+ expect(subject.ticket_amount).to be_nil
20
+ expect(subject.interest_amount).to be_nil
21
+ expect(subject.discount_amount).to be_nil
22
+ expect(subject.total_amount).to be_nil
23
+ expect(subject.description).to be_nil
24
+ expect(subject.document).to be_nil
25
+ expect(subject.document_emission_date).to be_nil
26
+ expect(subject.observation).to be_nil
27
+ expect(subject.remind).to be_falsy
28
+ expect(subject.reminded_at).to be_nil
29
+ expect(subject.income_tax_relevant).to be_falsy
30
+ expect(subject.category_id).to be_nil
31
+ expect(subject.classification_center_id).to be_nil
32
+ expect(subject.expected_deposit_account_id).to be_nil
33
+ expect(subject.recurrence_id).to be_nil
34
+ expect(subject.person_id).to be_nil
35
+ expect(subject.created_at).to eq(DateTime.parse("2015-08-05T11:59:05-03:00"))
36
+ expect(subject.updated_at).to eq(DateTime.parse("2015-08-05T11:59:05-03:00"))
37
+ expect(subject.recurrent).to be_falsy
38
+ expect(subject.parcelled).to be_falsy
39
+ expect(subject.recurrence_period).to be_nil
40
+ expect(subject.number_of_parcels).to be_nil
41
+ expect(subject.current_parcel).to be_nil
42
+ expect(subject.competency_month).to eq("2015-08")
43
+ expect(subject.financial_account_taxes_attributes).to be_empty
44
+ end
45
+
46
+ context "when the receivable_account has occurred_at value defined" do
47
+ let(:params) { { due_date: '2015-08-01', amount: 150.99, occurred_at: '2015-08-05', total_amount: 150.99 } }
48
+
49
+ it "creates a new receivable_account as paid" do
50
+ expect(subject.id).to eq(1215635)
51
+ expect(subject.due_date).to eq(Date.new(2015, 8, 1))
52
+ expect(subject.occurred_at).to eq(Date.new(2015, 8, 5))
53
+ expect(subject.amount).to eq(150.99)
54
+ expect(subject.total_amount).to eq(150.99)
55
+ expect(subject.status).to eq(2)
56
+ expect(subject.status_name).to eq('received')
57
+ end
58
+ end
59
+ end
60
+
61
+ context "when any data is invalid" do
62
+ let(:params) { { due_date: '', amount: 150.99 } }
63
+
64
+ it "raises Myfinance::RequestError" do
65
+ expect { subject }.to raise_error(Myfinance::RequestError)
66
+ end
67
+
68
+ it "adds information on request error object" do
69
+ expect(Myfinance::RequestError).to receive(:new).with(code: 422, message: "", body: "{\"competency_month\":[\"n\\u00e3o pode ser vazio\"],\"due_date\":[\"n\\u00e3o \\u00e9 uma data v\\u00e1lida\"]}").and_call_original
70
+ expect { subject }.to raise_error(Myfinance::RequestError)
71
+ end
72
+ end
73
+
74
+ context "when entity does not exist" do
75
+ subject { client.receivable_accounts.create(555, params) }
76
+
77
+ it "raises Myfinance::RequestError" do
78
+ expect { subject }.to raise_error(Myfinance::RequestError)
79
+ end
80
+
81
+ it "adds information on request error object" do
82
+ expect(Myfinance::RequestError).to receive(:new).with(code: 403, message: "Forbidden", body: "{\"error\":\"Voc\\u00ea n\\u00e3o tem permiss\\u00e3o para acessar este recurso.\"}").and_call_original
83
+ expect { subject }.to raise_error(Myfinance::RequestError)
84
+ end
85
+ end
86
+ end
87
+
88
+ describe "#receive", vcr: true do
89
+ let(:params) { { total_amount: 150.99, occurred_at: '2015-08-05', amount: 150.99 } }
90
+ subject { client.receivable_accounts.receive(1215634, entity_id, params) }
91
+
92
+ context "with success" do
93
+ it "receives the receivable account" do
94
+ expect(subject.id).to eq(1215634)
95
+ expect(subject.status).to eq(2)
96
+ expect(subject.status_name).to eq('received')
97
+ expect(subject.amount).to eq(150.99)
98
+ expect(subject.total_amount).to eq(150.99)
99
+ expect(subject.ticket_amount).to be_nil
100
+ expect(subject.interest_amount).to be_nil
101
+ expect(subject.discount_amount).to be_nil
102
+ end
103
+ end
104
+
105
+ context "when any parameter is invalid" do
106
+ let(:params) { { total_amount: nil, occurred_at: '2015-08-05', amount: 150.99 } }
107
+
108
+ it "raises request error" do
109
+ expect { subject }.to raise_error(Myfinance::RequestError)
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "#undo_receivement", vcr: true do
115
+ subject { client.receivable_accounts.undo_receivement(1215634, entity_id) }
116
+
117
+ it "undoes receivement of the receivable account" do
118
+ expect(subject.id).to eq(1215634)
119
+ expect(subject.status).to eq(1)
120
+ expect(subject.status_name).to eq('unreceived')
121
+ expect(subject.amount).to eq(150.99)
122
+ expect(subject.total_amount).to be_nil
123
+ expect(subject.ticket_amount).to be_nil
124
+ expect(subject.interest_amount).to be_nil
125
+ expect(subject.discount_amount).to be_nil
126
+ end
127
+ end
128
+
129
+ describe "#destroy", vcr: true do
130
+ subject { client.receivable_accounts.destroy(1215634, entity_id) }
131
+
132
+ context "when receivable account exists" do
133
+ it "returns true" do
134
+ expect(subject).to be_truthy
135
+ end
136
+ end
137
+
138
+ context "when receivable account does not exist" do
139
+ subject { client.receivable_accounts.destroy(1215631099, entity_id) }
140
+
141
+ it "raises request error" do
142
+ expect { subject }.to raise_error(Myfinance::RequestError)
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ describe Myfinance::Response do
4
+ subject { described_class.new(response) }
5
+
6
+ describe "#resolve!" do
7
+ context 'when success' do
8
+ let(:response) { double(success?: true) }
9
+
10
+ it 'returns self' do
11
+ expect(subject.resolve!).to eq(response)
12
+ end
13
+
14
+ context 'when block given' do
15
+ let(:response) { double(success?: true) }
16
+
17
+ it 'returns block' do
18
+ expect(subject.resolve!{ response }).to eq(response)
19
+ end
20
+ end
21
+ end
22
+
23
+ context 'when timeout' do
24
+ let(:response) { double(success?: false, timed_out?: true) }
25
+
26
+ it 'raises RequestTimeout' do
27
+ expect { subject.resolve! }.to raise_error(Myfinance::RequestTimeout)
28
+ end
29
+ end
30
+
31
+ context 'when not success neither timeout' do
32
+ let(:response) { double(success?: false, timed_out?: false, code: 301, status_message: 'Moved Permanently', body: '') }
33
+
34
+ it 'raises RequestError' do
35
+ expect { subject.resolve! }.to raise_error(Myfinance::RequestError)
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "#parsed_body" do
41
+ let(:response) { double(success?: true) }
42
+
43
+ context "when JSON is invalid" do
44
+ it "returns empty hash" do
45
+ allow(subject).to receive(:body).and_return("{\"key\": ")
46
+ expect(subject.parsed_body).to eq({})
47
+ end
48
+ end
49
+
50
+ context "when JSON is valid" do
51
+ it "returns empty hash" do
52
+ allow(subject).to receive(:body).and_return("{\"key\": \"value\"}")
53
+ expect(MultiJson).to receive(:load).with("{\"key\": \"value\"}").and_call_original
54
+ expect(subject.parsed_body).to eq({ "key" => "value" })
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Myfinance do
4
+ it 'has a version number' do
5
+ expect(Myfinance::VERSION).to_not be_nil
6
+ end
7
+
8
+ describe 'configuration' do
9
+ it 'should be done via block initialization' do
10
+ Myfinance.configure do |c|
11
+ c.user_agent = "My App v1.0"
12
+ c.url = "https://sandbox.myfinance.com.br"
13
+ end
14
+
15
+ expect(Myfinance.configuration.user_agent).to eq("My App v1.0")
16
+ expect(Myfinance.configuration.url).to eq("https://sandbox.myfinance.com.br")
17
+ end
18
+
19
+ it 'uses a singleton object for the configuration values' do
20
+ config1 = Myfinance.configuration
21
+ config2 = Myfinance.configuration
22
+ expect(config1).to eq(config2)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,39 @@
1
+ require "codeclimate-test-reporter"
2
+ require "simplecov"
3
+
4
+ CodeClimate::TestReporter.start
5
+
6
+ SimpleCov.start do
7
+ SimpleCov.maximum_coverage_drop 0.2
8
+
9
+ SimpleCov.start do
10
+ add_group "Resources", "lib/myfinance/resources"
11
+ add_group "Entities", "lib/myfinance/entities"
12
+ add_filter "vendor"
13
+ end
14
+ end
15
+
16
+
17
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
18
+ require "myfinance"
19
+ require "pry"
20
+ require "vcr"
21
+
22
+
23
+ Dir["spec/support/**/*.rb"].each { |f| load f }
24
+
25
+ VCR.configure do |config|
26
+ config.cassette_library_dir = "spec/vcr_cassettes"
27
+ config.hook_into :typhoeus
28
+ config.ignore_hosts "codeclimate.com"
29
+ end
30
+
31
+ RSpec.configure do |config|
32
+ config.mock_with :rspec
33
+
34
+ config.before(:each) do
35
+ Myfinance.configuration.url = "https://sandbox.myfinance.com.br"
36
+ Typhoeus::Expectation.clear
37
+ end
38
+ end
39
+
@@ -0,0 +1,3 @@
1
+ def client
2
+ Myfinance.client('b552dd5a8598ca089b91c5e355a83b86e4696aefac9eaa05')
3
+ end
@@ -0,0 +1,18 @@
1
+ RSpec::Matchers.define :have_attr_accessor do |field|
2
+ match do |object_instance|
3
+ object_instance.respond_to?(field) &&
4
+ object_instance.respond_to?("#{field}=")
5
+ end
6
+
7
+ failure_message do |object_instance|
8
+ "expected attr_accessor for #{field} on #{object_instance}"
9
+ end
10
+
11
+ failure_message_when_negated do |object_instance|
12
+ "expected attr_accessor for #{field} not to be defined on #{object_instance}"
13
+ end
14
+
15
+ description do
16
+ "checks to see if there is an attr accessor on the supplied object"
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ shared_examples "entity_attributes" do |attrs|
2
+ attrs.each do |attr|
3
+ it { is_expected.to have_attr_accessor(attr) }
4
+ end
5
+
6
+ it "expect to cover all attributes" do
7
+ expect(subject.attributes.keys).to match_array(attrs)
8
+ end
9
+ end