Class: HttpContentType::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/http_content_type/checker.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  timeout: 5
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asset_url, opts = {}) ⇒ HttpContentType::Checker

Creates a ‘HttpContentType::Checker` object given an asset URL and options.

Parameters:

  • the asset URL

  • (defaults to: {})

    misc options

Options Hash (opts):

  • :timeout (String)

    The read timeout for the ‘HEAD` request.

  • :expected_content_type (String)

    The expected ‘Content-Type` for the given `asset_url`.



22
23
24
25
26
# File 'lib/http_content_type/checker.rb', line 22

def initialize(asset_url, opts = {})
  @asset_url = asset_url
  @expected_content_type = opts.delete(:expected_content_type)
  @options = DEFAULT_OPTIONS.merge(opts)
end

Instance Attribute Details

#expected_content_typeString

Returns the expected ‘Content-Type` for the actually requested URL, based on its extension or the `:expected_content_type` option passed when instantiating the `HttpContentType::Checker` object.

Returns:

  • the expected ‘Content-Type` for the actual asset URL.

See Also:



80
81
82
# File 'lib/http_content_type/checker.rb', line 80

def expected_content_type
  @expected_content_type
end

#last_responseObject

Returns the value of attribute last_response.



11
12
13
# File 'lib/http_content_type/checker.rb', line 11

def last_response
  @last_response
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/http_content_type/checker.rb', line 11

def options
  @options
end

Instance Method Details

#content_typeString

Returns the ‘Content-Type` for the actually requested URL.

Note: If the original URL included query parameters or redirected on another URL, the ‘Content-Type` is the one for the actually requested URL without the query parameters.

Returns:

  • the ‘Content-Type` for the actual asset URL.

See Also:



70
71
72
# File 'lib/http_content_type/checker.rb', line 70

def content_type
  _head[:content_type]
end

#error?Boolean

Returns true if there was an error requesting the asset. Most common errors are ‘HTTPClientError`, `HTTPServerError`, `HTTPUnknownResponse`. Note that any other (less common) exceptions are catched as well.

Returns:

  • whether or not there was an error while requesting the asset.



34
35
36
# File 'lib/http_content_type/checker.rb', line 34

def error?
  !_head[:error].nil?
end

#found?Boolean

Returns true if the asset was found (i.e. request returned an ‘Net::HTTPSuccess` response).

Note: You should always check for ‘#error?` before checking for `#found?` to be sure you don’t assume an asset doesn’t exist when the request actually errored!

Returns:

  • whether or not the asset exists



46
47
48
# File 'lib/http_content_type/checker.rb', line 46

def found?
  _head[:found]
end

#valid_content_type?Boolean

Returns true if the asset’s ‘Content-Type` is valid according to its extension.

Note: This always returns true if ‘#error?` or `#found?` return true so be sure to check for `#found?` before checking for `#valid_content_type?`!

Returns:

  • whether or not the asset exists



58
59
60
# File 'lib/http_content_type/checker.rb', line 58

def valid_content_type?
  error? || !found? || content_type == expected_content_type
end