wcc_ministries_client 1.0.2 → 1.0.3

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: b96140c4e35013a90b6a4905f48568e2489f85488b659ebb6ab31c3b2b330637
4
- data.tar.gz: '05569aac8a34144e765b5aa3717e28463f9f045591415e010acca83f5b24c907'
3
+ metadata.gz: 83660a89294ddeb66b82b63d6bd683419b6420b3b9163f37d0607e975038dba5
4
+ data.tar.gz: 8c46cc56b6226a6ed1ae12edd4a4f8642dc9e8a8747139d6c41accd056b7f0c7
5
5
  SHA512:
6
- metadata.gz: 86d2b2841adae8e81d80912809510471cd73de14532c5a7bd461a81263b0e69bba6c6e573e718eea4ef779cb60b9f70ae6ab985096997c062d07979e20e39ca3
7
- data.tar.gz: 656932c3195877d7f06a979c634e29250ad17531d3a0957c2b76f554b241f716741b4580203cb6b076c5445270a7e3b664a744c1f07820ac0dcad96449cdf874
6
+ metadata.gz: bab4d0ae60d989639ec66590c847d23128c33e8115b680b4321dc16bb103fcb33f1c9548bcd5e692bd7db072cfd7d417e56b4a5597c1d928d3e914fec21ea4db
7
+ data.tar.gz: 153d8e2c926c5a6517b497406bc1f5cba1f260e6afbb188514a44e05d24a443da08b3d0b41e0daeb60c8d453a8f2b7a47c3452288609c3ea3f0671941a370f27
data/Gemfile.lock CHANGED
@@ -1,8 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- wcc_ministries_client (1.0.2)
5
- typhoeus (~> 1.0, >= 1.0.1)
4
+ wcc_ministries_client (1.0.3)
5
+ faraday (>= 0.9, < 2.0)
6
+ faraday-http-cache (>= 1.0, < 3.0)
7
+ faraday_middleware (>= 0.13, < 2.0)
6
8
 
7
9
  GEM
8
10
  remote: https://rubygems.org/
@@ -11,11 +13,15 @@ GEM
11
13
  byebug (11.1.3)
12
14
  coderay (1.1.3)
13
15
  diff-lcs (1.4.4)
14
- ethon (0.15.0)
15
- ffi (>= 1.15.0)
16
- ffi (1.15.4)
16
+ faraday (0.15.4)
17
+ multipart-post (>= 1.2, < 3)
18
+ faraday-http-cache (1.3.1)
19
+ faraday (~> 0.8)
20
+ faraday_middleware (0.13.1)
21
+ faraday (>= 0.7.4, < 1.0)
17
22
  jaro_winkler (1.5.4)
18
23
  method_source (1.0.0)
24
+ multipart-post (2.1.1)
19
25
  parallel (1.21.0)
20
26
  parser (3.0.2.0)
21
27
  ast (~> 2.4.1)
@@ -50,8 +56,6 @@ GEM
50
56
  ruby-progressbar (~> 1.7)
51
57
  unicode-display_width (>= 1.4.0, < 1.6)
52
58
  ruby-progressbar (1.11.0)
53
- typhoeus (1.4.0)
54
- ethon (>= 0.9.0)
55
59
  unicode-display_width (1.5.0)
56
60
 
57
61
  PLATFORMS
@@ -15,7 +15,7 @@ require 'json'
15
15
  require 'logger'
16
16
  require 'tempfile'
17
17
  require 'time'
18
- require 'typhoeus'
18
+ require 'faraday'
19
19
 
20
20
  module WCC::Ministries::Client
21
21
  class ApiClient
@@ -47,26 +47,29 @@ module WCC::Ministries::Client
47
47
  # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
48
48
  # the data deserialized from response body (could be nil), response status code and response headers.
49
49
  def call_api(http_method, path, opts = {})
50
- request = build_request(http_method, path, opts)
51
- response = request.run
50
+ begin
51
+ response = @config.connection.public_send(http_method.to_sym.downcase) do |req|
52
+ build_request(http_method, path, req, opts)
53
+ end
52
54
 
