faraday 0.7.4 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +276 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +40 -153
  5. data/Rakefile +4 -139
  6. data/examples/client_spec.rb +65 -0
  7. data/examples/client_test.rb +79 -0
  8. data/lib/faraday/adapter/em_http.rb +286 -0
  9. data/lib/faraday/adapter/em_http_ssl_patch.rb +62 -0
  10. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +69 -0
  11. data/lib/faraday/adapter/em_synchrony.rb +120 -36
  12. data/lib/faraday/adapter/excon.rb +108 -12
  13. data/lib/faraday/adapter/httpclient.rb +152 -0
  14. data/lib/faraday/adapter/net_http.rb +187 -43
  15. data/lib/faraday/adapter/net_http_persistent.rb +91 -0
  16. data/lib/faraday/adapter/patron.rb +106 -10
  17. data/lib/faraday/adapter/rack.rb +75 -0
  18. data/lib/faraday/adapter/test.rb +160 -61
  19. data/lib/faraday/adapter/typhoeus.rb +7 -46
  20. data/lib/faraday/adapter.rb +105 -33
  21. data/lib/faraday/adapter_registry.rb +30 -0
  22. data/lib/faraday/autoload.rb +95 -0
  23. data/lib/faraday/connection.rb +525 -157
  24. data/lib/faraday/dependency_loader.rb +37 -0
  25. data/lib/faraday/encoders/flat_params_encoder.rb +98 -0
  26. data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
  27. data/lib/faraday/error.rb +122 -30
  28. data/lib/faraday/file_part.rb +128 -0
  29. data/lib/faraday/logging/formatter.rb +105 -0
  30. data/lib/faraday/middleware.rb +14 -22
  31. data/lib/faraday/middleware_registry.rb +129 -0
  32. data/lib/faraday/options/connection_options.rb +22 -0
  33. data/lib/faraday/options/env.rb +181 -0
  34. data/lib/faraday/options/proxy_options.rb +28 -0
  35. data/lib/faraday/options/request_options.rb +22 -0
  36. data/lib/faraday/options/ssl_options.rb +59 -0
  37. data/lib/faraday/options.rb +222 -0
  38. data/lib/faraday/param_part.rb +53 -0
  39. data/lib/faraday/parameters.rb +5 -0
  40. data/lib/faraday/rack_builder.rb +248 -0
  41. data/lib/faraday/request/authorization.rb +55 -0
  42. data/lib/faraday/request/basic_authentication.rb +20 -0
  43. data/lib/faraday/request/instrumentation.rb +54 -0
  44. data/lib/faraday/request/multipart.rb +84 -48
  45. data/lib/faraday/request/retry.rb +239 -0
  46. data/lib/faraday/request/token_authentication.rb +20 -0
  47. data/lib/faraday/request/url_encoded.rb +46 -27
  48. data/lib/faraday/request.rb +112 -50
  49. data/lib/faraday/response/logger.rb +24 -25
  50. data/lib/faraday/response/raise_error.rb +40 -11
  51. data/lib/faraday/response.rb +44 -35
  52. data/lib/faraday/utils/headers.rb +139 -0
  53. data/lib/faraday/utils/params_hash.rb +61 -0
  54. data/lib/faraday/utils.rb +72 -117
  55. data/lib/faraday.rb +142 -64
  56. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  57. data/spec/faraday/adapter/em_http_spec.rb +47 -0
  58. data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
  59. data/spec/faraday/adapter/excon_spec.rb +49 -0
  60. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  61. data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
  62. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  63. data/spec/faraday/adapter/patron_spec.rb +18 -0
  64. data/spec/faraday/adapter/rack_spec.rb +8 -0
  65. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  66. data/spec/faraday/adapter_registry_spec.rb +28 -0
  67. data/spec/faraday/adapter_spec.rb +55 -0
  68. data/spec/faraday/composite_read_io_spec.rb +80 -0
  69. data/spec/faraday/connection_spec.rb +691 -0
  70. data/spec/faraday/error_spec.rb +45 -0
  71. data/spec/faraday/middleware_spec.rb +26 -0
  72. data/spec/faraday/options/env_spec.rb +70 -0
  73. data/spec/faraday/options/options_spec.rb +297 -0
  74. data/spec/faraday/options/proxy_options_spec.rb +37 -0
  75. data/spec/faraday/options/request_options_spec.rb +19 -0
  76. data/spec/faraday/params_encoders/flat_spec.rb +34 -0
  77. data/spec/faraday/params_encoders/nested_spec.rb +134 -0
  78. data/spec/faraday/rack_builder_spec.rb +196 -0
  79. data/spec/faraday/request/authorization_spec.rb +88 -0
  80. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  81. data/spec/faraday/request/multipart_spec.rb +274 -0
  82. data/spec/faraday/request/retry_spec.rb +242 -0
  83. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  84. data/spec/faraday/request_spec.rb +109 -0
  85. data/spec/faraday/response/logger_spec.rb +220 -0
  86. data/spec/faraday/response/middleware_spec.rb +68 -0
  87. data/spec/faraday/response/raise_error_spec.rb +106 -0
  88. data/spec/faraday/response_spec.rb +75 -0
  89. data/spec/faraday/utils/headers_spec.rb +82 -0
  90. data/spec/faraday/utils_spec.rb +56 -0
  91. data/spec/faraday_spec.rb +37 -0
  92. data/spec/spec_helper.rb +132 -0
  93. data/spec/support/disabling_stub.rb +14 -0
  94. data/spec/support/fake_safe_buffer.rb +15 -0
  95. data/spec/support/helper_methods.rb +133 -0
  96. data/spec/support/shared_examples/adapter.rb +104 -0
  97. data/spec/support/shared_examples/params_encoder.rb +18 -0
  98. data/spec/support/shared_examples/request_method.rb +234 -0
  99. data/spec/support/streaming_response_checker.rb +35 -0
  100. data/spec/support/webmock_rack_app.rb +68 -0
  101. metadata +126 -126
  102. data/Gemfile +0 -29
  103. data/config.ru +0 -6
  104. data/faraday.gemspec +0 -92
  105. data/lib/faraday/adapter/action_dispatch.rb +0 -29
  106. data/lib/faraday/builder.rb +0 -160
  107. data/lib/faraday/request/json.rb +0 -35
  108. data/lib/faraday/upload_io.rb +0 -23
  109. data/test/adapters/live_test.rb +0 -205
  110. data/test/adapters/logger_test.rb +0 -37
  111. data/test/adapters/net_http_test.rb +0 -33
  112. data/test/adapters/test_middleware_test.rb +0 -70
  113. data/test/connection_test.rb +0 -254
  114. data/test/env_test.rb +0 -158
  115. data/test/helper.rb +0 -41
  116. data/test/live_server.rb +0 -45
  117. data/test/middleware_stack_test.rb +0 -118
  118. data/test/request_middleware_test.rb +0 -116
  119. data/test/response_middleware_test.rb +0 -74
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1d23ec0aefc40d23ccc4410db08092bd29e477309fb8aa6794b3c8401961de10
4
+ data.tar.gz: c33ca7313dcccbf148266a80aa4e12d4cba96a4feb96ecfc3e8ff50fa06756c5
5
+ SHA512:
6
+ metadata.gz: 8058c22dc6e78bc3d46a9c8a22c458656a1380a4aed62b31e11b9b054fc6791373dd103276fc018148daed3934a724bcb39390bbe88e2762281ff810b61b408d
7
+ data.tar.gz: 341f3b44148f666bb03f8e7b232fd92e26b8c34d758090e6e82f1276af29ff17d4f62bdb58871254ba48b2232ab58269c71db08df7a4b0c35c53f2c7e9bbe7e8
data/CHANGELOG.md ADDED
@@ -0,0 +1,276 @@
1
+ # Faraday Changelog
2
+
3
+ ## v1.0
4
+
5
+ Features:
6
+
7
+ * Add #trace support to Faraday::Connection #861 (@technoweenie)
8
+ * Add the log formatter that is easy to override and safe to inherit #889 (@prikha)
9
+ * Support standalone adapters #941 (@iMacTia)
10
+ * Introduce Faraday::ConflictError for 409 response code #979 (@lucasmoreno)
11
+ * Add support for setting `read_timeout` option separately #1003 (@springerigor)
12
+ * Refactor and cleanup timeout settings across adapters #1022 (@technoweenie)
13
+ * Create ParamPart class to allow multipart posts with JSON content and file upload at the same time #1017 (@jeremy-israel)
14
+ * Copy UploadIO const -> FilePart for consistency with ParamPart #1018, #1021 (@technoweenie)
15
+ * Implement streaming responses in the Excon adapter #1026 (@technoweenie)
16
+ * Add default implementation of `Middleware#close`. #1069 (@ioquatix)
17
+ * Add `Adapter#close` so that derived classes can call super. #1091 (@ioquatix)
18
+ * Add log_level option to logger default formatter #1079 (@amrrbakry)
19
+ * Fix empty array for FlatParamsEncoder `{key: []} -> "key="` #1084 (@mrexox)
20
+
21
+ Bugs:
22
+
23
+ * Explicitly require date for DateTime library in Retry middleware #844 (@nickpresta)
24
+ * Refactor Adapter as final endpoints #846 (@iMacTia)
25
+ * Separate Request and Response bodies in Faraday::Env #847 (@iMacTia)
26
+ * Implement Faraday::Connection#options to make HTTP requests with the OPTIONS verb. #857 (@technoweenie)
27
+ * Multipart: Drop Ruby 1.8 String behavior compat #892 (@olleolleolle)
28
+ * Fix Ruby warnings in Faraday::Options.memoized #962 (@technoweenie)
29
+ * Allow setting min/max SSL version for a Net::HTTP::Persistent connection #972, #973 (@bdewater, @olleolleolle)
30
+ * Fix instances of frozen empty string literals #1040 (@BobbyMcWho)
31
+ * remove temp_proxy and improve proxy tests #1063 (@technoweenie)
32
+ * improve error initializer consistency #1095 (@technoweenie)
33
+
34
+ Misc:
35
+
36
+ * Convert minitest suite to RSpec #832 (@iMacTia, with help from @gaynetdinov, @Insti, @technoweenie)
37
+ * Major effort to update code to RuboCop standards. #854 (@olleolleolle, @iMacTia, @technoweenie, @htwroclau, @jherdman, @Drenmi, @Insti)
38
+ * Rubocop #1044, #1047 (@BobbyMcWho, @olleolleolle)
39
+ * Documentation tweaks (@adsteel, @Hubro, @iMacTia, @olleolleolle, @technoweenie)
40
+ * Update license year #981 (@Kevin-Kawai)
41
+ * Configure Jekyll plugin jekyll-remote-theme to support Docker usage #999 (@Lewiscowles1986)
42
+ * Fix Ruby 2.7 warnings #1009 (@tenderlove)
43
+ * Cleanup adapter connections #1023 (@technoweenie)
44
+ * Describe clearing cached stubs #1045 (@viraptor)
45
+ * Add project metadata to the gemspec #1046 (@orien)
46
+
47
+ ## v0.17.3
48
+
49
+ Fixes:
50
+
51
+ * Reverts changes in error classes hierarchy. #1092 (@iMacTia)
52
+ * Fix Ruby 1.9 syntax errors and improve Error class testing #1094 (@BanzaiMan,
53
+ @mrexox, @technoweenie)
54
+
55
+ Misc:
56
+
57
+ * Stops using `&Proc.new` for block forwarding. #1083 (@olleolleolle)
58
+ * Update CI to test against ruby 2.0-2.7 #1087, #1099 (@iMacTia, @olleolleolle,
59
+ @technoweenie)
60
+ * require FARADAY_DEPRECATE=warn to show Faraday v1.0 deprecation warnings
61
+ #1098 (@technoweenie)
62
+
63
+ ## v0.17.1
64
+
65
+ Final release before Faraday v1.0, with important fixes for Ruby 2.7.
66
+
67
+ Fixes:
68
+
69
+ * RaiseError response middleware raises exception if HTTP client returns a nil
70
+ status. #1042 (@jonnyom, @BobbyMcWho)
71
+
72
+ Misc:
73
+
74
+ * Fix Ruby 2.7 warnings (#1009)
75
+ * Add `Faraday::Deprecate` to warn about upcoming v1.0 changes. (#1054, #1059,
76
+ #1076, #1077)
77
+ * Add release notes up to current in CHANGELOG.md (#1066)
78
+ * Port minimal rspec suite from main branch to run backported tests. (#1058)
79
+
80
+ ## v0.17.0
81
+
82
+ This release is the same as v0.15.4. It was pushed to cover up releases
83
+ v0.16.0-v0.16.2.
84
+
85
+ ## v0.15.4
86
+
87
+ * Expose `pool_size` as a option for the NetHttpPersistent adapter (#834)
88
+
89
+ ## v0.15.3
90
+
91
+ * Make Faraday::Request serialisable with Marshal. (#803)
92
+ * Add DEFAULT_EXCEPTIONS constant to Request::Retry (#814)
93
+ * Add support for Ruby 2.6 Net::HTTP write_timeout (#824)
94
+
95
+ ## v0.15.2
96
+
97
+ * Prevents `Net::HTTP` adapters to retry request internally by setting `max_retries` to 0 if available (Ruby 2.5+). (#799)
98
+ * Fixes `NestedParamsEncoder` handling of empty array values (#801)
99
+
100
+ ## v0.15.1
101
+
102
+ * NetHttpPersistent adapter better reuse of SSL connections (#793)
103
+ * Refactor: inline cached_connection (#797)
104
+ * Logger middleware: use $stdout instead of STDOUT (#794)
105
+ * Fix: do not memoize/reuse Patron session (#796)
106
+
107
+ Also in this release:
108
+
109
+ * Allow setting min/max ssl version for Net::HTTP (#792)
110
+ * Allow setting min/max ssl version for Excon (#795)
111
+
112
+ ## v0.15.0
113
+
114
+ Features:
115
+
116
+ * Added retry block option to retry middleware. (#770)
117
+ * Retry middleware improvements (honour Retry-After header, retry statuses) (#773)
118
+ * Improve response logger middleware output (#784)
119
+
120
+ Fixes:
121
+
122
+ * Remove unused class error (#767)
123
+ * Fix minor typo in README (#760)
124
+ * Reuse persistent connections when using net-http-persistent (#778)
125
+ * Fix Retry middleware documentation (#781)
126
+ * Returns the http response when giving up on retrying by status (#783)
127
+
128
+ ## v0.14.0
129
+
130
+ Features:
131
+
132
+ * Allow overriding env proxy #754 (@iMacTia)
133
+ * Remove legacy Typhoeus adapter #715 (@olleolleolle)
134
+ * External Typhoeus Adapter Compatibility #748 (@iMacTia)
135
+ * Warn about missing adapter when making a request #743 (@antstorm)
136
+ * Faraday::Adapter::Test stubs now support entire urls (with host) #741 (@erik-escobedo)
137
+
138
+ Fixes:
139
+
140
+ * If proxy is manually provided, this takes priority over `find_proxy` #724 (@iMacTia)
141
+ * Fixes the behaviour for Excon's open_timeout (not setting write_timeout anymore) #731 (@apachelogger)
142
+ * Handle all connection timeout messages in Patron #687 (@stayhero)
143
+
144
+ ## v0.13.1
145
+
146
+ * Fixes an incompatibility with Addressable::URI being used as uri_parser
147
+
148
+ ## v0.13.0
149
+
150
+ Features:
151
+
152
+ * Dynamically reloads the proxy when performing a request on an absolute domain (#701)
153
+ * Adapter support for Net::HTTP::Persistent v3.0.0 (#619)
154
+
155
+ Fixes:
156
+
157
+ * Prefer #hostname over #host. (#714)
158
+ * Fixes an edge-case issue with response headers parsing (missing HTTP header) (#719)
159
+
160
+ ## v0.12.2
161
+
162
+ * Parse headers from aggregated proxy requests/responses (#681)
163
+ * Guard against invalid middleware configuration with warning (#685)
164
+ * Do not use :insecure option by default in Patron (#691)
165
+ * Fixes an issue with HTTPClient not raising a `Faraday::ConnectionFailed` (#702)
166
+ * Fixes YAML serialization/deserialization for `Faraday::Utils::Headers` (#690)
167
+ * Fixes an issue with Options having a nil value (#694)
168
+ * Fixes an issue with Faraday.default_connection not using Faraday.default_connection_options (#698)
169
+ * Fixes an issue with Options.merge! and Faraday instrumentation middleware (#710)
170
+
171
+ ## v0.12.1
172
+
173
+ * Fix an issue with Patron tests failing on jruby
174
+ * Fix an issue with new `rewind_files` feature that was causing an exception when the body was not an Hash
175
+ * Expose wrapped_exception in all client errors
176
+ * Add Authentication Section to the ReadMe
177
+
178
+ ## v0.12.0.1
179
+
180
+ * Hotfix release to address an issue with TravisCI deploy on Rubygems
181
+
182
+ ## v0.12.0
183
+
184
+ Features:
185
+
186
+ * Proxy feature now relies on Ruby `URI::Generic#find_proxy` and can use `no_proxy` ENV variable (not compatible with ruby < 2.0)
187
+ * Adds support for `context` request option to pass arbitrary information to middlewares
188
+
189
+ Fixes:
190
+
191
+ * Fix an issue with options that was causing new options to override defaults ones unexpectedly
192
+ * Rewind `UploadIO`s on retry to fix a compatibility issue
193
+ * Make multipart boundary unique
194
+ * Improvements in `README.md`
195
+
196
+ ## v0.11.0
197
+
198
+ Features:
199
+
200
+ * Add `filter` method to Logger middleware
201
+ * Add support for Ruby2.4 and Minitest 6
202
+ * Introduce block syntax to customise the adapter
203
+
204
+ Fixes:
205
+
206
+ * Fix an issue that was allowing to override `default_connection_options` from a connection instance
207
+ * Fix a bug that was causing newline escape characters ("\n") to be used when building the Authorization header
208
+
209
+ ## v0.10.1
210
+
211
+ - Fix an issue with HTTPClient adapter that was causing the SSL to be reset on every request
212
+ - Rescue `IOError` instead of specific subclass
213
+ - `Faraday::Utils::Headers` can now be successfully serialised in YAML
214
+ - Handle `default_connection_options` set with hash
215
+
216
+ ## v0.10.0
217
+
218
+ Breaking changes:
219
+ - Drop support for Ruby 1.8
220
+
221
+ Features:
222
+ - Include wrapped exception/reponse in ClientErrors
223
+ - Add `response.reason_phrase`
224
+ - Provide option to selectively skip logging request/response headers
225
+ - Add regex support for pattern matching in `test` adapter
226
+
227
+ Fixes:
228
+ - Add `Faraday.respond_to?` to find methods managed by `method_missing`
229
+ - em-http: `request.host` instead of `connection.host` should be taken for SSL validations
230
+ - Allow `default_connection_options` to be merged when options are passed as url parameter
231
+ - Improve splitting key-value pairs in raw HTTP headers
232
+
233
+ ## v0.9.2
234
+
235
+ Adapters:
236
+ - Enable gzip compression for httpclient
237
+ - Fixes default certificate store for httpclient not having default paths.
238
+ - Make excon adapter compatible with 0.44 excon version
239
+ - Add compatibility with Patron 0.4.20
240
+ - Determine default port numbers in Net::HTTP adapters (Addressable compatibility)
241
+ - em-http: wrap "connection closed by server" as ConnectionFailed type
242
+ - Wrap Errno::ETIMEDOUT in Faraday::Error::TimeoutError
243
+
244
+ Utils:
245
+ - Add Rack-compatible support for parsing `a[][b]=c` nested queries
246
+ - Encode nil values in queries different than empty strings. Before: `a=`; now: `a`.
247
+ - Have `Faraday::Utils::Headers#replace` clear internal key cache
248
+ - Dup the internal key cache when a Headers hash is copied
249
+
250
+ Env and middleware:
251
+ - Ensure `env` stored on middleware response has reference to the response
252
+ - Ensure that Response properties are initialized during `on_complete` (VCR compatibility)
253
+ - Copy request options in Faraday::Connection#dup
254
+ - Env custom members should be copied by Env.from(env)
255
+ - Honour per-request `request.options.params_encoder`
256
+ - Fix `interval_randomness` data type for Retry middleware
257
+ - Add maximum interval option for Retry middleware
258
+
259
+ ## v0.9.1
260
+
261
+ * Refactor Net:HTTP adapter so that with_net_http_connection can be overridden to allow pooled connections. (@Ben-M)
262
+ * Add configurable methods that bypass `retry_if` in the Retry request middleware. (@mike-bourgeous)
263
+
264
+ ## v0.9.0
265
+
266
+ * Add HTTPClient adapter (@hakanensari)
267
+ * Improve Retry handler (@mislav)
268
+ * Remove autoloading by default (@technoweenie)
269
+ * Improve internal docs (@technoweenie, @mislav)
270
+ * Respect user/password in http proxy string (@mislav)
271
+ * Adapter options are structs. Reinforces consistent options across adapters
272
+ (@technoweenie)
273
+ * Stop stripping trailing / off base URLs in a Faraday::Connection. (@technoweenie)
274
+ * Add a configurable URI parser. (@technoweenie)
275
+ * Remove need to manually autoload when using the authorization header helpers on `Faraday::Connection`. (@technoweenie)
276
+ * `Faraday::Adapter::Test` respects the `Faraday::RequestOptions#params_encoder` option. (@technoweenie)
data/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 rick olson, zack hobson
1
+ Copyright (c) 2009-2019 Rick Olson, Zack Hobson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,168 +1,55 @@
1
- faraday
2
- =======
3
- Modular HTTP client library using middleware heavily inspired by Rack.
1
+ # [![Faraday](./docs/assets/img/repo-card-slim.png)][website]
4
2
 
5
- This mess is gonna get raw, like sushi. So, haters to the left.
3
+ [![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
4
+ ![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/f869daab091ceef1da73/test_coverage)](https://codeclimate.com/github/lostisland/faraday/test_coverage)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/f869daab091ceef1da73/maintainability)](https://codeclimate.com/github/lostisland/faraday/maintainability)
7
+ [![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
6
8
 
7
- Usage
8
- -----
9
- conn = Faraday.new(:url => 'http://sushi.com') do |builder|
10
- builder.use Faraday::Request::UrlEncoded # convert request params as "www-form-urlencoded"
11
- builder.use Faraday::Request::JSON # encode request params as json
12
- builder.use Faraday::Response::Logger # log the request to STDOUT
13
- builder.use Faraday::Adapter::NetHttp # make http requests with Net::HTTP
14
9
 
15
- # or, use shortcuts:
16
- builder.request :url_encoded
17
- builder.request :json
18
- builder.response :logger
19
- builder.adapter :net_http
20
- end
10
+ Faraday is an HTTP client library that provides a common interface over many
11
+ adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
12
+ processing the request/response cycle.
21
13
 
22
- ## GET ##
14
+ ## Getting Started
23
15
 
24
- response = conn.get '/nigiri/sake.json' # GET http://sushi.com/nigiri/sake.json
25
- response.body
16
+ The best starting point is the [Faraday Website][website], with its introduction and explanation.
17
+ Need more details? See the [Faraday API Documentation][apidoc] to see how it works internally.
26
18
 
27
- conn.get '/nigiri', 'X-Awesome' => true # custom request header
19
+ ## Supported Ruby versions
28
20
 
29
- conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
30
- req.url '/search', :page => 2
31
- req.params['limit'] = 100
32
- end
21
+ This library aims to support and is [tested against][actions] the following Ruby
22
+ implementations:
33
23
 
34
- ## POST ##
24
+ * Ruby 2.4+
35
25
 
36
- conn.post '/nigiri', { :name => 'Maguro' } # POST "name=maguro" to http://sushi.com/nigiri
26
+ If something doesn't work on one of these Ruby versions, it's a bug.
37
27
 
38
- # post payload as JSON instead of "www-form-urlencoded" encoding:
39
- conn.post '/nigiri', payload, 'Content-Type' => 'application/json'
28
+ This library may inadvertently work (or seem to work) on other Ruby
29
+ implementations and versions, however support will only be provided for the versions listed
30
+ above.
40
31
 
41
- # a more verbose way:
42
- conn.post do |req|
43
- req.url '/nigiri'
44
- req.headers['Content-Type'] = 'application/json'
45
- req.body = { :name => 'Unagi' }
46
- end
32
+ If you would like this library to support another Ruby version, you may
33
+ volunteer to be a maintainer. Being a maintainer entails making sure all tests
34
+ run and pass on that implementation. When something breaks on your
35
+ implementation, you will be responsible for providing patches in a timely
36
+ fashion. If critical issues for a particular implementation exist at the time
37
+ of a major release, support for that Ruby version may be dropped.
47
38
 
48
- If you're ready to roll with just the bare minimum:
39
+ ## Contribute
49
40
 
50
- # default stack (net/http), no extra middleware:
51
- response = Faraday.get 'http://sushi.com/nigiri/sake.json'
41
+ Do you want to contribute to Faraday?
42
+ Open the issues page and check for the `help wanted` label!
43
+ But before you start coding, please read our [Contributing Guide][contributing]
52
44
 
53
- Advanced middleware usage
54
- -------------------------
55
- The order in which middleware is stacked is important. Like with Rack, the first middleware on the list wraps all others, while the last middleware is the innermost one, so that's usually the adapter.
45
+ ## Copyright
46
+ &copy; 2009 - 2019, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
56
47
 
57
- conn = Faraday.new(:url => 'http://sushi.com') do |builder|
58
- # POST/PUT params encoders:
59
- builder.request :multipart
60
- builder.request :url_encoded
61
- builder.request :json
62
-
63
- builder.adapter :net_http
64
- end
65
-
66
- This request middleware setup affects POST/PUT requests in the following way:
67
-
68
- 1. `Request::Multipart` checks for files in the payload, otherwise leaves everything untouched;
69
- 2. `Request::UrlEncoded` encodes as "application/x-www-form-urlencoded" if not already encoded or of another type
70
- 2. `Request::JSON` encodes as "application/json" if not already encoded or of another type
71
-
72
- Because "UrlEncoded" is higher on the stack than JSON encoder, it will get to process the request first. Swapping them means giving the other priority. Specifying the "Content-Type" for the request is explicitly stating which middleware should process it.
73
-
74
- Examples:
75
-
76
- payload = { :name => 'Maguro' }
77
-
78
- # post payload as JSON instead of urlencoded:
79
- conn.post '/nigiri', payload, 'Content-Type' => 'application/json'
80
-
81
- # uploading a file:
82
- payload = { :profile_pic => Faraday::UploadIO.new('avatar.jpg', 'image/jpeg') }
83
-
84
- # "Multipart" middleware detects files and encodes with "multipart/form-data":
85
- conn.put '/profile', payload
86
-
87
- Writing middleware
88
- ------------------
89
- Middleware are classes that respond to `call()`. They wrap the request/response cycle.
90
-
91
- def call(env)
92
- # do something with the request
93
-
94
- @app.call(env).on_complete do
95
- # do something with the response
96
- end
97
- end
98
-
99
- It's important to do all processing of the response only in the `on_complete` block. This enables middleware to work in parallel mode where requests are asynchronous.
100
-
101
- The `env` is a hash with symbol keys that contains info about the request and, later, response. Some keys are:
102
-
103
- # request phase
104
- :method - :get, :post, ...
105
- :url - URI for the current request; also contains GET parameters
106
- :body - POST parameters for :post/:put requests
107
- :request_headers
108
-
109
- # response phase
110
- :status - HTTP response status code, such as 200
111
- :body - the response body
112
- :response_headers
113
-
114
- Testing
115
- -------
116
- # It's possible to define stubbed request outside a test adapter block.
117
- stubs = Faraday::Adapter::Test::Stubs.new do |stub|
118
- stub.get('/tamago') { [200, {}, 'egg'] }
119
- end
120
-
121
- # You can pass stubbed request to the test adapter or define them in a block
122
- # or a combination of the two.
123
- test = Faraday.new do |builder|
124
- builder.adapter :test, stubs do |stub|
125
- stub.get('/ebi') {[ 200, {}, 'shrimp' ]}
126
- end
127
- end
128
-
129
- # It's also possible to stub additional requests after the connection has
130
- # been initialized. This is useful for testing.
131
- stubs.get('/uni') {[ 200, {}, 'urchin' ]}
132
-
133
- resp = test.get '/tamago'
134
- resp.body # => 'egg'
135
- resp = test.get '/ebi'
136
- resp.body # => 'shrimp'
137
- resp = test.get '/uni'
138
- resp.body # => 'urchin'
139
- resp = test.get '/else' #=> raises "no such stub" error
140
-
141
- # If you like, you can treat your stubs as mocks by verifying that all of
142
- # the stubbed calls were made. NOTE that this feature is still fairly
143
- # experimental: It will not verify the order or count of any stub, only that
144
- # it was called once during the course of the test.
145
- stubs.verify_stubbed_calls
146
-
147
- TODO
148
- ----
149
- * support streaming requests/responses
150
- * better stubbing API
151
- * Support timeouts
152
- * Add curb, em-http, fast_http
153
-
154
- Note on Patches/Pull Requests
155
- -----------------------------
156
- * Fork the project.
157
- * Make your feature addition or bug fix.
158
- * Add tests for it. This is important so I don't break it in a
159
- future version unintentionally.
160
- * Commit, do not mess with rakefile, version, or history.
161
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
162
- * Send me a pull request. Bonus points for topic branches.
163
-
164
- Copyright
165
- ---------
166
- Copyright (c) 2009-2011 rick, hobson. See [LICENSE][license] for details.
167
-
168
- [license]: https://github.com/technoweenie/faraday/blob/master/LICENSE.md
48
+ [website]: https://lostisland.github.io/faraday
49
+ [faraday_team]: https://lostisland.github.io/faraday/team
50
+ [contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
51
+ [apidoc]: http://www.rubydoc.info/gems/faraday
52
+ [actions]: https://github.com/lostisland/faraday/actions
53
+ [jruby]: http://jruby.org/
54
+ [rubinius]: http://rubini.us/
55
+ [license]: LICENSE.md
data/Rakefile CHANGED
@@ -1,142 +1,7 @@
1
- #!/usr/bin/env rake
1
+ # frozen_string_literal: true
2
2
 
3
- require 'date'
3
+ require 'rspec/core/rake_task'
4
4
 
5
- #############################################################################
6
- #
7
- # Helper functions
8
- #
9
- #############################################################################
5
+ RSpec::Core::RakeTask.new(:spec)
10
6
 
11
- def name
12
- @name ||= Dir['*.gemspec'].first.split('.').first
13
- end
14
-
15
- def version
16
- line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
- line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
- end
19
-
20
- def date
21
- Date.today.to_s
22
- end
23
-
24
- def rubyforge_project
25
- name
26
- end
27
-
28
- def gemspec_file
29
- "#{name}.gemspec"
30
- end
31
-
32
- def gem_file
33
- "#{name}-#{version}.gem"
34
- end
35
-
36
- def replace_header(head, header_name)
37
- head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
- end
39
-
40
- #############################################################################
41
- #
42
- # Standard tasks
43
- #
44
- #############################################################################
45
-
46
- task :default => :test
47
-
48
- require 'rake/testtask'
49
- Rake::TestTask.new(:test) do |test|
50
- test.libs << 'lib' << 'test'
51
- test.pattern = 'test/**/*_test.rb'
52
- test.verbose = true
53
- end
54
-
55
- TEST_SERVER = 'http://faradaylive.heroku.com'
56
-
57
- desc "Run tests including live tests against #{TEST_SERVER}"
58
- task :"test:live" do
59
- ENV['LIVE'] = TEST_SERVER
60
- Rake::Task[:test].invoke
61
- end
62
-
63
- desc "Open an irb session preloaded with this library"
64
- task :console do
65
- sh "irb -rubygems -r ./lib/#{name}.rb"
66
- end
67
-
68
- #############################################################################
69
- #
70
- # Custom tasks (add your own tasks here)
71
- #
72
- #############################################################################
73
-
74
-
75
-
76
- #############################################################################
77
- #
78
- # Packaging tasks
79
- #
80
- #############################################################################
81
-
82
- desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
83
- task :release => :build do
84
- unless `git branch` =~ /^\* master$/
85
- puts "You must be on the master branch to release!"
86
- exit!
87
- end
88
- sh "git commit --allow-empty -a -m 'Release #{version}'"
89
- sh "git tag v#{version}"
90
- sh "git push origin master"
91
- sh "git push origin v#{version}"
92
- sh "gem push pkg/#{gem_file}"
93
- end
94
-
95
- desc "Build #{gem_file} into the pkg directory"
96
- task :build => :gemspec do
97
- sh "mkdir -p pkg"
98
- sh "gem build #{gemspec_file}"
99
- sh "mv #{gem_file} pkg"
100
- end
101
-
102
- desc "Generate #{gemspec_file}"
103
- task :gemspec => :validate do
104
- # read spec file and split out manifest section
105
- spec = File.read(gemspec_file)
106
- head, manifest, tail = spec.split(" # = MANIFEST =\n")
107
-
108
- # replace name version and date
109
- replace_header(head, :name)
110
- replace_header(head, :version)
111
- replace_header(head, :date)
112
- #comment this out if your rubyforge_project has a different name
113
- replace_header(head, :rubyforge_project)
114
-
115
- # determine file list from git ls-files
116
- files = `git ls-files`.
117
- split("\n").
118
- sort.
119
- reject { |file| file =~ /^\./ }.
120
- reject { |file| file =~ /^(rdoc|pkg)/ }.
121
- map { |file| " #{file}" }.
122
- join("\n")
123
-
124
- # piece file back together and write
125
- manifest = " s.files = %w[\n#{files}\n ]\n"
126
- spec = [head, manifest, tail].join(" # = MANIFEST =\n")
127
- File.open(gemspec_file, 'w') { |io| io.write(spec) }
128
- puts "Updated #{gemspec_file}"
129
- end
130
-
131
- desc "Validate #{gemspec_file}"
132
- task :validate do
133
- libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
134
- unless libfiles.empty?
135
- puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
136
- exit!
137
- end
138
- unless Dir['VERSION*'].empty?
139
- puts "A `VERSION` file at root level violates Gem best practices."
140
- exit!
141
- end
142
- end
7
+ task default: :spec