Module: ImageUrlLoader
- Defined in:
- lib/asker/loader/image_url_loader.rb
Overview
Search URL images on Internet Methods:
- 
load- Accept String or an Array with the desired search
- 
sanitize_string- Clean URL string
- 
sanitize_array- Clean URL Array
Class Method Summary collapse
- 
  
    
      .load(input = [])  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Search “input” images on Google and return URL. 
- .sanitize_array(input) ⇒ Object
- .sanitize_string(input) ⇒ Object
Class Method Details
.load(input = []) ⇒ Object
Search “input” images on Google and return URL
| 12 13 14 15 16 17 18 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 | # File 'lib/asker/loader/image_url_loader.rb', line 12 def self.load(input = []) filters = [] if input.instance_of? String filters += sanitize_string(input.clone) elsif input.instance_of? Array filters = sanitize_array(input.clone) else Logger.error "ImageUrlLoader: Unknown type (#{input.class})" exit 1 end # Search Image URLs from Google site, selected by <filters> search_url = "https://www.google.es/search?q=" search_url << filters.flatten.join("+").to_s search_url << "&source=lnms&tbm=isch&sa=X&ved=2ahUKEwj_g8Wfst7nAhWpzoUKHf_wDbsQ_AUoAnoECBMQBA&biw=1280&bih=591" image_urls = [] begin uri = URI.parse(search_url) response = Net::HTTP.get_response(uri) r = response.body.split(" ") r.each do |line| if line.include? 'src="https' image_urls << line.delete('"')[4, line.size] end end rescue Logger.warn "ImageUrlLoader: Problems with URL (#{search_url})" Logger.warn " (a) Check Internet connections" Logger.warn " (b) Ensure URL is well formed" end image_urls end | 
.sanitize_array(input) ⇒ Object
| 57 58 59 | # File 'lib/asker/loader/image_url_loader.rb', line 57 def self.sanitize_array(input) input.map { |i| sanitize_string(i) } end | 
.sanitize_string(input) ⇒ Object
| 45 46 47 48 49 50 51 52 53 54 55 | # File 'lib/asker/loader/image_url_loader.rb', line 45 def self.sanitize_string(input) text = input.dup r = [ %w[á a], %w[é e], %w[í i], %w[ó o], %w[ú u], %w[ñ n], %w[Á A], %w[É E], %w[Í I], %w[Ó O], %w[Ú U], %w[Ñ N] ] r.each { |item| text.gsub!(item[0], item[1]) } r = %w[- _ , "] r.each { |item| text.gsub!(item, " ") } text.split(" ") end |