53
- if @config.debugging
54
- @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
55
- end
55
+ if @config.debugging
56
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
57
+ end
56
58
 
57
- unless response.success?
58
- if response.timed_out?
59
- fail ApiError.new('Connection timed out')
60
- elsif response.code == 0
61
- # Errors from libcurl will be made visible here
62
- fail ApiError.new(:code => 0,
63
- :message => response.return_message)
64
- else
65
- fail ApiError.new(:code => response.code,
66
- :response_headers => response.headers,
67
- :response_body => response.body),
68
- response.status_message
59
+ unless response.success?
60
+ if response.status == 0
61
+ # Errors from libcurl will be made visible here
62
+ fail ApiError.new(:code => 0,
63
+ :message => response.return_message)
64
+ else
65
+ fail ApiError.new(:code => response.status,
66
+ :response_headers => response.headers,
67
+ :response_body => response.body),
68
+ response.status
69
+ end
69
70
  end
71
+ rescue Faraday::TimeoutError
72
+ fail ApiError.new('Connection timed out')
70
73
  end
71
74
 
72
75
  if opts[:return_type]
@@ -74,7 +77,7 @@ module WCC::Ministries::Client
74
77
  else
75
78
  data = nil
76
79
  end
77
- return data, response.code, response.headers
80
+ return data, response.status, response.headers
78
81
  end
79
82
 
80
83
  # Builds the HTTP request
@@ -86,7 +89,7 @@ module WCC::Ministries::Client
86
89
  # @option opts [Hash] :form_params Query parameters
87
90
  # @option opts [Object] :body HTTP body (JSON/XML)
88
91
  # @return [Typhoeus::Request] A Typhoeus Request
89
- def build_request(http_method, path, opts = {})
92
+ def build_request(http_method, path, request, opts = {})
90
93
  url = build_request_url(path, opts)
91
94
  http_method = http_method.to_sym.downcase
92
95
 
@@ -94,9 +97,7 @@ module WCC::Ministries::Client
94
97
  query_params = opts[:query_params] || {}
95
98
  form_params = opts[:form_params] || {}
96
99
 
97
-
98
- # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
99
- _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
100
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
100
101
 
101
102
  req_opts = {
102
103
  :method => http_method,
@@ -104,16 +105,9 @@ module WCC::Ministries::Client
104
105
  :params => query_params,
105
106
  :params_encoding => @config.params_encoding,
106
107
  :timeout => @config.timeout,
107
- :ssl_verifypeer => @config.verify_ssl,
108
- :ssl_verifyhost => _verify_ssl_host,
109
- :sslcert => @config.cert_file,
110
- :sslkey => @config.key_file,
111
108
  :verbose => @config.debugging
112
109
  }
113
110
 
114
- # set custom cert, if provided
115
- req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
116
-
117
111
  if [:post, :patch, :put, :delete].include?(http_method)
118
112
  req_body = build_request_body(header_params, form_params, opts[:body])
119
113
  req_opts.update :body => req_body
@@ -121,8 +115,10 @@ module WCC::Ministries::Client
121
115
  @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
122
116
  end
123
117
  end
124
-
125
- request = Typhoeus::Request.new(url, req_opts)
118
+ request.headers = header_params
119
+ request.body = req_body
120
+ request.url url
121
+ request.params = query_params
126
122
  download_file(request) if opts[:return_type] == 'File'
127
123
  request
128
124
  end
@@ -135,13 +131,17 @@ module WCC::Ministries::Client
135
131
  # @return [String] HTTP body data in the form of string
136
132
  def build_request_body(header_params, form_params, body)
137
133
  # http form
138
- if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
139
- header_params['Content-Type'] == 'multipart/form-data'
134
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded'
135
+ data = URI.encode_www_form(form_params)
136
+ elsif header_params['Content-Type'] == 'multipart/form-data'
140
137
  data = {}
141
138
  form_params.each do |key, value|
142
139
  case value
