Class: Plumnailer::ImgParser

Inherits:
Object
  • Object
show all
Defined in:
lib/plumnailer/img_parser.rb

Overview

Fetch image data urls, load into Magick::Image and set some additional fields.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fetcher) ⇒ ImgParser

Returns a new instance of ImgParser.



11
12
13
# File 'lib/plumnailer/img_parser.rb', line 11

def initialize(fetcher)
  @fetcher = fetcher
end

Instance Attribute Details

#fetcherObject

Returns the value of attribute fetcher.



42
43
44
# File 'lib/plumnailer/img_parser.rb', line 42

def fetcher
  @fetcher
end

Instance Method Details

#parse(img_urls) ⇒ Object

Parse image data from one or more urls.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/plumnailer/img_parser.rb', line 16

def parse(img_urls)
  if img_urls.respond_to?(:inject)
    cache = {}
    img_urls.inject([]) do |memo,u|
      # nil values should be cached
      memo << (cache.include?(u) ? cache[u] : (cache[u] = parse_one(u)))
    end.compact
  else
    parse_one i
  end
end

#parse_one(img_url) ⇒ Object

Fetch image data from a url, load into Magick::ImageList and set some additional fields.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/plumnailer/img_parser.rb', line 30

def parse_one(img_url)
  begin
    img_data = fetcher.fetch(img_url)
    img = Magick::ImageList.new.from_blob(img_data).extend(
      Plumnailer::WebImage)
    img.source_url = img_url
    img
  rescue Exception
    nil
  end
end