A wee Google CSE client. Use it to easily query your custom search engine. Supports image search.
Add this line to your application's Gemfile:
gem 'google-cse'
And then execute:
$ bundle
Or install it yourself as:
$ gem install google-cse
Set it up:
# you will need a Google API account and have CSE enabled.
require 'google_cse'
GoogleCSE::CX = '<your-cx>'
GoogleCSE::KEY = '<your-app-key>'
If you are on a rails app, you should stick the constants in an initializer
Run a search and visit the first result, 'feeling lucky' style (on a mac):
g = GoogleCSE.search('Ian Kilminster')
`open -a Safari #{g.fetch.results.first.link}`
Do the same for an image:
g = GoogleCSE.image_search('Ian Kilminster')
img = g.fetch.results.first.link
file = img.split('/').last
File.open(file,'w') {|f| f.write(open(img).read)}
`open -a Preview #{file}`
Paginated access:
g = GoogleCSE.image_search('Ian Kilminster')
g.fetch.results.first.link # fetches first page, first result
#=> http://en.wikipedia.org/wiki/Lemmy
g.total # number of total results
#=> 5400000
g.per_page # number of results per page
#=> 10
g.previous? # is there a previous page?
#=> false
g.previous # what did I just say just now?
#=> nil
g.next? # is there a next page?
#=> true
g.next.results.first.link # fetches next page, first result
#=> http://www.discogs.com/artist/Ian+Fraser+Kilmister
g.page # the current page number
#=> 2
g.current_index # the current page's starting index / or rank :)
=> 11
g.previous?
#=> true
g.previous.results.first.link # ...and back to first page
#=> http://en.wikipedia.org/wiki/Lemmy
g.page
#=> 1
You can specify any extra parameters the API allows. View a complete list here
Example:
# Only return large jpegs, I can't enjoy such high art otherwise
g = GoogleCSE.image_search('Flight of Icarus Cover', :fileType => :jpg, :imgSize => :large)
#=> http://musictrajectory.com/wp-content/uploads/2011/08/iron-maiden-flight-of-icarus-single-cover.jpg
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request