vindi-hermes 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cfeffedc8df5f4cfd1d2fc38730c15185bb9c454a30c8e9fbe3308168a7cda6f
4
- data.tar.gz: 707dad5195dc739dc23a46953198bd084be58cbb0a95faef053231f39c9f264e
3
+ metadata.gz: 0f54cd4844598eda33e4bb1aa7d8a7a9a6dd9b5997e1442510642dc656e4ed70
4
+ data.tar.gz: 4a043d2c49f7633650693bcc16b0b400677ed9890ea014e3f6bfbe187d74b6aa
5
5
  SHA512:
6
- metadata.gz: 985bbbda160a2c67ae61896cdff47d4ab4e05284e3661cf3498821aa84223c920fc9c4fac6d204b513a7260fc03ff62de877e6f664d71ddf025a5c6d4c5d55b4
7
- data.tar.gz: 6b8bf90607a8ba997ddb4273c7c24c07028fdafc46e1668be3750a89d8c2d6a7e0d318982f2c38bd3fc2861911ba4db6f83b7e673da7f78f851947a718db33b9
6
+ metadata.gz: f82e4c48114f55a6bb2cdeb319e400c0a0d3238df5455bf00e4fda5e60615afd4889a9d65b57aa0014f998d248190bd83e67e5aa420f669df12677910f3581d3
7
+ data.tar.gz: 400a9f6d5254697234b6c69350e3824b8ee58d1b3bd432de652aff9a4705feffdabcfc15a949672262743d79153ac50a12d3b55966529886321c6d4423f39f15
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ .env
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.7.3
data/Gemfile CHANGED
@@ -6,4 +6,15 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
- gem "rubocop"
9
+
10
+ group :development do
11
+ gem "httplog"
12
+ gem "pry-byebug"
13
+ gem "rubocop"
14
+ end
15
+
16
+ group :test do
17
+ gem "rspec"
18
+ gem "vcr"
19
+ gem "webmock"
20
+ end
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- vindi-hermes (0.0.1)
4
+ vindi-hermes (0.0.2)
5
5
  faraday (~> 1.4)
6
6
  faraday_middleware (~> 1.0)
7
7
  her (~> 1.1)
@@ -17,8 +17,14 @@ GEM
17
17
  minitest (>= 5.1)
18
18
  tzinfo (~> 2.0)
19
19
  zeitwerk (~> 2.3)
20
+ addressable (2.7.0)
21
+ public_suffix (>= 2.0.2, < 5.0)
20
22
  ast (2.4.2)
23
+ byebug (11.1.3)
24
+ coderay (1.1.3)
21
25
  concurrent-ruby (1.1.8)
26
+ crack (0.4.5)
27
+ rexml
22
28
  diff-lcs (1.4.4)
23
29
  faraday (1.4.2)
24
30
  faraday-em_http (~> 1.0)
@@ -35,6 +41,7 @@ GEM
35
41
  faraday-net_http_persistent (1.1.0)
36
42
  faraday_middleware (1.0.0)
37
43
  faraday (~> 1.0)
44
+ hashdiff (1.0.1)
38
45
  her (1.1.1)
39
46
  activemodel (>= 4.2.1)
40
47
  faraday (>= 0.8)
@@ -44,12 +51,20 @@ GEM
44
51
  rainbow (>= 2.0.0)
45
52
  i18n (1.8.10)
46
53
  concurrent-ruby (~> 1.0)
54
+ method_source (1.0.0)
47
55
  minitest (5.14.4)
48
56
  multi_json (1.15.0)
49
57
  multipart-post (2.1.1)
50
58
  parallel (1.20.1)
51
59
  parser (3.0.1.1)
52
60
  ast (~> 2.4.1)
61
+ pry (0.13.1)
62
+ coderay (~> 1.1)
63
+ method_source (~> 1.0)
64
+ pry-byebug (3.9.0)
65
+ byebug (~> 11.0)
66
+ pry (~> 0.13.0)
67
+ public_suffix (4.0.6)
53
68
  rack (2.2.3)
54
69
  rainbow (3.0.0)
55
70
  rake (13.0.3)
@@ -84,6 +99,11 @@ GEM
84
99
  tzinfo (2.0.4)
85
100
  concurrent-ruby (~> 1.0)
86
101
  unicode-display_width (2.0.0)
102
+ vcr (6.0.0)
103
+ webmock (3.12.2)
104
+ addressable (>= 2.3.6)
105
+ crack (>= 0.3.2)
106
+ hashdiff (>= 0.4.0, < 2.0.0)
87
107
  zeitwerk (2.4.2)
88
108
 
89
109
  PLATFORMS
@@ -91,10 +111,13 @@ PLATFORMS
91
111
 
