-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-search.rb
executable file
·49 lines (44 loc) · 1.03 KB
/
google-search.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env ruby
#
# Returns a Google search for the input term
# Reacts to inputs as `g:<search terms>`
#
# Config usage:
# => exec=ruby
# => params=path/to/google-search.rb
# => name="Google Search"
#
require 'json'
require 'uri'
class GoogleSearchBackend
def run
$stderr.print '> '
while input = $stdin.gets
input = input.chomp.downcase if input
result = if /^g:(.+)$/ =~ input
search = $1.strip
[{
:name => 'Google',
:description => "Search for `#{search}'",
:exec => "xdg-open https://www.google.com/search?q=#{URI::escape search}",
:icon => File.join(File.expand_path(File.dirname(__FILE__)), 'icons/google-search.png')
}]
else
[]
end
puts serialize(result)
STDOUT.flush
$stderr.print '> '
end
end
private
def serialize(data)
{
:backend => 'application',
:version => '1.0.0',
:priority => 9,
:results => data
}.to_json
end
end
GoogleSearchBackend.new.run if __FILE__ == $0