Skip to content

Instantly share code, notes, and snippets.

@alexdunae
Created February 11, 2015 18:11
Show Gist options
  • Select an option

  • Save alexdunae/a1769ade1f49551caab2 to your computer and use it in GitHub Desktop.

Select an option

Save alexdunae/a1769ade1f49551caab2 to your computer and use it in GitHub Desktop.
IMG 240: ImageFetcher
# Uses Google Image Search to get an image URL for a search term.
#
# ImageFetcher.new.fetch('dog')
# => "http://www.cdc.gov/animalimportation/images/dog2.jpg"
#
#
# The `fetch` method returns an image URL, or an empty string if no image was found.
class ImageFetcher
def fetch(term)
url = query_url(term)
Rails.logger.debug "Querying #{url}"
response = Net::HTTP.get_response(URI.parse(url))
result_from_response(response)
end
private
def query_url(term)
params = default_params.merge(q: term)
'http://ajax.googleapis.com/ajax/services/search/images?' + params.to_param
end
def result_from_response(response)
response = JSON.parse(response.body)
response['responseData']['results'].first['url']
rescue
# no result (or unknown response format), so return an empty string
''
end
def default_params
{
rsz: '8',
imgsz: 'medium',
imgtype: 'photo',
safe: 'active',
v: '1.0'
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment