lib/link_thumbnailer.rb in link_thumbnailer-1.0.0 vs lib/link_thumbnailer.rb in link_thumbnailer-1.0.2
- old
+ new
@@ -42,38 +42,52 @@
def configure
yield config
end
def generate(url, options = {})
+ set_options(options)
+ instantiate_classes
+
+ doc = self.doc_parser.parse(self.fetcher.fetch(url), url)
+
+ self.object[:url] = doc.source_url
+ opengraph(doc) || custom(doc)
+ end
+
+ private
+
+ def set_options(options)
LinkThumbnailer.configure {|config|
- config.top = options[:top].to_i if options[:top]
- config.limit = options[:limit].to_i if options[:limit]
+ config.mandatory_attributes = options[:mandatory_attributes] if options[:mandatory_attributes]
+ config.strict = options[:strict] if options[:strict]
+ config.redirect_limit = options[:redirect_limit].to_i if options[:redirect_limit]
+ config.blacklist_urls = options[:blacklist_urls] if options[:blacklist_urls]
+ config.rmagick_attributes = options[:rmagick_attributes] if options[:rmagick_attributes]
+ config.limit = options[:limit].to_i if options[:limit]
+ config.top = options[:top].to_i if options[:top]
}
+ end
+ def instantiate_classes
self.object = LinkThumbnailer::Object.new
self.fetcher = LinkThumbnailer::Fetcher.new
self.doc_parser = LinkThumbnailer::DocParser.new
self.img_url_filters = [LinkThumbnailer::ImgUrlFilter.new]
self.img_parser = LinkThumbnailer::ImgParser.new(self.fetcher, self.img_url_filters)
+ end
- doc_string = self.fetcher.fetch(url)
- doc = self.doc_parser.parse(doc_string, url)
-
- self.object[:url] = doc.source_url
-
- # Try Opengraph first
+ def opengraph(doc)
self.object = LinkThumbnailer::Opengraph.parse(self.object, doc)
-
return self.object if self.object.valid?
+ nil
+ end
- # Else try manually
-
+ def custom(doc)
self.object[:title] = doc.title
self.object[:description] = doc.description
self.object[:images] = self.img_parser.parse(doc.img_abs_urls.dup)
-
- return nil unless self.object.valid?
- self.object
+ return self.object if self.object.valid?
+ nil
end
end
end