Class: Mechanize::Page::Image

### Home Classes Methods

### In Files

- lib/mechanize/page/image.rb

### Parent

Object

### Methods

- ::new
- #alt
- #caption
- #dom_class
- #dom_id
- #extname
- #fetch
- #height
- #mime_type
- #src
- #text
- #title
- #to_s
- #url
- #width

### Files

- EXAMPLES.rdoc
- GUIDE.rdoc
- LICENSE.rdoc
- Manifest.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Mechanize
- Mechanize::ChunkedTerminationError
- Mechanize::ContentTypeError
- Mechanize::Cookie
- Mechanize::CookieCMethods
- Mechanize::CookieDeprecated
- Mechanize::CookieIMethods
- Mechanize::CookieJar
- Mechanize::CookieJarIMethods
- Mechanize::DirectorySaver
- Mechanize::Download
- Mechanize::ElementMatcher
- Mechanize::ElementNotFoundError
- Mechanize::Error
- Mechanize::File
- Mechanize::FileConnection
- Mechanize::FileRequest
- Mechanize::FileResponse
- Mechanize::FileSaver
- Mechanize::Form
- Mechanize::Form::Button
- Mechanize::Form::CheckBox
- Mechanize::Form::Field
- Mechanize::Form::FileUpload
- Mechanize::Form::Hidden
- Mechanize::Form::ImageButton
- Mechanize::Form::Keygen
- Mechanize::Form::MultiSelectList
- Mechanize::Form::Option
- Mechanize::Form::RadioButton
- Mechanize::Form::Reset
- Mechanize::Form::SelectList
- Mechanize::Form::Submit
- Mechanize::Form::Text
- Mechanize::Form::Textarea
- Mechanize::HTTP
- Mechanize::HTTP::Agent
- Mechanize::HTTP::AuthChallenge
- Mechanize::HTTP::AuthRealm
- Mechanize::HTTP::AuthStore
- Mechanize::HTTP::ContentDispositionParser
- Mechanize::HTTP::WWWAuthenticateParser
- Mechanize::Headers
- Mechanize::History
- Mechanize::Image
- Mechanize::Page
- Mechanize::Page::Base
- Mechanize::Page::Frame
- Mechanize::Page::Image
- Mechanize::Page::Label
- Mechanize::Page::Link
- Mechanize::Page::MetaRefresh
- Mechanize::Parser
- Mechanize::PluggableParser
- Mechanize::Prependable
- Mechanize::RedirectLimitReachedError
- Mechanize::RedirectNotGetOrHeadError
- Mechanize::ResponseCodeError
- Mechanize::ResponseReadError
- Mechanize::RobotsDisallowedError
- Mechanize::TestCase
- Mechanize::UnauthorizedError
- Mechanize::UnsupportedSchemeError
- Mechanize::Util
- Mechanize::XmlFile
- HTTP
- HTTP::CookieJar

No matching classes.

# Mechanize::Page::Image

An image element on an HTML page

### Attributes

mech[RW]
node[R]
page[RW]

### Public Class Methods

new(node, page) click to toggle source

Creates a new Mechanize::Page::Image from an image `node` and source `page`.

    # File lib/mechanize/page/image.rb, line 14
    def initialize node, page
      @node = node
      @page = page
      @mech = page.mech
    end

### Public Instance Methods

alt() click to toggle source

The alt attribute of the image

    # File lib/mechanize/page/image.rb, line 23
    def alt
      node['alt']
    end

caption() click to toggle source

The caption of the image. In order of preference, the title, alt, or empty string "".

    # File lib/mechanize/page/image.rb, line 31
    def caption
      title || alt || ''
    end

Also aliased as: text
dom_class() click to toggle source

The class attribute of the image

    # File lib/mechanize/page/image.rb, line 40
    def dom_class
      node['class']
    end

dom_id() click to toggle source

The id attribute of the image

    # File lib/mechanize/page/image.rb, line 47
    def dom_id
      node['id']
    end

extname() click to toggle source

The suffix of the url. The dot is a part of suffix, not a delimiter.

    p image.url     # => "http://example/test.jpg"
    p image.extname # => ".jpg"

Returns an empty string if url has no suffix:

    p image.url     # => "http://example/sampleimage"
    p image.extname # => ""

    # File lib/mechanize/page/image.rb, line 62
    def extname
      return nil unless src

      File.extname url.path
    end

fetch(parameters = [], referer = nil, headers = {}) click to toggle source

Downloads the image.

    agent.page.image_with(:src => /logo/).fetch.save

The referer is:

page("parent") | all images on http html, relative src images on https html
(no referer) | absolute src images on https html
user specified | img.fetch(nil, my_referer_uri_or_page)

    # File lib/mechanize/page/image.rb, line 82
    def fetch parameters = [], referer = nil, headers = {}
      mech.get src, parameters, referer || image_referer, headers
    end

height() click to toggle source

The height attribute of the image

    # File lib/mechanize/page/image.rb, line 89
    def height
      node['height']
    end

mime_type() click to toggle source

MIME type guessed from the image url suffix

    p image.extname   # => ".jpg"
    p image.mime_type # => "image/jpeg"
    page.images_with(:mime_type => /gif|jpeg|png/).each do ...

Returns nil if url has no (well-known) suffix:

    p image.url       # => "http://example/sampleimage"
    p image.mime_type # => nil

    # File lib/mechanize/page/image.rb, line 117
    def mime_type
      suffix_without_dot = extname ? extname.sub(/\A\./){''}.downcase : nil

      Mechanize::Util::DefaultMimeTypes[suffix_without_dot]
    end

src() click to toggle source

The src attribute of the image

    # File lib/mechanize/page/image.rb, line 139
    def src
      node['src']
    end

text() click to toggle source
Alias for: caption
title() click to toggle source

The title attribute of the image

    # File lib/mechanize/page/image.rb, line 146
    def title
      node['title']
    end

to_s() click to toggle source

The URL string of this image

    # File lib/mechanize/page/image.rb, line 153
    def to_s
      url.to_s
    end

url() click to toggle source

URI for this image

    # File lib/mechanize/page/image.rb, line 160
    def url
      if relative? then
        if page.bases[0] then
          page.bases[0].href + src.to_s
        else
          page.uri + Mechanize::Util.uri_escape(src.to_s)
        end
      else
        URI Mechanize::Util.uri_escape(src)
      end
    end

width() click to toggle source

The width attribute of the image

    # File lib/mechanize/page/image.rb, line 175
    def width
      node['width']
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.