Class: Fetch::UrlFetcher
- Inherits:
-
Object
- Object
- Fetch::UrlFetcher
- Defined in:
- lib/url-fetcher.rb
Instance Attribute Summary collapse
-
#image_urls ⇒ Object
readonly
Returns the value of attribute image_urls.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#url ⇒ Object
Returns the value of attribute url.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#find ⇒ Object
def custom_error raise(Fetch::MyCustomException, ‘Your custom error message here’) end.
-
#initialize(options = {}) ⇒ UrlFetcher
constructor
A new instance of UrlFetcher.
Constructor Details
#initialize(options = {}) ⇒ UrlFetcher
Returns a new instance of UrlFetcher.
10 11 12 13 |
# File 'lib/url-fetcher.rb', line 10 def initialize(={}) @url = get_url_params([:url]).scheme.nil? ? "http://#{[:url]}" : [:url] @width = [:width].nil? ? 100 : [:width] end |
Instance Attribute Details
#image_urls ⇒ Object (readonly)
Returns the value of attribute image_urls.
8 9 10 |
# File 'lib/url-fetcher.rb', line 8 def image_urls @image_urls end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
8 9 10 |
# File 'lib/url-fetcher.rb', line 8 def title @title end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/url-fetcher.rb', line 7 def url @url end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
8 9 10 |
# File 'lib/url-fetcher.rb', line 8 def width @width end |
Instance Method Details
#find ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/url-fetcher.rb', line 19 def find agent = Mechanize.new doc = agent.get(url) images = doc.parser.xpath("//img/@src | //a/img/@src").map {|a| unless get_url_params(a.value).nil? image_uri = a.value #if fetch uri is relative, we just add host name if (image_uri) image_uri = url+image_uri end #check this uri has file extension unless has_extention?(image_uri).empty? image_sizes = calculate_img_size(image_uri) if image_sizes (image_sizes[0].to_i > width) ? image_uri : nil else return nil end end end } @image_urls = images.compact @title = doc.title end |