effective_resources 1.8.18 → 1.8.23

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 180638d525bdd6d499cddd67f09595509685ebbfdc59e69ea16d89fdef6ff1fa
4
- data.tar.gz: e9687c798dceef560a070a4631876ca17b401cf2e7d993c7df98dbe5af5febc4
3
+ metadata.gz: 125098fcfbdac7c18b9a7df8a451d3f4f7d0ac1b7d8120658b07e6c33d5b0f2b
4
+ data.tar.gz: 67f2018e9ed650ac1dfcc4f3143005367259a135964ce7e0bfe99c1b4b04d78f
5
5
  SHA512:
6
- metadata.gz: 41b62506a91d29ebe9049ce5de063e18412ba60cf4d63636e2cbe245d2b972a8a767d4645d2f42f5b4923b73eb867b4ea0ed508bda1ab55747d68f46dc1db4af
7
- data.tar.gz: de31440669699862980273c6b1ce68b786057d200b524ec1ad0a54a09d73b8fbae9bfb372918b263fe8bc23254329ca15feb305adab7a683d86833738d0c5f6c
6
+ metadata.gz: 6bc4633cb2e6c6c85f99d09f1c0a559547e74ebcfe7e9da21383d9b663325374efa467e2809041edc1f24ff4c37cea660a9416e3586a9c128df9b6a5a2375231
7
+ data.tar.gz: b3e4cc3b69b53eb69affbe7ab0eda2f7741746eb9ab43ac84fd7d907b1a365d74511bbe446037d22bee8ee6ea143b529f3889f3c3c87a6c7f9d125bfe9109d9e
@@ -125,8 +125,10 @@ module Effective
125
125
  end
126
126
 
127
127
  def resource_layout
128
- namespace = controller_path.include?('admin/') ? 'admin' : 'application'
129
- defined?(Tenant) ? "#{Tenant.current}/#{namespace}" : namespace
128
+ if defined?(Tenant)
129
+ namespace = controller_path.include?('admin/') ? 'admin' : 'application'
130
+ "#{Tenant.current}/#{namespace}"
131
+ end
130
132
  end
131
133
 
132
134
  def resource_params_method_name
@@ -41,7 +41,7 @@ module Effective
41
41
 
42
42
  messages = resource.errors.map do |error|
43
43
  attribute = error.respond_to?(:attribute) ? error.attribute : error
44
- message = error.respond_to?(:message) ? error.message : resource.errors[attribute].to_sentence
44
+ message = error.respond_to?(:attribute) ? error.message : resource.errors[attribute].to_sentence
45
45
 
46
46
  if message[0] == message[0].upcase # If the error begins with a capital letter
47
47
  message
@@ -65,6 +65,8 @@ module EffectiveDeviseUser
65
65
  end
66
66
 
67
67
  module ClassMethods
68
+ def effective_devise_user?; true; end
69
+
68
70
  def permitted_sign_up_params # Should contain all fields as per views/users/_sign_up_fields
69
71
  raise('please define a self.permitted_sign_up_params')
70
72
  [:email, :password, :password_confirmation, :first_name, :last_name, :name, :login]
