Class: HttpContentType::Checker
- Inherits:
-
Object
- Object
- HttpContentType::Checker
- Defined in:
- lib/http_content_type/checker.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ timeout: 5 }
Instance Attribute Summary collapse
-
#expected_content_type ⇒ String
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.
-
#last_response ⇒ Object
Returns the value of attribute last_response.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#content_type ⇒ String
Returns the ‘Content-Type` for the actually requested URL.
-
#error? ⇒ Boolean
Returns true if there was an error requesting the asset.
-
#found? ⇒ Boolean
Returns true if the asset was found (i.e. request returned an ‘Net::HTTPSuccess` response).
-
#initialize(asset_url, opts = {}) ⇒ HttpContentType::Checker
constructor
Creates a ‘HttpContentType::Checker` object given an asset URL and options.
-
#valid_content_type? ⇒ Boolean
Returns true if the asset’s ‘Content-Type` is valid according to its extension.
Constructor Details
#initialize(asset_url, opts = {}) ⇒ HttpContentType::Checker
Creates a ‘HttpContentType::Checker` object given an asset URL and options.
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) = DEFAULT_OPTIONS.merge(opts) end |
Instance Attribute Details
#expected_content_type ⇒ String
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.
80 81 82 |
# File 'lib/http_content_type/checker.rb', line 80 def expected_content_type @expected_content_type end |
#last_response ⇒ Object
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 |
#options ⇒ Object
Returns the value of attribute options.
11 12 13 |
# File 'lib/http_content_type/checker.rb', line 11 def end |
Instance Method Details
#content_type ⇒ String
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.
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.
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!
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?`!
58 59 60 |
# File 'lib/http_content_type/checker.rb', line 58 def valid_content_type? error? || !found? || content_type == expected_content_type end |