faraday-patron 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 86f2d1771622458b46fddbeab9a9ddb34c8d12b2e43cbd3fea81414e39b0ab7e
4
+ data.tar.gz: d2a93bace59414b0b3609956b2528bc45d761f000446c5ae4ab713690929804e
5
+ SHA512:
6
+ metadata.gz: 292cc66a5c24bf2a124919e49bef1721d2c5eae6c2f30c94ec40207f634f0ba4e4b5135d8a83dbd9ef07d0ba8c8e38b3f24e8f90d18818cf30fd4ee1ad4a337b
7
+ data.tar.gz: '079ab563617d1b1b8dfe63590606a20726cca7c647ef5386db829bb2506a57174add14333d7c4dbd07d0d539b0f585746c7dc1814ed12341b9518725b83d6cb1'
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Jan van der Pas
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,62 @@
1
+ # Faraday Patron adapter
2
+
3
+ This gem is a [Faraday][faraday] adapter for the [Patron][patron] library.
4
+ Faraday is an HTTP client library that provides a common interface over many adapters.
5
+ Every adapter is defined into its own gem. This gem defines the adapter for Patron.
6
+
7
+ ## Installation
8
+
9
+ Add these lines to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'patron', '>= 2.2'
13
+ gem 'faraday'
14
+ gem 'faraday-patron'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install them yourself as:
22
+
23
+ $ gem install patron -v '>= 2.2'
24
+ $ gem install faraday faraday-patron
25
+
26
+ ## Usage
27
+
28
+ Configure your Faraday connection to use this adapter like this:
29
+
30
+ ```ruby
31
+ connection = Faraday.new(url, conn_options) do |conn|
32
+ conn.adapter(:patron)
33
+ end
34
+ ```
35
+
36
+ For more information on how to setup your Faraday connection and adapters usage, please refer to the [Faraday Website][faraday-website].
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](rubygems).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on [GitHub][repo].
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [license][license].
51
+
52
+ ## Code of Conduct
53
+
54
+ Everyone interacting in the Faraday Patron adapter project's codebase, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct][code-of-conduct].
55
+
56
+ [faraday]: https://github.com/lostisland/faraday
57
+ [faraday-website]: https://lostisland.github.io/faraday
58
+ [patron]: https://github.com/toland/patron
59
+ [rubygems]: https://rubygems.org
60
+ [repo]: https://github.com/lostisland/faraday-patron
61
+ [license]: https://github.com/lostisland/faraday-patron/blob/main/LICENSE.md
62
+ [code-of-conduct]: https://github.com/lostisland/faraday-patron/blob/main/CODE_OF_CONDUCT.md
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ class Adapter
5
+ # Patron adapter
6
+ class Patron < Faraday::Adapter
7
+ dependency 'patron'
8
+
9
+ def build_connection(env)
10
+ session = ::Patron::Session.new
11
+ @config_block&.call(session)
12
+ if (env[:url].scheme == 'https') && env[:ssl]
13
+ configure_ssl(session, env[:ssl])
14
+ end
15
+
16
+ if (req = env[:request])
17
+ configure_timeouts(session, req)
18
+ configure_proxy(session, req[:proxy])
19
+ end
20
+
21
+ session
22
+ end
23
+
24
+ def call(env)
25
+ super
26
+ # TODO: support streaming requests
27
+ env[:body] = env[:body].read if env[:body].respond_to? :read
28
+
29
+ response = connection(env) do |session|
30
+ begin
31
+ data = env[:body] ? env[:body].to_s : nil
32
+ session.request(env[:method], env[:url].to_s,
33
+ env[:request_headers], data: data)
34
+ rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
35
+ raise Faraday::ConnectionFailed, $ERROR_INFO
36
+ end
37
+ end
38
+
39
+ if (req = env[:request]).stream_response?
40
+ warn "Streaming downloads for #{self.class.name} " \
41
+ 'are not yet implemented.'
42
+ req.on_data.call(response.body, response.body.bytesize)
43
+ end
44
+ # Remove the "HTTP/1.1 200", leaving just the reason phrase
45
+ reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
46
+
47
+ save_response(env, response.status, response.body,
48
+ response.headers, reason_phrase)
49
+
50
+ @app.call env
51
+ rescue ::Patron::TimeoutError => e
52
+ if connection_timed_out_message?(e.message)
53
+ raise Faraday::ConnectionFailed, e
54
+ end
55
+
56
+ raise Faraday::TimeoutError, e
57
+ rescue ::Patron::Error => e
58
+ if e.message.include?('code 407')
59
+ raise Faraday::ConnectionFailed,
60
+ %(407 "Proxy Authentication Required ")
61
+ end
62
+
63
+ raise Faraday::ConnectionFailed, e
64
+ end
65
+
66
+ if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
67
+ # HAX: helps but doesn't work completely
68
+ # https://github.com/toland/patron/issues/34
69
+ ::Patron::Request::VALID_ACTIONS.tap do |actions|
70
+ if actions[0].is_a?(Symbol)
71
+ actions << :patch unless actions.include? :patch
72
+ actions << :options unless actions.include? :options
73
+ else
74
+ # Patron 0.4.20 and up
75
+ actions << 'PATCH' unless actions.include? 'PATCH'
76
+ actions << 'OPTIONS' unless actions.include? 'OPTIONS'
77
+ end
78
+ end
79
+ end
80
+
81
+ def configure_ssl(session, ssl)
82
+ if ssl.fetch(:verify, true)
83
+ session.cacert = ssl[:ca_file]
84
+ else
85
+ session.insecure = true
86
+ end
87
+ end
88
+
89
+ def configure_timeouts(session, req)
90
+ return unless req
91
+
92
+ if (sec = request_timeout(:read, req))
93
+ session.timeout = sec
94
+ end
95
+
96
+ return unless (sec = request_timeout(:open, req))
97
+
98
+ session.connect_timeout = sec
99
+ end
100
+
101
+ def configure_proxy(session, proxy)
102
+ return unless proxy
103
+
104
+ proxy_uri = proxy[:uri].dup
105
+ proxy_uri.user = proxy[:user] &&
106
+ Utils.escape(proxy[:user]).gsub('+', '%20')
107
+ proxy_uri.password = proxy[:password] &&
108
+ Utils.escape(proxy[:password]).gsub('+', '%20')
109
+ session.proxy = proxy_uri.to_s
110
+ end
111
+
112
+ private
113
+
114
+ CURL_TIMEOUT_MESSAGES = [
115
+ 'Connection time-out',
116
+ 'Connection timed out',
117
+ 'Timed out before name resolve',
118
+ 'server connect has timed out',
119
+ 'Resolving timed out',
120
+ 'name lookup timed out',
121
+ 'timed out before SSL',
122
+ 'connect() timed out'
123
+ ].freeze
124
+
125
+ def connection_timed_out_message?(message)
126
+ CURL_TIMEOUT_MESSAGES.any? do |curl_message|
127
+ message.include?(curl_message)
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'adapter/patron'
4
+ require_relative 'patron/version'
5
+
6
+ module Faraday
7
+ # Main Faraday::Patron module
8
+ module Patron
9
+ Faraday::Adapter.register_middleware(patron: Faraday::Adapter::Patron)
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module Patron
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,205 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-patron
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - "@iMacTia"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: patron
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.4.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.4.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.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.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.19.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.19.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: multipart-parser
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.1.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.1.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.4'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.12.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.12.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-packaging
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.5'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.5'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-performance
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.0'
167
+ description: Faraday adapter for Patron
168
+ email:
169
+ - giuffrida.mattia@gmail.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - LICENSE.md
175
+ - README.md
176
+ - lib/faraday/adapter/patron.rb
177
+ - lib/faraday/patron.rb
178
+ - lib/faraday/patron/version.rb
179
+ homepage: https://github.com/lostisland/faraday-patron
180
+ licenses:
181
+ - MIT
182
+ metadata:
183
+ homepage_uri: https://github.com/lostisland/faraday-patron
184
+ source_code_uri: https://github.com/lostisland/faraday-patron
185
+ changelog_uri: https://github.com/lostisland/faraday-patron
186
+ post_install_message:
187
+ rdoc_options: []
188
+ require_paths:
189
+ - lib
190
+ required_ruby_version: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: 2.4.0
195
+ required_rubygems_version: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - ">="
198
+ - !ruby/object:Gem::Version
199
+ version: '0'
200
+ requirements: []
201
+ rubygems_version: 3.1.6
202
+ signing_key:
203
+ specification_version: 4
204
+ summary: Faraday adapter for Patron
205
+ test_files: []