class Mechanize::Page::Image
An image element on an HTML page
Attributes
Public Class Methods
Source
# File lib/mechanize/page/image.rb, line 15 def initialize node, page @node = node @page = page @mech = page.mech end
Creates a new Mechanize::Page::Image
from an image node
and source page
.
Public Instance Methods
Source
# File lib/mechanize/page/image.rb, line 24 def alt node['alt'] end
The alt attribute of the image
Source
# File lib/mechanize/page/image.rb, line 32 def caption title || alt || '' end
The caption of the image. In order of preference, the title
, alt
, or empty string “”.
Also aliased as: text
Source
# File lib/mechanize/page/image.rb, line 41 def dom_class node['class'] end
The class attribute of the image
Source
# File lib/mechanize/page/image.rb, line 48 def dom_id node['id'] end
The id attribute of the image
Source
# File lib/mechanize/page/image.rb, line 63 def extname return nil unless src File.extname url.path end
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 # => ""
Source
# File lib/mechanize/page/image.rb, line 83 def fetch parameters = [], referer = nil, headers = {} mech.get src, parameters, referer || image_referer, headers end
Downloads the image.
agent.page.image_with(:src => /logo/).fetch.save
The referer is:
Source
# File lib/mechanize/page/image.rb, line 90 def height node['height'] end
The height attribute of the image
Source
# File lib/mechanize/page/image.rb, line 118 def mime_type suffix_without_dot = extname ? extname.sub(/\A\./){''}.downcase : nil Mechanize::Util::DefaultMimeTypes[suffix_without_dot] end
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
Source
# File lib/mechanize/page/image.rb, line 140 def src node['src'] end
The src attribute of the image
Source
# File lib/mechanize/page/image.rb, line 147 def title node['title'] end
The title attribute of the image
Source
# File lib/mechanize/page/image.rb, line 154 def to_s url.to_s end
The URL string of this image
Source
# File lib/mechanize/page/image.rb, line 161 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
URI for this image
Also aliased as: uri
Source
# File lib/mechanize/page/image.rb, line 178 def width node['width'] end
The width attribute of the image