Class: Faraday::HttpCache::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday/http_cache/request.rb

Overview

Internal: A class to represent a request

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method:, url:, headers:) ⇒ Request

Returns a new instance of Request.



16
17
18
19
20
# File 'lib/faraday/http_cache/request.rb', line 16

def initialize(method:, url:, headers:)
  @method = method
  @url = url
  @headers = headers
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



14
15
16
# File 'lib/faraday/http_cache/request.rb', line 14

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



14
15
16
# File 'lib/faraday/http_cache/request.rb', line 14

def method
  @method
end

#urlObject (readonly)

Returns the value of attribute url.



14
15
16
# File 'lib/faraday/http_cache/request.rb', line 14

def url
  @url
end

Class Method Details

.from_env(env) ⇒ Object



8
9
10
11
# File 'lib/faraday/http_cache/request.rb', line 8

def from_env(env)
  hash = env.to_hash
  new(method: hash[:method], url: hash[:url], headers: hash[:request_headers].dup)
end

Instance Method Details

#cache_controlObject

Internal: Gets the ‘CacheControl’ object.



37
38
39
# File 'lib/faraday/http_cache/request.rb', line 37

def cache_control
  @cache_control ||= CacheControl.new(headers['Cache-Control'])
end

#cacheable?Boolean

Internal: Validates if the current request method is valid for caching.

Returns true if the method is ‘:get’ or ‘:head’.

Returns:

  • (Boolean)


25
26
27
28
29
30
# File 'lib/faraday/http_cache/request.rb', line 25

def cacheable?
  return false if method != :get && method != :head
  return false if cache_control.no_store?

  true
end

#no_cache?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/faraday/http_cache/request.rb', line 32

def no_cache?
  cache_control.no_cache?
end

#serializable_hashObject



41
42
43
44
45
46
47
# File 'lib/faraday/http_cache/request.rb', line 41

def serializable_hash
  {
    method: @method,
    url: @url.to_s,
    headers: @headers
  }
end