Class: Mechanize::Page::Image
- Inherits:
-
Object
- Object
- Mechanize::Page::Image
- Defined in:
- lib/mechanize/page/image.rb
Overview
An image element on an HTML page
Instance Attribute Summary collapse
-
#mech ⇒ Object
Returns the value of attribute mech.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#page ⇒ Object
Returns the value of attribute page.
Instance Method Summary collapse
-
#alt ⇒ Object
The alt attribute of the image.
-
#caption ⇒ Object
(also: #text)
The caption of the image.
-
#dom_class ⇒ Object
The class attribute of the image.
-
#dom_id ⇒ Object
The id attribute of the image.
-
#extname ⇒ Object
The suffix of the #url.
-
#fetch(parameters = [], referer = nil, headers = {}) ⇒ Object
Downloads the image.
-
#height ⇒ Object
The height attribute of the image.
-
#image_referer ⇒ Object
:nodoc:.
-
#initialize(node, page) ⇒ Image
constructor
Creates a new Mechanize::Page::Image from an image
node
and sourcepage
. -
#mime_type ⇒ Object
MIME type guessed from the image url suffix.
-
#pretty_print(q) ⇒ Object
:nodoc:.
-
#relative? ⇒ Boolean
:nodoc:.
-
#src ⇒ Object
The src attribute of the image.
-
#title ⇒ Object
The title attribute of the image.
-
#to_s ⇒ Object
The URL string of this image.
-
#url ⇒ Object
(also: #uri)
URI for this image.
-
#width ⇒ Object
The width attribute of the image.
Constructor Details
#initialize(node, page) ⇒ Image
Creates a new Mechanize::Page::Image from an image node
and source page
.
15 16 17 18 19 |
# File 'lib/mechanize/page/image.rb', line 15 def initialize node, page @node = node @page = page @mech = page.mech end |
Instance Attribute Details
#mech ⇒ Object
Returns the value of attribute mech.
9 10 11 |
# File 'lib/mechanize/page/image.rb', line 9 def mech @mech end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
7 8 9 |
# File 'lib/mechanize/page/image.rb', line 7 def node @node end |
#page ⇒ Object
Returns the value of attribute page.
8 9 10 |
# File 'lib/mechanize/page/image.rb', line 8 def page @page end |
Instance Method Details
#alt ⇒ Object
The alt attribute of the image
24 25 26 |
# File 'lib/mechanize/page/image.rb', line 24 def alt node['alt'] end |
#caption ⇒ Object Also known as: text
The caption of the image. In order of preference, the #title, #alt, or empty string “”.
32 33 34 |
# File 'lib/mechanize/page/image.rb', line 32 def title || alt || '' end |
#dom_class ⇒ Object
The class attribute of the image
41 42 43 |
# File 'lib/mechanize/page/image.rb', line 41 def dom_class node['class'] end |
#dom_id ⇒ Object
The id attribute of the image
48 49 50 |
# File 'lib/mechanize/page/image.rb', line 48 def dom_id node['id'] end |
#extname ⇒ Object
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 # => ""
63 64 65 66 67 |
# File 'lib/mechanize/page/image.rb', line 63 def extname return nil unless src File.extname url.path end |
#fetch(parameters = [], referer = nil, headers = {}) ⇒ Object
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)
83 84 85 |
# File 'lib/mechanize/page/image.rb', line 83 def fetch parameters = [], referer = nil, headers = {} mech.get src, parameters, referer || image_referer, headers end |
#height ⇒ Object
The height attribute of the image
90 91 92 |
# File 'lib/mechanize/page/image.rb', line 90 def height node['height'] end |
#image_referer ⇒ Object
:nodoc:
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mechanize/page/image.rb', line 94 def image_referer # :nodoc: http_page = page.uri && page.uri.scheme == 'http' https_page = page.uri && page.uri.scheme == 'https' case when http_page then page when https_page && relative? then page else Mechanize::File.new(nil, { 'content-type' => 'text/plain' }, '', 200) end end |
#mime_type ⇒ Object
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
118 119 120 121 122 |
# 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 |
#pretty_print(q) ⇒ Object
:nodoc:
124 125 126 127 128 129 |
# File 'lib/mechanize/page/image.rb', line 124 def pretty_print(q) # :nodoc: q.object_group(self) { q.breakable; q.pp url q.breakable; q.pp } end |
#relative? ⇒ Boolean
:nodoc:
133 134 135 |
# File 'lib/mechanize/page/image.rb', line 133 def relative? # :nodoc: %r{^https?://} !~ src end |
#src ⇒ Object
The src attribute of the image
140 141 142 |
# File 'lib/mechanize/page/image.rb', line 140 def src node['src'] end |
#title ⇒ Object
The title attribute of the image
147 148 149 |
# File 'lib/mechanize/page/image.rb', line 147 def title node['title'] end |
#to_s ⇒ Object
The URL string of this image
154 155 156 |
# File 'lib/mechanize/page/image.rb', line 154 def to_s url.to_s end |
#url ⇒ Object Also known as: uri
URI for this image
161 162 163 164 165 166 167 168 169 170 171 |
# 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 |
#width ⇒ Object
The width attribute of the image
178 179 180 |
# File 'lib/mechanize/page/image.rb', line 178 def width node['width'] end |