143
- when ::File, ::Array, nil
144
- # let typhoeus handle File, Array and nil parameters
140
+ when ::File, ::Tempfile
141
+ # TODO hardcode to application/octet-stream, need better way to detect content type
142
+ data[key] = Faraday::UploadIO.new(value.path, 'application/octet-stream', value.path)
143
+ when ::Array, nil
144
+ # let Faraday handle Array and nil parameters
145
145
  data[key] = value
146
146
  else
147
147
  data[key] = value.to_s
@@ -155,41 +155,12 @@ module WCC::Ministries::Client
155
155
  data
156
156
  end
157
157
 
158
- # Save response body into a file in (the defined) temporary folder, using the filename
159
- # from the "Content-Disposition" header if provided, otherwise a random filename.
160
- # The response body is written to the file in chunks in order to handle files which
161
- # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
162
- # process can use.
163
- #
164
- # @see Configuration#temp_folder_path
165
158
  def download_file(request)
166
- tempfile = nil
167
- encoding = nil
168
- request.on_headers do |response|
169
- content_disposition = response.headers['Content-Disposition']
170
- if content_disposition && content_disposition =~ /filename=/i
171
- filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
172
- prefix = sanitize_filename(filename)
173
- else
174
- prefix = 'download-'
175
- end
176
- prefix = prefix + '-' unless prefix.end_with?('-')
177
- encoding = response.body.encoding
178
- tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
179
- @tempfile = tempfile
180
- end
181
- request.on_body do |chunk|
182
- chunk.force_encoding(encoding)
183
- tempfile.write(chunk)
184
- end
185
- request.on_complete do |response|
186
- if tempfile
187
- tempfile.close
188
- @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
189
- "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
190
- "will be deleted automatically with GC. It's also recommended to delete the temp file "\
191
- "explicitly with `tempfile.delete`"
192
- end
159
+ @stream = []
160
+
161
+ # handle streaming Responses
162
+ request.options.on_data = Proc.new do |chunk, overall_received_bytes|
163
+ @stream << chunk
193
164
  end
194
165
  end
195
166
 
@@ -214,7 +185,25 @@ module WCC::Ministries::Client
214
185
 
215
186
  # handle file downloading - return the File instance processed in request callbacks
216
187
  # note that response body is empty when the file is written in chunks in request on_body callback
217
- return @tempfile if return_type == 'File'
188
+ if return_type == 'File'
189
+ content_disposition = response.headers['Content-Disposition']
190
+ if content_disposition && content_disposition =~ /filename=/i
191
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
192
+ prefix = sanitize_filename(filename)
193
+ else
194
+ prefix = 'download-'
195
+ end
196
+ prefix = prefix + '-' unless prefix.end_with?('-')
197
+ encoding = body.encoding
198
+ @tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
199
+ @tempfile.write(@stream.join.force_encoding(encoding))
200
+ @tempfile.close
201
+ @config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
202
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
203
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
204
+ "explicitly with `tempfile.delete`"
205
+ return @tempfile
206
+ end
218
207
 
219
208
  return nil if body.nil? || body.empty?
220
209
 
@@ -98,33 +98,28 @@ module WCC::Ministries::Client
98
98
  # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
99
99
  #
100
100
  # @return [true, false]
101
- attr_accessor :verify_ssl
101
+ attr_accessor :ssl_verify
102
102
 
103
103
  ### TLS/SSL setting
104
- # Set this to false to skip verifying SSL host name
105
- # Default to true.
104
+ # Any `OpenSSL::SSL::` constant (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL.html)
106
105
  #
107
106
  # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
108
107
  #
109
- # @return [true, false]
110
- attr_accessor :verify_ssl_host
108
+ attr_accessor :ssl_verify_mode
111
109
 
112
110
  ### TLS/SSL setting
113
111
  # Set this to customize the certificate file to verify the peer.
114
112
  #
115
113
  # @return [String] the path to the certificate file
116
- #
117
- # @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
118
- # https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
119
- attr_accessor :ssl_ca_cert
114
+ attr_accessor :ssl_ca_file
120
115
 
121
116
  ### TLS/SSL setting
122
117
  # Client certificate file (for client certificate)
