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(options) ⇒ Request

Returns a new instance of Request.



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

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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.from_env(env) ⇒ Object



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

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.



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

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)


21
22
23
24
25
# File 'lib/faraday/http_cache/request.rb', line 21

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

#no_cache?Boolean

Returns:

  • (Boolean)


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

def no_cache?
  cache_control.no_cache?
end

#serializable_hashObject



36
37
38
39
40
41
42
# File 'lib/faraday/http_cache/request.rb', line 36

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