Class: GoogleImageFetcher::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/google_image_fetcher/fetcher.rb

Class Method Summary collapse

Class Method Details

.search_url(query) ⇒ Object



4
5
6
7
8
# File 'lib/google_image_fetcher/fetcher.rb', line 4

def search_url(query)
  api_key = ENV["GOOGLE_API_KEY"]
  engine_id = ENV["SEARCH_ENGINE_ID"]
  url = "https://www.googleapis.com/customsearch/v1?key=#{api_key}&cx=#{engine_id}&q=#{query}&searchType=image"
end

.store(dir_path, image_url) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/google_image_fetcher/fetcher.rb', line 10

def store(dir_path, image_url)
  FileUtils.mkdir_p(dir_path)

  image_url.each do |url|
    filename = "#{dir_path}/#{File.basename(url)}"
    open(filename, 'wb') do |file|
      open(url) do |data|
        file.write(data.read)
      end
    end
  end
end