Module: Smeargle::Image

Included in:
Sketch
Defined in:
lib/smeargle/image.rb

Instance Method Summary collapse

Instance Method Details

#corrupt?(img) ⇒ Boolean

Returns:

  • (Boolean)


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

def corrupt? img
  img =~ /\;/
end

#detailed_imagesObject



22
23
24
25
# File 'lib/smeargle/image.rb', line 22

def detailed_images
  @detailed_images ||=
    formatted_images.map { |i| image_details i }
end

#filtered_imagesObject



41
42
43
44
45
46
# File 'lib/smeargle/image.rb', line 41

def filtered_images
  images = detailed_images
  images.reject! { |x| x[:width] < min_width } if min_width
  images.reject! { |x| x[:height] < min_height } if min_height
  images
end

#format_image_url(img) ⇒ Object



27
28
29
# File 'lib/smeargle/image.rb', line 27

def format_image_url img
  URI(img).relative? ? "#{clean_url}#{img}" : img
end

#formatted_imagesObject Also known as: simple_images



16
17
18
19
20
# File 'lib/smeargle/image.rb', line 16

def formatted_images
  @formatted_images ||=
    image_collection.reject{ |i| corrupt? i }.
      map { |i| format_image_url i }
end

#image_collectionObject



8
9
10
11
12
13
14
# File 'lib/smeargle/image.rb', line 8

def image_collection
  images = []
  response_body.css('img').each do |img|
    images << img['src']
  end
  images.uniq
end

#image_details(img) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/smeargle/image.rb', line 31

def image_details img
  begin
    remote_image = open img
    image = Magick::Image::from_blob(remote_image.read).first
    { url: img, height: image.columns, width: image.rows }
  rescue
    nil
  end
end

#imagesObject



4
5
6
# File 'lib/smeargle/image.rb', line 4

def images
  @images ||= filtered_images
end