Skip to content

Instantly share code, notes, and snippets.

@MaxPleaner
Last active June 5, 2017 21:27
Show Gist options
  • Save MaxPleaner/d01b9231c3ef72eaede416feeadd8167 to your computer and use it in GitHub Desktop.
Save MaxPleaner/d01b9231c3ef72eaede416feeadd8167 to your computer and use it in GitHub Desktop.
google search with ruby
require 'json'
require 'active_support/all'
class GoogleSearch
attr_reader :results
def initialize(search_term)
@results = JSON.parse `googler #{search_term} --json`
end
end
class GoogleHit
attr_accessor :abstract, :title, :url
def initialize(hit)
@abstract, @title, @url = hit.with_indifferent_access.values_at(
:abstract, :title, :url
)
end
def attributes
{
abstract: abstract,
title: title,
url: url,
}
end
end
# Usage:
results = GoogleSearch.new("potato").results.map &GoogleHit.method(:new)
puts results[0].title # => "Potato - Wikipedia"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment