rvindi 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/gem-push.yml +45 -0
  3. data/.gitignore +9 -0
  4. data/.rubocop.yml +13 -0
  5. data/.tool-versions +1 -0
  6. data/CHANGELOG.md +5 -0
  7. data/CODE_OF_CONDUCT.md +84 -0
  8. data/Gemfile +21 -0
  9. data/Gemfile.lock +134 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +205 -0
  12. data/Rakefile +8 -0
  13. data/bin/console +22 -0
  14. data/bin/setup +8 -0
  15. data/lib/rvindi.rb +3 -0
  16. data/lib/vindi/core_extensions/her_save_only_changed_attrs.rb +73 -0
  17. data/lib/vindi/core_extensions/her_with_query_filter.rb +69 -0
  18. data/lib/vindi/middleware/rate_limit_validation.rb +23 -0
  19. data/lib/vindi/middleware/response_parser.rb +50 -0
  20. data/lib/vindi/models/address.rb +9 -0
  21. data/lib/vindi/models/bill.rb +26 -0
  22. data/lib/vindi/models/bill_item.rb +18 -0
  23. data/lib/vindi/models/charge.rb +9 -0
  24. data/lib/vindi/models/customer.rb +29 -0
  25. data/lib/vindi/models/discount.rb +8 -0
  26. data/lib/vindi/models/issue.rb +8 -0
  27. data/lib/vindi/models/model.rb +77 -0
  28. data/lib/vindi/models/notification.rb +8 -0
  29. data/lib/vindi/models/payment_method.rb +8 -0
  30. data/lib/vindi/models/payment_profile.rb +9 -0
  31. data/lib/vindi/models/period.rb +8 -0
  32. data/lib/vindi/models/plan.rb +90 -0
  33. data/lib/vindi/models/plan_item.rb +11 -0
  34. data/lib/vindi/models/pricing_schema.rb +8 -0
  35. data/lib/vindi/models/product.rb +25 -0
  36. data/lib/vindi/models/product_item.rb +8 -0
  37. data/lib/vindi/models/subscription.rb +48 -0
  38. data/lib/vindi/models/transaction.rb +8 -0
  39. data/lib/vindi/rate_limit.rb +18 -0
  40. data/lib/vindi/version.rb +5 -0
  41. data/lib/vindi.rb +80 -0
  42. data/rvindi.gemspec +30 -0
  43. metadata +141 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1fa6f7bfdb7151faa01caa723d91c7f253b621773642126a5ed2e0cd4811d88e