@@ -0,0 +1,111 @@
1
+ module Effective
2
+ class Http
3
+
4
+ def self.get(endpoint, params: nil, headers: nil)
5
+ headers = { 'Content-Type': 'application/json' }.merge(headers || {})
6
+ query = ('?' + params.compact.map { |k, v| "$#{k}=#{v}" }.join('&')) if params.present?
7
+
8
+ uri = URI.parse(endpoint + query.to_s)
9
+
10
+ http = Net::HTTP.new(uri.host, uri.port)
11
+ http.read_timeout = 10
12
+ http.use_ssl = true if endpoint.start_with?('https')
13
+
14
+ response = with_retries do
15
+ puts "[GET] #{uri}" if Rails.env.development?
16
+ http.get(uri, headers)
17
+ end
18
+
19
+ unless ['200', '204'].include?(response.code.to_s)
20
+ puts("Response code: #{response.code} #{response.body}")
21
+ return false
22
+ end
23
+
24
+ JSON.parse(response.body)
25
+ end
26
+
27
+ def self.post(endpoint, params:, headers: nil)
28
+ headers = { 'Content-Type': 'application/json' }.merge(headers || {})
29
+
30
+ uri = URI.parse(endpoint)
31
+
32
+ http = Net::HTTP.new(uri.host, uri.port)
33
+ http.read_timeout = 10
34
+ http.use_ssl = true if endpoint.start_with?('https')
35
+
36
+ response = with_retries do
37
+ puts "[POST] #{uri} #{params}" if Rails.env.development?
38
+ http.post(uri.path, params.to_json, headers)
39
+ end
40
+
41
+ unless ['200', '204'].include?(response.code.to_s)
42
+ puts("Response code: #{response.code} #{response.body}")
43
+ return false
44
+ end
45
+
46
+ JSON.parse(response.body)
47
+ end
48
+
49
+ def self.patch(endpoint, params:, headers: nil)
50
+ headers = { 'Content-Type': 'application/json' }.merge(headers || {})
51
+
52
+ uri = URI.parse(endpoint)
53
+
54
+ http = Net::HTTP.new(uri.host, uri.port)
55
+ http.read_timeout = 10
56
+ http.use_ssl = true if endpoint.start_with?('https')
57
+
58
+ response = with_retries do
59
+ puts "[PATCH] #{uri} #{params}" if Rails.env.development?
60
+ http.post(uri.path, params.to_json, headers)
61
+ end
62
+
63
+ unless ['200', '204'].include?(response.code.to_s)
64
+ puts("Response code: #{response.code} #{response.body}")
65
+ return false
66
+ end
67
+
68
+ JSON.parse(response.body)
69
+ end
70
+
71
+ def self.delete(endpoint, params: nil, headers: nil)
72
+ headers = { 'Content-Type': 'application/json' }.merge(headers || {})
73
+ query = ('?' + params.compact.map { |k, v| "$#{k}=#{v}" }.join('&')) if params.present?
74
+
75
+ uri = URI.parse(endpoint + query.to_s)
76
+
77
+ http = Net::HTTP.new(uri.host, uri.port)
78
+ http.read_timeout = 10
79
+ http.use_ssl = true if endpoint.start_with?('https')
80
+
81
+ response = with_retries do
82
+ puts "[DELETE] #{uri}" if Rails.env.development?
83
+ http.delete(uri, headers)
84
+ end
85
+
86
+ unless ['200', '204'].include?(response.code.to_s)
87
+ puts("Response code: #{response.code} #{response.body}")
88
+ return false
89
+ end
90
+
91
+ JSON.parse(response.body)
92
+ end
93
+
94
+ private
95
+
96
+ def self.with_retries(retries: 3, wait: 2, &block)
97
+ raise('expected a block') unless block_given?
98
+
99
+ begin
100
+ return yield
101
+ rescue Exception => e
102
+ if (retries -= 1) > 0
103
+ sleep(wait); retry
104
+ else
105
+ raise
106
+ end
107
+ end
108
+ end
109
+
110
+ end
111
+ end
@@ -11,6 +11,11 @@ module Effective
11
11
  @attributes = {}
12
12
  end
13
13
 
14
+ # For EffectiveLogging. This is a protected keyword I think.
15
+ def message(*args)
16
+ method_missing(:message, args)
17
+ end
18
+
14
19
  def read(&block)
15
20
  instance_exec(&block)
16
21
  end
@@ -46,4 +46,22 @@ module EffectiveResources
46
46
  (config.respond_to?(:active_job) && config.active_job.queue_adapter) ? :deliver_later : :deliver_now
47
47
  end
48
48
 
49
+ def self.advance_date(date, business_days: 1, holidays: [:ca, :observed])
50
+ raise('business_days must be an integer <= 365') unless business_days.kind_of?(Integer) && business_days <= 365
51
+
52
+ business_days.times do
53
+ loop do
54
+ date = date + 1.day
55
+ break if business_day?(date, holidays: holidays)
56
+ end
57
+ end
58
+
59
+ date
60
+ end
61
+
62
+ def self.business_day?(date, holidays: [:ca, :observed])
63
+ require 'holidays' unless defined?(Holidays)
64
+ date.wday != 0 && date.wday != 6 && Holidays.on(date, *holidays).blank?
65
+ end
66
+
49
67
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.8.18'.freeze
2
+ VERSION = '1.8.23'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.18
4
+ version: 1.8.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-28 00:00:00.000000000 Z
11
+ date: 2021-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: holidays
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: Make any controller an effective resource controller.
112
126
  email:
113
127
  - info@codeandeffect.com
@@ -150,6 +164,7 @@ files:
150
164
  - app/models/effective/action_failed.rb
151
165
  - app/models/effective/attribute.rb
152
166
  - app/models/effective/code_reader.rb
167
+ - app/models/effective/http.rb
153
168
  - app/models/effective/model_reader.rb
154
169
  - app/models/effective/resource.rb
155
170
  - app/models/effective/resource_exec.rb