faraday_middleware 0.9.2 → 0.10.0
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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2434c5fb11c290e271ebba1748a43ae5d320735e
|
4
|
+
data.tar.gz: e0bf3f73a45c469e8e2908ba102586299adb76b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcbbc5d1571c285bee8124873a91278ee3ce26eab0a7e6b5a08bf4c9a22cef8f7bd9ce13e29dc308f0c7f7537720b373a3ed4334141a009b7c971ba80f9cea92
|
7
|
+
data.tar.gz: acc1a19eb83cce409d3b7586714fd787b1830e7f03e222b688d9ad5a52b41ab0a142fdc1406cb8a0d6e89afc1b5bc0915c681394f16ec8d44d88eb468dc15bbc
|
data/lib/faraday_middleware.rb
CHANGED
@@ -17,6 +17,7 @@ module FaradayMiddleware
|
|
17
17
|
autoload :RackCompatible, 'faraday_middleware/rack_compatible'
|
18
18
|
autoload :FollowRedirects, 'faraday_middleware/response/follow_redirects'
|
19
19
|
autoload :Instrumentation, 'faraday_middleware/instrumentation'
|
20
|
+
autoload :Gzip, 'faraday_middleware/gzip'
|
20
21
|
|
21
22
|
if Faraday::Middleware.respond_to? :register_middleware
|
22
23
|
Faraday::Request.register_middleware \
|
@@ -39,7 +40,8 @@ module FaradayMiddleware
|
|
39
40
|
:chunked => lambda { Chunked }
|
40
41
|
|
41
42
|
Faraday::Middleware.register_middleware \
|
42
|
-
:instrumentation => lambda { Instrumentation }
|
43
|
+
:instrumentation => lambda { Instrumentation },
|
44
|
+
:gzip => lambda { Gzip }
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module FaradayMiddleware
|
4
|
+
# Middleware to automatically decompress response bodies. If the
|
5
|
+
# "Accept-Encoding" header wasn't set in the request, this sets it to
|
6
|
+
# "gzip,deflate" and appropriately handles the compressed response from the
|
7
|
+
# server. This resembles what Ruby 1.9+ does internally in Net::HTTP#get.
|
8
|
+
#
|
9
|
+
# This middleware is NOT necessary when these adapters are used:
|
10
|
+
# - net_http on Ruby 1.9+
|
11
|
+
# - net_http_persistent on Ruby 2.0+
|
12
|
+
# - em_http
|
13
|
+
class Gzip < Faraday::Middleware
|
14
|
+
dependency 'zlib'
|
15
|
+
|
16
|
+
ACCEPT_ENCODING = 'Accept-Encoding'.freeze
|
17
|
+
CONTENT_ENCODING = 'Content-Encoding'.freeze
|
18
|
+
CONTENT_LENGTH = 'Content-Length'.freeze
|
19
|
+
SUPPORTED_ENCODINGS = 'gzip,deflate'.freeze
|
20
|
+
RUBY_ENCODING = '1.9'.respond_to?(:force_encoding)
|
21
|
+
|
22
|
+
def call(env)
|
23
|
+
env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS
|
24
|
+
@app.call(env).on_complete do |response_env|
|
25
|
+
case response_env[:response_headers][CONTENT_ENCODING]
|
26
|
+
when 'gzip'
|
27
|
+
reset_body(response_env, &method(:uncompress_gzip))
|
28
|
+
when 'deflate'
|
29
|
+
reset_body(response_env, &method(:inflate))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def reset_body(env)
|
35
|
+
env[:body] = yield(env[:body])
|
36
|
+
env[:response_headers].delete(CONTENT_ENCODING)
|
37
|
+
env[:response_headers][CONTENT_LENGTH] = env[:body].length
|
38
|
+
end
|
39
|
+
|
40
|
+
def uncompress_gzip(body)
|
41
|
+
io = StringIO.new(body)
|
42
|
+
gzip_reader = if RUBY_ENCODING
|
43
|
+
Zlib::GzipReader.new(io, :encoding => 'ASCII-8BIT')
|
44
|
+
else
|
45
|
+
Zlib::GzipReader.new(io)
|
46
|
+
end
|
47
|
+
gzip_reader.read
|
48
|
+
end
|
49
|
+
|
50
|
+
def inflate(body)
|
51
|
+
Zlib::Inflate.inflate(body)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -7,6 +7,15 @@ module FaradayMiddleware
|
|
7
7
|
# Public: Caches GET responses and pulls subsequent ones from the cache.
|
8
8
|
class Caching < Faraday::Middleware
|
9
9
|
attr_reader :cache
|
10
|
+
# Internal: List of status codes that can be cached:
|
11
|
+
# * 200 - 'OK'
|
12
|
+
# * 203 - 'Non-Authoritative Information'
|
13
|
+
# * 300 - 'Multiple Choices'
|
14
|
+
# * 301 - 'Moved Permanently'
|
15
|
+
# * 302 - 'Found'
|
16
|
+
# * 404 - 'Not Found'
|
17
|
+
# * 410 - 'Gone'
|
18
|
+
CACHEABLE_STATUS_CODES = [200, 203, 300, 301, 302, 404, 410]
|
10
19
|
|
11
20
|
extend Forwardable
|
12
21
|
def_delegators :'Faraday::Utils', :parse_query, :build_query
|
@@ -34,7 +43,14 @@ module FaradayMiddleware
|
|
34
43
|
cache_on_complete(env)
|
35
44
|
else
|
36
45
|
# synchronous mode
|
37
|
-
|
46
|
+
key = cache_key(env)
|
47
|
+
unless response = cache.read(key) and response
|
48
|
+
response = @app.call(env)
|
49
|
+
|
50
|
+
if CACHEABLE_STATUS_CODES.include?(response.status)
|
51
|
+
cache.write(key, response)
|
52
|
+
end
|
53
|
+
end
|
38
54
|
finalize_response(response, env)
|
39
55
|
end
|
40
56
|
else
|
@@ -63,7 +79,9 @@ module FaradayMiddleware
|
|
63
79
|
finalize_response(cached_response, env)
|
64
80
|
else
|
65
81
|
response = @app.call(env)
|
66
|
-
|
82
|
+
if CACHEABLE_STATUS_CODES.include?(response.status)
|
83
|
+
response.on_complete { cache.write(key, response) }
|
84
|
+
end
|
67
85
|
end
|
68
86
|
end
|
69
87
|
|
@@ -12,26 +12,24 @@ module FaradayMiddleware
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
# Public: Follow HTTP 301, 302, 303, and 307 redirects
|
16
|
-
# PUT, and DELETE requests.
|
15
|
+
# Public: Follow HTTP 301, 302, 303, and 307 redirects.
|
17
16
|
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
17
|
+
# For HTTP 301, 302, and 303, the original GET, POST, PUT, DELETE, or PATCH
|
18
|
+
# request gets converted into a GET. With `:standards_compliant => true`,
|
19
|
+
# however, the HTTP method after 301/302 remains unchanged. This allows you
|
20
|
+
# to opt into HTTP/1.1 compliance and act unlike the major web browsers.
|
22
21
|
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
# compliance enabled, a 302 will instead act in accordance with the HTTP
|
26
|
-
# specification, which will replay the original request to the received
|
27
|
-
# Location, just as with a 307.
|
22
|
+
# This middleware currently only works with synchronous requests; i.e. it
|
23
|
+
# doesn't support parallelism.
|
28
24
|
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
# original headers, and original body.
|
25
|
+
# If you wish to persist cookies across redirects, you could use
|
26
|
+
# the faraday-cookie_jar gem:
|
32
27
|
#
|
33
|
-
#
|
34
|
-
#
|
28
|
+
# Faraday.new(:url => url) do |faraday|
|
29
|
+
# faraday.use FaradayMiddleware::FollowRedirects
|
30
|
+
# faraday.use :cookie_jar
|
31
|
+
# faraday.adapter Faraday.default_adapter
|
32
|
+
# end
|
35
33
|
class FollowRedirects < Faraday::Middleware
|
36
34
|
# HTTP methods for which 30x redirects can be followed
|
37
35
|
ALLOWED_METHODS = Set.new [:head, :options, :get, :post, :put, :patch, :delete]
|
@@ -50,19 +48,16 @@ module FaradayMiddleware
|
|
50
48
|
# Public: Initialize the middleware.
|
51
49
|
#
|
52
50
|
# options - An options Hash (default: {}):
|
53
|
-
# limit
|
54
|
-
# standards_compliant - A Boolean indicating whether to respect
|
55
|
-
#
|
56
|
-
#
|
57
|
-
# cookie - Use either an array of strings
|
58
|
-
# (e.g. ['cookie1', 'cookie2']) to choose kept cookies
|
59
|
-
# or :all to keep all cookies.
|
51
|
+
# :limit - A Numeric redirect limit (default: 3)
|
52
|
+
# :standards_compliant - A Boolean indicating whether to respect
|
53
|
+
# the HTTP spec when following 301/302
|
54
|
+
# (default: false)
|
60
55
|
def initialize(app, options = {})
|
61
56
|
super(app)
|
62
57
|
@options = options
|
63
58
|
|
64
|
-
@
|
65
|
-
@
|
59
|
+
@convert_to_get = Set.new [303]
|
60
|
+
@convert_to_get << 301 << 302 unless standards_compliant?
|
66
61
|
end
|
67
62
|
|
68
63
|
def call(env)
|
@@ -71,11 +66,9 @@ module FaradayMiddleware
|
|
71
66
|
|
72
67
|
private
|
73
68
|
|
74
|
-
def
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
!@replay_request_codes.include? response.status
|
69
|
+
def convert_to_get?(response)
|
70
|
+
![:head, :options].include?(response.env[:method]) &&
|
71
|
+
@convert_to_get.include?(response.status)
|
79
72
|
end
|
80
73
|
|
81
74
|
def perform_with_redirection(env, follows)
|
@@ -94,12 +87,8 @@ module FaradayMiddleware
|
|
94
87
|
|
95
88
|
def update_env(env, request_body, response)
|
96
89
|
env[:url] += safe_escape(response['location'])
|
97
|
-
if @options[:cookies]
|
98
|
-
cookies = keep_cookies(env)
|
99
|
-
env[:request_headers][:cookies] = cookies unless cookies.nil?
|
100
|
-
end
|
101
90
|
|
102
|
-
if
|
91
|
+
if convert_to_get?(response)
|
103
92
|
env[:method] = :get
|
104
93
|
env[:body] = nil
|
105
94
|
else
|
@@ -120,25 +109,6 @@ module FaradayMiddleware
|
|
120
109
|
@options.fetch(:limit, FOLLOW_LIMIT)
|
121
110
|
end
|
122
111
|
|
123
|
-
def keep_cookies(env)
|
124
|
-
cookies = @options.fetch(:cookies, [])
|
125
|
-
response_cookies = env[:response_headers][:cookies]
|
126
|
-
cookies == :all ? response_cookies : selected_request_cookies(response_cookies)
|
127
|
-
end
|
128
|
-
|
129
|
-
def selected_request_cookies(cookies)
|
130
|
-
selected_cookies(cookies)[0...-1]
|
131
|
-
end
|
132
|
-
|
133
|
-
def selected_cookies(cookies)
|
134
|
-
"".tap do |cookie_string|
|
135
|
-
@options[:cookies].each do |cookie|
|
136
|
-
string = /#{cookie}=?[^;]*/.match(cookies)[0] + ';'
|
137
|
-
cookie_string << string
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
112
|
def standards_compliant?
|
143
113
|
@options.fetch(:standards_compliant, false)
|
144
114
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Michaels-Ober
|
@@ -61,6 +61,7 @@ files:
|
|
61
61
|
- lib/faraday_middleware.rb
|
62
62
|
- lib/faraday_middleware/addressable_patch.rb
|
63
63
|
- lib/faraday_middleware/backwards_compatibility.rb
|
64
|
+
- lib/faraday_middleware/gzip.rb
|
64
65
|
- lib/faraday_middleware/instrumentation.rb
|
65
66
|
- lib/faraday_middleware/rack_compatible.rb
|
66
67
|
- lib/faraday_middleware/request/encode_json.rb
|