gugl 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/gugl +29 -0
- data/lib/gugl.rb +9 -0
- data/lib/search/api_credentials.rb +11 -0
- data/lib/search/data_getter.rb +26 -0
- data/lib/search/displayers/displayer_mapper.rb +27 -0
- data/lib/search/displayers/mini_terminal_result_displayer.rb +17 -0
- data/lib/search/displayers/terminal_result_displayer.rb +24 -0
- data/lib/search/remote_searcher.rb +50 -0
- data/lib/search/result_item.rb +13 -0
- data/lib/search/searcher.rb +65 -0
- data/version.rb +3 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fccdc086bbec504323e41dc2f940dc9a49c7781e
|
4
|
+
data.tar.gz: 9cb928ffc3da6cf87be2f09981cd057dd67594dc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 511e8836e260f17864ca9974c2f0c3dae0d1bee3ab7cbbf7942b3c158852580a3a96811d6881a37af6805c79fe4f14fc417f67a2c29bdb7446f88e9b6239d0cb
|
7
|
+
data.tar.gz: ee3166ad00044285d8e605ab883d0e0488c4c124558b20a68e8ac84717bbcc283ef0396a293511c5e4ae9501d0999262b777a76b1bb55673e0ba59d0546ffe9d
|
data/bin/gugl
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Usage:
|
4
|
+
#
|
5
|
+
# gugl "word"
|
6
|
+
# gugl "word" -l 2
|
7
|
+
#
|
8
|
+
|
9
|
+
require_relative "../lib/gugl"
|
10
|
+
|
11
|
+
# the easy way
|
12
|
+
|
13
|
+
query = ARGV[0]
|
14
|
+
limit = ARGV[2]
|
15
|
+
|
16
|
+
searcher = Gugl.searcher.new
|
17
|
+
|
18
|
+
searcher.authorize do |auth|
|
19
|
+
auth.key = ENV["GOOGLE_CUSTOM_SEARCH_KEY"]
|
20
|
+
auth.cx = ENV["GOOGLE_CUSTOM_SEARCH_CX"]
|
21
|
+
end
|
22
|
+
|
23
|
+
if limit
|
24
|
+
searcher.limit = limit
|
25
|
+
end
|
26
|
+
|
27
|
+
searcher.query = query
|
28
|
+
|
29
|
+
searcher.search_and_display
|
data/lib/gugl.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative './remote_searcher'
|
2
|
+
require_relative './result_item'
|
3
|
+
|
4
|
+
module Gugl
|
5
|
+
module Search
|
6
|
+
class DataGetter
|
7
|
+
attr_accessor :query, :limit
|
8
|
+
|
9
|
+
def get
|
10
|
+
searcher.search(query, limit) do |items|
|
11
|
+
items.map(&method(:build_item))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def searcher
|
16
|
+
@searcher ||= RemoteSearcher.new
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def build_item(item)
|
22
|
+
ResultItem.new(item)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative './terminal_result_displayer'
|
2
|
+
require_relative './mini_terminal_result_displayer'
|
3
|
+
|
4
|
+
module Gugl
|
5
|
+
module Search
|
6
|
+
module Displayers
|
7
|
+
module DisplayerMapper
|
8
|
+
extend self
|
9
|
+
|
10
|
+
DISPLAYERS = {
|
11
|
+
term: ->{ Displayers::TerminalResultDisplayer.new },
|
12
|
+
mini_term: ->{ Displayers::MiniTerminalResultDisplayer.new },
|
13
|
+
}
|
14
|
+
|
15
|
+
DEFAULT_DISPLAYER = :term
|
16
|
+
|
17
|
+
def [](key)
|
18
|
+
if key == :default
|
19
|
+
DISPLAYERS[DEFAULT_DISPLAYER].call
|
20
|
+
else
|
21
|
+
DISPLAYERS[key].call
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'term/ansicolor'
|
2
|
+
|
3
|
+
module Gugl
|
4
|
+
module Search
|
5
|
+
module Displayers
|
6
|
+
class MiniTerminalResultDisplayer
|
7
|
+
include Term::ANSIColor
|
8
|
+
|
9
|
+
def display(result, index)
|
10
|
+
# title => link
|
11
|
+
print magenta, result.title, reset, " => "
|
12
|
+
print blue, result.link, reset, "\n"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'term/ansicolor'
|
2
|
+
|
3
|
+
module Gugl
|
4
|
+
module Search
|
5
|
+
module Displayers
|
6
|
+
class TerminalResultDisplayer
|
7
|
+
include Term::ANSIColor
|
8
|
+
|
9
|
+
ENDING_LINES = 50
|
10
|
+
|
11
|
+
def display(result, index)
|
12
|
+
# title
|
13
|
+
print magenta, index.next, ".", result.title, reset, "\n\n"
|
14
|
+
# snippet
|
15
|
+
print white, result.snippet, "\n\n", reset
|
16
|
+
# link
|
17
|
+
print white, "Link: ", blue, result.link, reset, "\n\n"
|
18
|
+
|
19
|
+
print ("-" * ENDING_LINES), "\n\n"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require_relative './api_credentials'
|
2
|
+
require 'httparty'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Gugl
|
6
|
+
module Search
|
7
|
+
class RemoteSearcher
|
8
|
+
attr_accessor :credentials
|
9
|
+
|
10
|
+
ITEMS_RESPONSE_NODE = 'items'
|
11
|
+
ERROR_RESPONSE_NODE = 'error'
|
12
|
+
|
13
|
+
def search(query, limit)
|
14
|
+
data = fetch_and_parse(query, limit)
|
15
|
+
if results = data[ITEMS_RESPONSE_NODE]
|
16
|
+
yield results
|
17
|
+
else
|
18
|
+
raise "Error: #{data[ERROR_RESPONSE_NODE]}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def authorize
|
23
|
+
self.credentials = ApiCredentials.new
|
24
|
+
yield credentials
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def fetch_and_parse(query, limit)
|
30
|
+
response = fetch(query, limit)
|
31
|
+
parse_response(response)
|
32
|
+
end
|
33
|
+
|
34
|
+
def fetch(query, limit)
|
35
|
+
HTTParty.get(base_url, query: {
|
36
|
+
num: limit,
|
37
|
+
q: query
|
38
|
+
}.merge(credentials.to_h))
|
39
|
+
end
|
40
|
+
|
41
|
+
def base_url
|
42
|
+
"https://www.googleapis.com/customsearch/v1"
|
43
|
+
end
|
44
|
+
|
45
|
+
def parse_response(response)
|
46
|
+
JSON.parse(response.body)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Gugl
|
2
|
+
module Search
|
3
|
+
class ResultItem
|
4
|
+
attr_reader :title, :snippet, :link
|
5
|
+
|
6
|
+
def initialize(plain_item)
|
7
|
+
@title = plain_item['title']
|
8
|
+
@snippet = plain_item['snippet'].gsub(/\s{2,}/, ' ')
|
9
|
+
@link = plain_item['link']
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require_relative './data_getter'
|
2
|
+
require_relative './displayers/displayer_mapper'
|
3
|
+
|
4
|
+
module Gugl
|
5
|
+
module Search
|
6
|
+
class Searcher
|
7
|
+
attr_reader :results
|
8
|
+
attr_accessor :limit, :query
|
9
|
+
|
10
|
+
DEFAULT_LIMIT = 5
|
11
|
+
|
12
|
+
def search
|
13
|
+
configure_getter
|
14
|
+
@results = getter.get
|
15
|
+
end
|
16
|
+
|
17
|
+
def display
|
18
|
+
results.each_with_index.map do |result, index|
|
19
|
+
displayer.display result, index
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def authorize(&blk)
|
24
|
+
getter.searcher.authorize(&blk)
|
25
|
+
end
|
26
|
+
|
27
|
+
def search_and_display
|
28
|
+
search
|
29
|
+
display
|
30
|
+
end
|
31
|
+
|
32
|
+
def displayer=(displayer)
|
33
|
+
@displayer = if displayer.is_a?(Symbol)
|
34
|
+
mapped_displayer(displayer)
|
35
|
+
else
|
36
|
+
displayer
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def displayer
|
41
|
+
@displayer ||= mapped_displayer(:default)
|
42
|
+
end
|
43
|
+
|
44
|
+
def limit
|
45
|
+
@limit ||= DEFAULT_LIMIT
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def mapped_displayer(key)
|
51
|
+
Displayers::DisplayerMapper[key]
|
52
|
+
end
|
53
|
+
|
54
|
+
def getter
|
55
|
+
@getter ||= DataGetter.new
|
56
|
+
end
|
57
|
+
|
58
|
+
def configure_getter
|
59
|
+
getter.query = query
|
60
|
+
getter.limit = limit
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gugl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julio García
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: httparty
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: term-ansicolor
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A wrapper to use google services programmatically, using your google
|
56
|
+
crdedentials, starting with Search.
|
57
|
+
email:
|
58
|
+
- julioggonz@gmail.com
|
59
|
+
executables:
|
60
|
+
- gugl
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- lib/gugl.rb
|
65
|
+
- lib/search/api_credentials.rb
|
66
|
+
- lib/search/data_getter.rb
|
67
|
+
- lib/search/displayers/displayer_mapper.rb
|
68
|
+
- lib/search/displayers/mini_terminal_result_displayer.rb
|
69
|
+
- lib/search/displayers/terminal_result_displayer.rb
|
70
|
+
- lib/search/remote_searcher.rb
|
71
|
+
- lib/search/result_item.rb
|
72
|
+
- lib/search/searcher.rb
|
73
|
+
- bin/gugl
|
74
|
+
- version.rb
|
75
|
+
homepage: https://github.com/juliogarciag/gugl
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.3
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: A wrapper to use google services programmatically
|
99
|
+
test_files: []
|