Last active
June 5, 2017 21:27
-
-
Save MaxPleaner/d01b9231c3ef72eaede416feeadd8167 to your computer and use it in GitHub Desktop.
google search with ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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