module Spidr::Headers

Constants

Reserved names used within Cookie strings

Public Instance Methods

atom?() click to toggle source

Determines if the page is an Atom feed.

@return [Boolean]

Specifies whether the page is an Atom feed.
# File lib/spidr/headers.rb, line 284
def atom?
  is_content_type?('application/atom+xml')
end
bad_request?() click to toggle source

Determines if the response code is `400`.

@return [Boolean]

Specifies whether the response code is `400`.
# File lib/spidr/headers.rb, line 46
def bad_request?
  code == 400
end
code() click to toggle source

The response code from the page.

@return [Integer]

Response code from the page.
# File lib/spidr/headers.rb, line 14
def code
  response.code.to_i
end
content_charset() click to toggle source

The charset included in the Content-Type.

@return [String, nil]

The charset of the content.

@since 0.4.0

# File lib/spidr/headers.rb, line 126
def content_charset
  content_types.each do |value|
    if value.include?(';')
      value.split(';').each do |param|
        param.strip!

        if param.start_with?('charset=')
          return param.split('=',2).last
        end
      end
    end
  end

  return nil
end
content_type() click to toggle source

The Content-Type of the page.

@return [String]

The Content-Type of the page.
# File lib/spidr/headers.rb, line 102
def content_type
  (response['Content-Type'] || '')
end
content_types() click to toggle source

The content types of the page.

@return [Array<String>]

The values within the Content-Type header.

@since 0.2.2

# File lib/spidr/headers.rb, line 114
def content_types
  (headers['content-type'] || [])
end
cookies() click to toggle source

The Cookie values sent along with the page.

@return [Array<String>]

The Cookies from the response.

@since 0.2.2

# File lib/spidr/headers.rb, line 340
def cookies
  (headers['set-cookie'] || [])
end
css?() click to toggle source

Determines if the page is a CSS stylesheet.

@return [Boolean]

Specifies whether the page is a CSS stylesheet.
# File lib/spidr/headers.rb, line 263
def css?
  is_content_type?('text/css')
end
directory?() click to toggle source

Determines if the page is a Directory Listing.

@return [Boolean]

Specifies whether the page is a Directory Listing.

@since 0.3.0

# File lib/spidr/headers.rb, line 199
def directory?
  is_content_type?('text/directory')
end
forbidden?()
Alias for: is_forbidden?
had_internal_server_error?() click to toggle source

Determines if the response code is `500`.

@return [Boolean]

Specifies whether the response code is `500`.
# File lib/spidr/headers.rb, line 92
def had_internal_server_error?
  code == 500
end
html?() click to toggle source

Determines if the page is HTML document.

@return [Boolean]

Specifies whether the page is HTML document.
# File lib/spidr/headers.rb, line 209
def html?
  is_content_type?('text/html')
end
is_content_type?(type) click to toggle source

Determines if any of the content-types of the page include a given type.

@param [String] type

The content-type to test for.

@return [Boolean]

Specifies whether the page includes the given content-type.

@example Match the Content-Type

page.is_content_type?('application/json')

@example Match the sub-type of the Content-Type

page.is_content_type?('json')

@since 0.4.0

# File lib/spidr/headers.rb, line 160
def is_content_type?(type)
  if type.include?('/')
    # otherwise only match the first param
    content_types.any? do |value|
      value = value.split(';',2).first

      value == type
    end
  else
    # otherwise only match the sub-type
    content_types.any? do |value|
      value = value.split(';',2).first
      value = value.split('/',2).last

      value == type
    end
  end
end
is_forbidden?() click to toggle source

Determines if the response code is `403`.

@return [Boolean]

Specifies whether the response code is `403`.
# File lib/spidr/headers.rb, line 68
def is_forbidden?
  code == 403
end
Also aliased as: forbidden?
is_missing?() click to toggle source

Determines if the response code is `404`.

@return [Boolean]

Specifies whether the response code is `404`.
# File lib/spidr/headers.rb, line 80
def is_missing?
  code == 404
end
Also aliased as: missing?
is_ok?() click to toggle source

Determines if the response code is `200`.

@return [Boolean]

Specifies whether the response code is `200`.
# File lib/spidr/headers.rb, line 24
def is_ok?
  code == 200
end
Also aliased as: ok?
is_unauthorized?() click to toggle source

Determines if the response code is `401`.

@return [Boolean]

Specifies whether the response code is `401`.
# File lib/spidr/headers.rb, line 56
def is_unauthorized?
  code == 401
end
Also aliased as: unauthorized?
javascript?() click to toggle source

Determines if the page is JavaScript.

@return [Boolean]

Specifies whether the page is JavaScript.
# File lib/spidr/headers.rb, line 240
def javascript?
  is_content_type?('text/javascript') || \
    is_content_type?('application/javascript')
end
json?() click to toggle source

Determines if the page is JSON.

@return [Boolean]

Specifies whether the page is JSON.

@since 0.3.0

# File lib/spidr/headers.rb, line 253
def json?
  is_content_type?('application/json')
end
missing?()
Alias for: is_missing?
ms_word?() click to toggle source

Determines if the page is a MS Word document.

@return [Boolean]

Specifies whether the page is a MS Word document.
# File lib/spidr/headers.rb, line 294
def ms_word?
  is_content_type?('application/msword')
end
ok?()
Alias for: is_ok?
pdf?() click to toggle source

Determines if the page is a PDF document.

@return [Boolean]

Specifies whether the page is a PDF document.
# File lib/spidr/headers.rb, line 304
def pdf?
  is_content_type?('application/pdf')
end
plain_text?() click to toggle source

Determines if the page is plain-text.

@return [Boolean]

Specifies whether the page is plain-text.
# File lib/spidr/headers.rb, line 185
def plain_text?
  is_content_type?('text/plain')
end
Also aliased as: txt?
rss?() click to toggle source

Determines if the page is a RSS feed.

@return [Boolean]

Specifies whether the page is a RSS feed.
# File lib/spidr/headers.rb, line 273
def rss?
  is_content_type?('application/rss+xml') || \
    is_content_type?('application/rdf+xml')
end
timedout?() click to toggle source

Determines if the response code is `308`.

@return [Boolean]

Specifies whether the response code is `308`.
# File lib/spidr/headers.rb, line 36
def timedout?
  code == 308
end
txt?()
Alias for: plain_text?
unauthorized?()
Alias for: is_unauthorized?
xml?() click to toggle source

Determines if the page is XML document.

@return [Boolean]

Specifies whether the page is XML document.
# File lib/spidr/headers.rb, line 219
def xml?
  is_content_type?('text/xml') || \
    is_content_type?('application/xml')
end
xsl?() click to toggle source

Determines if the page is XML Stylesheet (XSL).

@return [Boolean]

Specifies whether the page is XML Stylesheet (XSL).
# File lib/spidr/headers.rb, line 230
def xsl?
  is_content_type?('text/xsl')
end
zip?() click to toggle source

Determines if the page is a ZIP archive.

@return [Boolean]

Specifies whether the page is a ZIP archive.
# File lib/spidr/headers.rb, line 314
def zip?
  is_content_type?('application/zip')
end