123
- attr_accessor :cert_file
118
+ attr_accessor :ssl_client_cert
124
119
 
125
120
  ### TLS/SSL setting
126
121
  # Client private key file (for client certificate)
127
- attr_accessor :key_file
122
+ attr_accessor :ssl_client_key
128
123
 
129
124
  # Set this to customize parameters encoding of array parameter with multi collectionFormat.
130
125
  # Default to nil.
@@ -136,6 +131,8 @@ module WCC::Ministries::Client
136
131
  attr_accessor :inject_format
137
132
 
138
133
  attr_accessor :force_ending_format
134
+
135
+ attr_accessor :cache_store
139
136
 
140
137
  def initialize
141
138
  @scheme = 'https'
@@ -149,15 +146,16 @@ module WCC::Ministries::Client
149
146
  @api_key_prefix = {}
150
147
  @timeout = 0
151
148
  @client_side_validation = true
152
- @verify_ssl = true
153
- @verify_ssl_host = true
154
- @params_encoding = nil
155
- @cert_file = nil
156
- @key_file = nil
149
+ @ssl_verify = true
150
+ @ssl_verify_mode = nil
151
+ @ssl_ca_file = nil
152
+ @ssl_client_cert = nil
153
+ @ssl_client_key = nil
157
154
  @debugging = false
158
155
  @inject_format = false
159
156
  @force_ending_format = false
160
157
  @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
158
+ @cache_store = defined?(Rails) ? Rails.cache : nil
161
159
 
162
160
  yield(self) if block_given?
163
161
  end
@@ -233,6 +231,34 @@ module WCC::Ministries::Client
233
231
  }
234
232
  end
235
233
 
234
+ def ssl_options
235
+ {
236
+ :ca_file => ssl_ca_file,
237
+ :verify => ssl_verify,
238
+ :verify_mode => ssl_verify_mode,
239
+ :client_cert => ssl_client_cert,
240
+ :client_key => ssl_client_key
241
+ }
242
+ end
243
+
244
+ # Allows supplying a custom Faraday connection
245
+ attr_writer :connection
246
+ def connection
247
+ # new one each time unless set in configuration
248
+ @connection ||
249
+ Faraday.new(:url => base_url, :ssl => ssl_options) do |conn|
250
+ # Use Rails.cache for conditional GET requests, b/c the Ministries
251
+ # API responds much faster to conditional GETs when fresh
252
+ # requires faraday-http-cache
253
+ conn.use :http_cache, store: cache_store if cache_store
254
+
255
+ conn.adapter(Faraday.default_adapter)
256
+
257
+ # Follow redirects (requires 'faraday_middleware')
258
+ conn.response :follow_redirects
259
+ end
260
+ end
261
+
236
262
  # Returns URL based on server settings
237
263
  #
238
264
  # @param index array index of the server settings
@@ -13,7 +13,7 @@ OpenAPI Generator version: unset
13
13
  module WCC
14
14
  module Ministries
15
15
  module Client
16
- VERSION = '1.0.2'
16
+ VERSION = '1.0.3'
17
17
  end
18
18
  end
19
19
  end
data/openapitools.json CHANGED
@@ -8,6 +8,7 @@
8
8
  "generatorName": "ruby",
9
9
  "output": "#{cwd}",
10
10
  "glob": "swagger.json",
