-
Notifications
You must be signed in to change notification settings - Fork 96
/
request.rb
48 lines (42 loc) · 1.15 KB
/
request.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
module Faraday
class HttpCache < Faraday::Middleware
# Internal: A class to represent a request
class Request
class << self
def from_env(env)
hash = env.to_hash
new(method: hash[:method], url: hash[:url], headers: hash[:request_headers].dup)
end
end
attr_reader :method, :url, :headers
def initialize(method:, url:, headers:)
@method = method
@url = url
@headers = headers
end
# Internal: Validates if the current request method is valid for caching.
#
# Returns true if the method is ':get' or ':head'.
def cacheable?
return false if method != :get && method != :head
return false if cache_control.no_store?
true
end
def no_cache?
cache_control.no_cache?
end
# Internal: Gets the 'CacheControl' object.
def cache_control
@cache_control ||= CacheControl.new(headers['Cache-Control'])
end
def serializable_hash
{
method: @method,
url: @url.to_s,
headers: @headers
}
end
end
end
end