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.



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

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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.from_env(env) ⇒ Object



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

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.



35
36
37
# File 'lib/faraday/http_cache/request.rb', line 35

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)


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

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

#no_cache?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/faraday/http_cache/request.rb', line 30

def no_cache?
  cache_control.no_cache?
end

#serializable_hashObject



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

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