4
+ data.tar.gz: a6bc8f9ab55f327b50ef770868370df368b0a079767aa5b8bc3d9abcf9db9bd7
5
+ SHA512:
6
+ metadata.gz: 8a18ebfb628e49f640892452b406c613e14a043b0b1bb05c2851ed516f8310dfa398b44a1afe8836f369dc8582b2b61fd6aab10b3cf63fb97ee53d28d7788357
7
+ data.tar.gz: 4c83c624d62a8d0e33d8c10c5ba66071d81927ae2e627220247122ddcef5c0f6d14d3871ddb5b99f7f8cf9b206f0bdaa20b053a070fa6b4496afc3c54640fe76
@@ -0,0 +1,45 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Build + Publish
12
+ runs-on: ubuntu-latest
13
+ permissions:
14
+ contents: read
15
+ packages: write
16
+
17
+ steps:
18
+ - uses: actions/checkout@v2
19
+ - name: Set up Ruby 2.5
20
+ uses: actions/setup-ruby@v1
21
+ with:
22
+ ruby-version: 2.5.x
23
+
24
+ - name: Publish to GPR
25
+ run: |
26
+ mkdir -p $HOME/.gem
27
+ touch $HOME/.gem/credentials
28
+ chmod 0600 $HOME/.gem/credentials
29
+ printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
30
+ gem build *.gemspec
31
+ gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
32
+ env:
33
+ GEM_HOST_API_KEY: "Bearer ${{secrets.GITHUB_TOKEN}}"
34
+ OWNER: ${{ github.repository_owner }}
35
+
36
+ - name: Publish to RubyGems
37
+ run: |
38
+ mkdir -p $HOME/.gem
39
+ touch $HOME/.gem/credentials
40
+ chmod 0600 $HOME/.gem/credentials
41
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
42
+ gem build *.gemspec
43
+ gem push *.gem
44
+ env:
45
+ GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .env
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.7.4
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-05-25
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at wedson.sousa.lima@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in vindi.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ group :development do
11
+ gem "dotenv"
12
+ gem "httplog"
13
+ gem "pry-byebug"
14
+ gem "rubocop"
15
+ end
16
+
17
+ group :test do
18
+ gem "rspec"
19
+ gem "vcr"
20
+ gem "webmock"
21
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,134 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rvindi (0.0.3)
5
+ dry-configurable (~> 0.7.0)
6
+ faraday (~> 1.4)
7
+ faraday_middleware (~> 1.0)
8
+ her (~> 1.1)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ activemodel (6.1.4.1)
14
+ activesupport (= 6.1.4.1)
15
+ activesupport (6.1.4.1)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 1.6, < 2)
18
+ minitest (>= 5.1)
19
+ tzinfo (~> 2.0)
20
+ zeitwerk (~> 2.3)
21
+ addressable (2.7.0)
22
+ public_suffix (>= 2.0.2, < 5.0)
23
+ ast (2.4.2)
24
+ byebug (11.1.3)
25
+ coderay (1.1.3)
26
+ concurrent-ruby (1.1.9)
27
+ crack (0.4.5)
28
+ rexml
29
+ diff-lcs (1.4.4)
30
+ dotenv (2.7.6)
31
+ dry-configurable (0.7.0)
32
+ concurrent-ruby (~> 1.0)
33
+ faraday (1.8.0)
34
+ faraday-em_http (~> 1.0)
35
+ faraday-em_synchrony (~> 1.0)
36
+ faraday-excon (~> 1.1)
37
+ faraday-httpclient (~> 1.0.1)
38
+ faraday-net_http (~> 1.0)
39
+ faraday-net_http_persistent (~> 1.1)
40
+ faraday-patron (~> 1.0)
41
+ faraday-rack (~> 1.0)
42
+ multipart-post (>= 1.2, < 3)
43
+ ruby2_keywords (>= 0.0.4)
44
+ faraday-em_http (1.0.0)
45
+ faraday-em_synchrony (1.0.0)
46
+ faraday-excon (1.1.0)
47
+ faraday-httpclient (1.0.1)
48
+ faraday-net_http (1.0.1)
49
+ faraday-net_http_persistent (1.2.0)
50
+ faraday-patron (1.0.0)
51
+ faraday-rack (1.0.0)
52
+ faraday_middleware (1.2.0)
53
+ faraday (~> 1.0)
54
+ hashdiff (1.0.1)
55
+ her (1.1.1)
56
+ activemodel (>= 4.2.1)
57
+ faraday (>= 0.8)
58
+ multi_json (~> 1.7)
59
+ httplog (1.5.0)
60
+ rack (>= 1.0)
61
+ rainbow (>= 2.0.0)
62
+ i18n (1.8.11)
63
+ concurrent-ruby (~> 1.0)
64
+ method_source (1.0.0)
65
+ minitest (5.14.4)
66
+ multi_json (1.15.0)
67
+ multipart-post (2.1.1)
68
+ parallel (1.20.1)
69
+ parser (3.0.1.1)
70
+ ast (~> 2.4.1)
71
+ pry (0.13.1)
72
+ coderay (~> 1.1)
73
+ method_source (~> 1.0)
74
+ pry-byebug (3.9.0)
75
+ byebug (~> 11.0)
76
+ pry (~> 0.13.0)
77
+ public_suffix (4.0.6)
78
+ rack (2.2.3)
79
+ rainbow (3.0.0)
80
+ rake (13.0.3)
81
+ regexp_parser (2.1.1)
82
+ rexml (3.2.5)
83
+ rspec (3.10.0)
84
+ rspec-core (~> 3.10.0)
85
+ rspec-expectations (~> 3.10.0)
86
+ rspec-mocks (~> 3.10.0)
87
+ rspec-core (3.10.1)
88
+ rspec-support (~> 3.10.0)
89
+ rspec-expectations (3.10.1)
90
+ diff-lcs (>= 1.2.0, < 2.0)
91
+ rspec-support (~> 3.10.0)
92
+ rspec-mocks (3.10.2)
93
+ diff-lcs (>= 1.2.0, < 2.0)
94
+ rspec-support (~> 3.10.0)
95
+ rspec-support (3.10.2)
96
+ rubocop (1.15.0)
97
+ parallel (~> 1.10)
98
+ parser (>= 3.0.0.0)
99
+ rainbow (>= 2.2.2, < 4.0)
100
+ regexp_parser (>= 1.8, < 3.0)
101
+ rexml
102
+ rubocop-ast (>= 1.5.0, < 2.0)
103
+ ruby-progressbar (~> 1.7)
104
+ unicode-display_width (>= 1.4.0, < 3.0)
105
+ rubocop-ast (1.6.0)
106
+ parser (>= 3.0.1.1)
107
+ ruby-progressbar (1.11.0)
108
+ ruby2_keywords (0.0.5)
109
+ tzinfo (2.0.4)
110
+ concurrent-ruby (~> 1.0)
111
+ unicode-display_width (2.0.0)
112
+ vcr (6.0.0)
113
+ webmock (3.12.2)
114
+ addressable (>= 2.3.6)
115
+ crack (>= 0.3.2)
116
+ hashdiff (>= 0.4.0, < 2.0.0)
117
+ zeitwerk (2.5.1)
118
+
119
+ PLATFORMS
120
+ x86_64-linux
121
+
122
+ DEPENDENCIES
123
+ dotenv
124
+ httplog
125
+ pry-byebug
126
+ rake (~> 13.0)
127
+ rspec
128
+ rubocop
129
+ rvindi!
130
+ vcr
131
+ webmock
132
+
133
+ BUNDLED WITH
134
+ 2.2.32
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Wedson Lima
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,205 @@
1
+ # Vindi Ruby
2
+
3
+ ActiveRecord-like way to interact with Vindi API
4
+
5
+ ## Vindi
6
+
7
+ [Vindi](https://vindi.com.br) is a brazilian fintech, that was recently acquired by [Locaweb](https://blog.vindi.com.br/agora-a-vindi-faz-parte-do-grupo-locaweb), that helps other companies to work with **recurring payments**.
8
+
9
+ ### API docs
10
+
11
+ * [Swagger sandbox](https://vindi.github.io/api-docs/dist/?url=https://sandbox-app.vindi.com.br/api/v1/docs#/)
12
+ * [Swagger prod](https://vindi.github.io/api-docs/dist)
13
+ *
14
+ [Vindi - Official Gem](https://github.com/vindi/vindi-ruby)
15
+
16
+ > You may ask: Why create another gem if there is already one that's official?
17
+ > *And I'll tell you, **little grasshopper**: Because MIT licenses are better than GPLv3.*
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem "rvindi"
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle install
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install rvindi
34
+
35
+ ## Usage
36
+
37
+ ### Config
38
+
39
+ ```ruby
40
+ # config/initializers/vindi.rb
41
+ Vindi.config do |c|
42
+ c.sandbox = true # default is false
43
+ c.api_key = "YOUR API KEY"
44
+ c.webhook_secret_name = "BASIC AUTH NAME"
45
+ c.webhook_secret_password = "BASIC AUTH PASSWORD"
46
+ end
47
+ ```
48
+
49
+ ### Examples
50
+
51
+ #### Saruman creates a subscription business to rent his Palantir
52
+
53
+ ```ruby
54
+ # Let's say that Gandalf wants to borrow Saruman's Palantir
55
+ # promising he'll give it back before the end of the 3rd era.
56
+ #
57
+ # But Saruman is wise and he thinks he could make a good money
58
+ # charging a rent for the use of the magic ball.
59
+ #
60
+ # So, Saruman went on to Vindi and creates a product...
61
+ #
62
+ palantir = Vindi::Product.new.tap do |p|
63
+ p.code = "palantir"
64
+ p.name = "Palantir"
65
+ p.description = "The Twitch of Istari folk"
66
+ p.pricing_schema = { price: 42.42 }
67
+ p.save
68
+ end
69
+
70
+ # ...then creates a recurring plan.
71
+ one_plan = Vindi::Plan.new.tap do |p|
72
+ p.code = "the-one-plan"
73
+ p.name = "Monthly Plan"
74
+ p.description = "The One Plan To Rule Them All"
75
+ p.period = "monthly"
76
+ p.recurring = true
77
+ p.plan_items = [
78
+ {
79
+ cycles: nil, # untill the end of time
80
+ product_id: palantir.id
81
+ }
82
+ ]
83
+ p.save
84
+ end
85
+
86
+ # Gandalf uses his fellow Bilbo's address to receive the invoices...
87
+ gandalf = Vindi::Customer.new.tap do |c|
88
+ c.code = 1
89
+ c.name = "Gandalf the Grey"
90
+ c.email = "mithrandir@middlearth.io"
91
+ c.address = {
92
+ street: "Bagshot Row",
93
+ number: "Bag End",
94
+ neighborhood: "Hobbiton",
95
+ city: "Shire"
96
+ }
97
+ c.save
98
+ end
99
+
100
+ # ...uses Elrond's credit card to create a payment profile...
101
+ pp = Vindi::PaymentProfile.new.tap do |pp|
102
+ pp.holder_name = "Elrond Half-elven"
103
+ pp.card_expiration = "12/3021"
104
+ pp.card_number = "5167454851671773"
105
+ pp.card_cvv = "123"
106
+ pp.customer_id = gandalf.id
107
+ pp.save
108
+ end
109
+
110
+ # ...subscribes to `the one plan` and then get the palantir.
111
+ subscription = Vindi::Subscription.new.tap do |s|
112
+ s.plan_id = one_plan.id
113
+ s.customer_id = gandalf.id
114
+ s.payment_method_code = "credit_card"
115
+ s.save
116
+ end
117
+ ```
118
+
119
+ #### After some time he wants to know how the business is going
120
+
121
+ ```ruby
122
+ # Active customers
123
+ customers = Vindi::Customer.active
124
+
125
+ # Active subscriptions
126
+ subscriptions = Vindi::Subscriptions.active
127
+
128
+ # Subscriptions for the-one-plan
129
+ subscriptions = Vindi::Plan.find_by(code: "the-one-plan").subscriptions
130
+
131
+ # Filter Gandalf
132
+ gandalf = Vindi::Customer.find_by(email: "mithrandir@middlearth.io")
133
+
134
+ # All Gandalf's subscriptions
135
+ subscriptions = gandalf.subscriptions.active
136
+
137
+ # Cancel Gandalf's subscription
138
+ gandalf.subscriptions.active.last.cancel!
139
+
140
+ # Refund the last Gandalf's payment
141
+ gandalf.charges.last.refund!
142
+ ```
143
+
144
+ ## Webhooks
145
+
146
+ You must validate incoming data from Vindi.
147
+ * [Vindi Blog Post](https://atendimento.vindi.com.br/hc/pt-br/articles/203305800)
148
+
149
+ You can validate webhook calls using baisc auth.
150
+ Just configure a webhook call with something like this `https://NAME:PASSWORD@www.startupmassa.com/vindi/webhook`.
151
+
152
+ On Vindi admin dashboard
153
+
154
+ ```bash
155
+ https://vindi:123456@www.startupmassa.com/vindi/webhook
156
+ ```
157
+
158
+ On your project
159
+
160
+ ```ruby
161
+ # config/initializers/vindi.rb
162
+ Vindi.configure do |config|
163
+ config.api_key = "123456"
164
+ config.webhook_secret_name = "vindi"
165
+ config.webhook_secret_password = "123456"
166
+ end
167
+
168
+ # routes.rb
169
+ namespace :vindi do
170
+ post :webhook, to: "webhook#listener"
171
+ end
172
+
173
+ # vindi/webhook_controller.rb
174
+ class Vindi::Webhook < ActionController::Base
175
+ http_basic_authenticate_with name: Vindi.webhook_secret_name, password: Vindi.webhook_secret_password
176
+
177
+ # POST https://usuario:senha@www.startupmassa.com/vindi/webhook
178
+ def listener
179
+ case event_params[:type]
180
+ when "charge_rejected" # defaulting user?
181
+ when "bill_paid" # do the magic
182
+ else
183
+ head :ok
184
+ end
185
+ end
186
+
187
+ private
188
+
189
+ def event_params
190
+ params.require(:event).permit!
191
+ end
192
+ end
193
+ ```
194
+
195
+ ## Contributing
196
+
197
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wedsonlima/vindi-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/wedsonlima/vindi-ruby/blob/main/CODE_OF_CONDUCT.md).
198
+
199
+ ## License
200
+
201
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
202
+
203
+ ## Code of Conduct
204
+
205
+ Everyone interacting in the Vindi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/wedsonlima/vindi-ruby/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
data/bin/console ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "dotenv/load"
6
+ require "vindi"
7
+ require "httplog"
8
+
9
+ # You can add fixtures and/or initialization code here to make experimenting
10
+ # with your gem easier. You can also use a different console, if you like.
11
+
12
+ Vindi.configure do |config|
13
+ config.sandbox = ENV["VINDI_SANDBOX"]
14
+ config.api_key = ENV["VINDI_API_KEY"]
15
+ end
16
+
17
+ HttpLog.configure do |config|
18
+ config.log_headers = true
19
+ end
20
+
21
+ require "pry"
22
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/rvindi.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "vindi"
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CoreExtensions
4
+ # WARNING: monkey patch (https://github.com/remi/her/blob/master/lib/her/model/relation.rb#L34)
5
+ module HerSaveOnlyChangedAttrs
6
+ # Validate record before save and after the request
7
+ # put the errors in the right places.
8
+ #
9
+ # @example A subscription without a customer
10
+ #
11
+ # @subscription = Vindi::Subscription.new.tap do |s|
12
+ # s.plan_id = plan.id
13
+ # s.payment_method_code = "credit_card"
14
+ # s.save
15
+ # end
16
+ #
17
+ # @subscription.errors.full_messages # ["Customer can't be blank"]
18
+ #
19
+ # @example A subscription with invalid plan
20
+ #
21
+ # @subscription = Vindi::Subscription.new.tap do |s|
22
+ # s.customer_id = customer.id
23
+ # s.plan_id = 1
24
+ # s.payment_method_code = "credit_card"
25
+ # s.save
26
+ # end
27
+ #
28
+ # @subscription.errors.full_messages # ["Plan nao encontrado"]
29
+ #
30
+ def save
31
+ if new?
32
+ super
33
+ else
34
+ save_current_changes
35
+ end
36
+
37
+ response_errors.any? && errors.clear && response_errors.each do |re|
38
+ errors.add re[:attribute], re[:type], message: re[:message]
39
+ end
40
+
41
+ return false if errors.any?
42
+
43
+ self
44
+ end
45
+
46
+ private
47
+
48
+ def save_current_changes
49
+ return self unless changed.any?
50
+
51
+ callback = new? ? :create : :update
52
+ method = self.class.method_for(callback)
53
+
54
+ run_callbacks :save do
55
+ run_callbacks callback do
56
+ self.class.request(filtered_changed_attributes.merge(_method: method, _path: request_path)) do |parsed_data, response|
57
+ load_from_parsed_data(parsed_data)
58
+ return false if !response.success? || @response_errors.any?
59
+ changes_applied
60
+ end
61
+ end
62
+ end
63
+
64
+ self
65
+ end
66
+
67
+ def filtered_changed_attributes
68
+ changes.transform_values(&:last)
69
+ end
70
+ end
71
+ end
72
+
73
+ Her::Model::ORM.prepend CoreExtensions::HerSaveOnlyChangedAttrs