Class: Mechanize::Page::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/mechanize/page/image.rb

Overview

An image element on an HTML page

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#mechObject

Returns the value of attribute mech.



9
10
11
# File 'lib/mechanize/page/image.rb', line 9

def mech
  @mech
end

#nodeObject (readonly)

Returns the value of attribute node.



7
8
9
# File 'lib/mechanize/page/image.rb', line 7

def node
  @node
end

#pageObject

Returns the value of attribute page.



8
9
10
# File 'lib/mechanize/page/image.rb', line 8

def page
  @page
end

Instance Method Details

#altObject

The alt attribute of the image



24
25
26
# File 'lib/mechanize/page/image.rb', line 24

def alt
  node['alt']
end

#captionObject 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 caption
  title || alt || ''
end

#dom_classObject

The class attribute of the image



41
42
43
# File 'lib/mechanize/page/image.rb', line 41

def dom_class
  node['class']
end

#dom_idObject

The id attribute of the image



48
49
50
# File 'lib/mechanize/page/image.rb', line 48

def dom_id
  node['id']
end

#extnameObject

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

#heightObject

The height attribute of the image



90
91
92
# File 'lib/mechanize/page/image.rb', line 90

def height
  node['height']
end

#image_refererObject

: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_typeObject

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 caption
  }
end

#relative?Boolean

:nodoc:

Returns:

  • (Boolean)


133
134
135
# File 'lib/mechanize/page/image.rb', line 133

def relative? # :nodoc:
  %r{^https?://} !~ src
end

#srcObject

The src attribute of the image



140
141
142
# File 'lib/mechanize/page/image.rb', line 140

def src
  node['src']
end

#titleObject

The title attribute of the image



147
148
149
# File 'lib/mechanize/page/image.rb', line 147

def title
  node['title']
end

#to_sObject

The URL string of this image



154
155
156
# File 'lib/mechanize/page/image.rb', line 154

def to_s
  url.to_s
end

#urlObject 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

#widthObject

The width attribute of the image



178
179
180
# File 'lib/mechanize/page/image.rb', line 178

def width
  node['width']
end