11
+ "library": "faraday",
11
12
  "additionalProperties": {
12
13
  "gemName": "wcc_ministries_client",
13
14
  "moduleName": "WCC::Ministries::Client",
@@ -51,44 +51,6 @@ describe WCC::Ministries::Client::ApiClient do
51
51
  end
52
52
  end
53
53
 
54
- describe 'params_encoding in #build_request' do
55
- let(:config) { WCC::Ministries::Client::Configuration.new }
56
- let(:api_client) { WCC::Ministries::Client::ApiClient.new(config) }
57
-
58
- it 'defaults to nil' do
59
- expect(WCC::Ministries::Client::Configuration.default.params_encoding).to eq(nil)
60
- expect(config.params_encoding).to eq(nil)
61
-
62
- request = api_client.build_request(:get, '/test')
63
- expect(request.options[:params_encoding]).to eq(nil)
64
- end
65
-
66
- it 'can be customized' do
67
- config.params_encoding = :multi
68
- request = api_client.build_request(:get, '/test')
69
- expect(request.options[:params_encoding]).to eq(:multi)
70
- end
71
- end
72
-
73
- describe 'timeout in #build_request' do
74
- let(:config) { WCC::Ministries::Client::Configuration.new }
75
- let(:api_client) { WCC::Ministries::Client::ApiClient.new(config) }
76
-
77
- it 'defaults to 0' do
78
- expect(WCC::Ministries::Client::Configuration.default.timeout).to eq(0)
79
- expect(config.timeout).to eq(0)
80
-
81
- request = api_client.build_request(:get, '/test')
82
- expect(request.options[:timeout]).to eq(0)
83
- end
84
-
85
- it 'can be customized' do
86
- config.timeout = 100
87
- request = api_client.build_request(:get, '/test')
88
- expect(request.options[:timeout]).to eq(100)
89
- end
90
- end
91
-
92
54
  describe '#deserialize' do
93
55
  it "handles Array<Integer>" do
94
56
  api_client = WCC::Ministries::Client::ApiClient.new
@@ -27,7 +27,10 @@ Gem::Specification.new do |s|
27
27
  s.license = "MIT"
28
28
  s.required_ruby_version = ">= 2.4"
29
29
 
30
- s.add_runtime_dependency 'typhoeus', '~> 1.0', '>= 1.0.1'
30
+ # Expand the range of Faraday that can be used
31
+ s.add_runtime_dependency 'faraday', '>= 0.9', '< 2.0'
32
+ s.add_runtime_dependency 'faraday-http-cache', '>= 1.0', '< 3.0'
33
+ s.add_runtime_dependency 'faraday_middleware', '>= 0.13', '< 2.0'
31
34
 
32
35
  s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'
33
36
 
metadata CHANGED
@@ -1,35 +1,75 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wcc_ministries_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Watermark Community Church
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-19 00:00:00.000000000 Z
11
+ date: 2021-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: typhoeus
14
+ name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.0'
19
+ version: '0.9'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
20
27
  - - ">="
21
28
  - !ruby/object:Gem::Version
22
- version: 1.0.1
29
+ version: '0.9'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday-http-cache
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '1.0'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '3.0'
23
43
  type: :runtime
24
44
  prerelease: false
25
45
  version_requirements: !ruby/object:Gem::Requirement
26
46
  requirements:
27
- - - "~>"
47
+ - - ">="
28
48
  - !ruby/object:Gem::Version
29
49
  version: '1.0'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '3.0'
53
+ - !ruby/object:Gem::Dependency
54
+ name: faraday_middleware
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
30
57
  - - ">="
31
58
  - !ruby/object:Gem::Version
32
- version: 1.0.1
59
+ version: '0.13'
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0.13'
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '2.0'
33
73
  - !ruby/object:Gem::Dependency
34
74
  name: rspec
35
75
  requirement: !ruby/object:Gem::Requirement
@@ -109,9 +149,6 @@ files:
109
149
  - lib/wcc_ministries_client/models/page_relationship_data.rb
110
150
  - lib/wcc_ministries_client/version.rb
111
151
  - openapitools.json
112
- - pkg/wcc_ministries_client-1.0.0.gem
113
- - pkg/wcc_ministries_client-1.0.1.gem
114
- - pkg/wcc_ministries_client-1.0.2.gem
115
152
  - spec/api/ministry_api_spec.rb
116
153
  - spec/api_client_spec.rb
117
154
  - spec/configuration_spec.rb
@@ -157,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
194
  version: '0'
158
195
  requirements: []
159
196
  rubyforge_project:
160
- rubygems_version: 2.7.6.2
197
+ rubygems_version: 2.7.6.3
161
198
  signing_key:
162
199
  specification_version: 4
163
200
  summary: WCC Ministries API Ruby Gem
Binary file
Binary file
Binary file