92
112
  DEPENDENCIES
93
113
  httplog
114
+ pry-byebug
94
115
  rake (~> 13.0)
95
- rspec (~> 3.2)
116
+ rspec
96
117
  rubocop
118
+ vcr
97
119
  vindi-hermes!
120
+ webmock
98
121
 
99
122
  BUNDLED WITH
100
123
  2.2.17
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # **Vindi** **Her**mes - Let the god of trade help you
1
+ # Vindi Hermes
2
2
 
3
3
  ActiveRecord-like way to interact with Vindi API
4
4
 
@@ -13,18 +13,15 @@ ActiveRecord-like way to interact with Vindi API
13
13
  *
14
14
  [Vindi - Official Gem](https://github.com/vindi/vindi-ruby)
15
15
 
16
- > You may ask: Why create another if there is already one that's official?
17
- > *And I'll tell you, **little grasshopper**: Because I think this one could be more beautiful.*
18
- >
19
- > And you also may ask: Why the hell is the doc in english when all those interested are from the big banana republic?
20
- > *And my answer to you would be: You ask a lot of questions, huh?*
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.*
21
18
 
22
19
  ## Installation
23
20
 
24
21
  Add this line to your application's Gemfile:
25
22
 
26
23
  ```ruby
27
- gem 'vindi-hermes'
24
+ gem "vindi-hermes"
28
25
  ```
29
26
 
30
27
  And then execute:
@@ -43,9 +40,9 @@ Or install it yourself as:
43
40
  # config/initializers/vindi.rb
44
41
  Vindi.config do |c|
45
42
  c.sandbox = true # default is false
46
- c.api_key = 'YOUR API KEY'
47
- c.webhook_name = 'BASIC AUTH NAME'
48
- c.webhook_password = 'BASIC AUTH PASSWORD'
43
+ c.api_key = "YOUR API KEY"
44
+ c.webhook_name = "BASIC AUTH NAME"
45
+ c.webhook_password = "BASIC AUTH PASSWORD"
49
46
  end
50
47
  ```
51
48
 
@@ -163,9 +160,9 @@ On your project
163
160
  ```ruby
164
161
  # config/initializers/vindi.rb
165
162
  Vindi.config do |c|
166
- c.api_key = '123456'
167
- c.webhook_name = 'vindi'
168
- c.webhook_password = '123456'
163
+ c.api_key = "123456"
164
+ c.webhook_name = "vindi"
165
+ c.webhook_password = "123456"
169
166
  end
170
167
 
171
168
  # routes.rb
data/bin/console CHANGED
@@ -8,8 +8,8 @@ require "vindi"
8
8
  # with your gem easier. You can also use a different console, if you like.
9
9
 
10
10
  # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
11
+ require "pry"
12
+ Pry.start
13
13
 
14
- require "irb"
15
- IRB.start(__FILE__)
14
+ # require "irb"
15
+ # IRB.start(__FILE__)
data/lib/vindi.rb CHANGED
@@ -6,11 +6,14 @@ require "her"
6
6
 
7
7
  require_relative "vindi/version"
8
8
  require_relative "vindi/rate_limit"
9
+
9
10
  require_relative "vindi/middleware/rate_limit_validation"
10
11
  require_relative "vindi/middleware/response_parser"
12
+
11
13
  require_relative "vindi/core_extensions/her_with_query_filter"
14
+ require_relative "vindi/core_extensions/her_save_only_changed_attrs"
12
15
 
13
- module Vindi
16
+ module Vindi # :nodoc:
14
17
  class Error < StandardError; end
15
18
 
16
19
  RESOURCE_MODELS = Dir[File.expand_path("vindi/models/**/*.rb", File.dirname(__FILE__))].freeze
@@ -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
@@ -19,25 +19,27 @@ module CoreExtensions
19
19
  return self if params.blank? && !@_fetch.nil?
20
20
 
21
21
  clone.tap do |r|
22
- default_params = extract_params params, r.params
23
- query = params_to_query params, parse_query(r.params.fetch(:query, ""))
22
+ r.params = r.params.merge(params)
23
+
24
+ # Default params, as order and page number, will always be used.
25
+ default_params = extract_default_params r.params
26
+
27
+ # Query filters is joined into a single param called :query.
28
+ query = [r.params.delete(:query), params_to_query(r.params)].compact.join " "
29
+
30
+ r.params = { query: query }.merge(default_params)
24
31
 
25
- r.params = r.params.merge default_params.merge(query: query)
26
32
  r.clear_fetch_cache!
27
33
  end
28
34
  end
29
35
 
30
36
  private
31
37
 
32
- def parse_query(query)
33
- CGI.parse(query).transform_values(&:first)
34
- end
35
-
36
- def extract_params(new_ones, old_ones)
37
- page = new_ones.delete(:page) || old_ones.delete(:page) || 1
38
- per_page = new_ones.delete(:per_page) || old_ones.delete(:per_page) || 25
39
- sort_by = new_ones.delete(:sort_by) || old_ones.delete(:sort_by) || :created_at
40
- sort_order = new_ones.delete(:sort_order) || old_ones.delete(:sort_order) || :desc
38
+ def extract_default_params(params)
39
+ page = params.delete(:page) || 1
40
+ per_page = params.delete(:per_page) || 25
41
+ sort_by = params.delete(:sort_by) || :created_at
42
+ sort_order = params.delete(:sort_order) || :desc
41
43
 
42
44
  {
43
45
  page: page,
@@ -47,8 +49,8 @@ module CoreExtensions
47
49
  }.delete_if { |_, v| v.nil? }
48
50
  end
49
51
 
50
- def params_to_query(params, query)
51
- params.merge(query).map do |key, value|
52
+ def params_to_query(params)
53
+ params.map do |key, value|
52
54
  case key
53
55
  when :contains then value.map { |k, v| "#{k}:#{v}" }.last
54
56
  when :gt then value.map { |k, v| "#{k}>#{v}" }.last
@@ -31,40 +31,14 @@ module Vindi
31
31
 
32
32
  before_validation
33
33
 
34
- # Validate record before save and after the request
35
- # put the errors in the right places.
34
+ # Archive a record.
36
35
  #
37
- # @example A subscription without a customer
36
+ # @example Archive a customer
38
37
  #
39
- # @subscription = Vindi::Subscription.new.tap do |s|
40
- # s.plan_id = plan.id
41
- # s.payment_method_code = "credit_card"
42
- # s.save
43
- # end
38
+ # Vindi::Customer.find_by(email: "sarumanthewhite@middlearth.io").archive!
44
39
  #
45
- # @subscription.errors.full_messages # ["Customer can't be blank"]
46
- #
47
- # @example A subscription with invalid plan
48
- #
49
- # @subscription = Vindi::Subscription.new.tap do |s|
50
- # s.customer_id = customer.id
51
- # s.plan_id = 1
52
- # s.payment_method_code = "credit_card"
53
- # s.save
54
- # end
55
- #
56
- # @subscription.errors.full_messages # ["Plan nao encontrado"]
57
- #
58
- def save
59
- super
60
-
61
- response_errors.any? && errors.clear && response_errors.each do |re|
62
- errors.add re[:attribute], re[:type], message: re[:message]
63
- end
64
-
65
- return false if errors.any?
66
-
67
- self
40
+ def archive!
41
+ destroy
68
42
  end
69
43
 
70
44
  # @private
data/lib/vindi/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vindi
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
data/vindi-hermes.gemspec CHANGED
@@ -26,7 +26,4 @@ Gem::Specification.new do |spec|
26
26
  spec.add_dependency "faraday", "~> 1.4"
27
27
  spec.add_dependency "faraday_middleware", "~> 1.0"
28
28
  spec.add_dependency "her", "~> 1.1"
29
-
30
- spec.add_development_dependency "httplog"
31
- spec.add_development_dependency "rspec", "~> 3.2"
32
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vindi-hermes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wedson Lima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-01 00:00:00.000000000 Z
11
+ date: 2021-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -52,34 +52,6 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.1'
55
- - !ruby/object:Gem::Dependency
56
- name: httplog
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: rspec
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '3.2'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '3.2'
83
55
  description: ActiveRecord-like way to interact with Vindi API
84
56
  email:
85
57
  - wedson.sousa.lima@gmail.com
@@ -89,6 +61,7 @@ extra_rdoc_files: []
89
61
  files:
90
62
  - ".gitignore"
91
63
  - ".rubocop.yml"
64
+ - ".tool-versions"
92
65
  - CHANGELOG.md
93
66
  - CODE_OF_CONDUCT.md
94
67
  - Gemfile
@@ -99,6 +72,7 @@ files:
99
72
  - bin/console
100
73
  - bin/setup
101
74
  - lib/vindi.rb
75
+ - lib/vindi/core_extensions/her_save_only_changed_attrs.rb
102
76
  - lib/vindi/core_extensions/her_with_query_filter.rb
103
77
  - lib/vindi/middleware/rate_limit_validation.rb
104
78
  - lib/vindi/middleware/response_parser.rb
@@ -143,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
117
  - !ruby/object:Gem::Version
144
118
  version: '0'
145
119
  requirements: []
146
- rubygems_version: 3.0.3
120
+ rubygems_version: 3.1.6
147
121
  signing_key:
148
122
  specification_version: 4
149
123
  summary: Vindi API - Let the god of trade help you