oldskool-gcse 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/oldskool/gcse.rb +100 -0
- data/lib/oldskool/gcse_handler.rb +32 -0
- data/lib/oldskool-gcse.rb +2 -0
- data/views/gcse.erb +59 -0
- metadata +83 -0
@@ -0,0 +1,100 @@
|
|
1
|
+
module Oldskool
|
2
|
+
class GCSE
|
3
|
+
attr_reader :querytime
|
4
|
+
|
5
|
+
include HTTParty
|
6
|
+
|
7
|
+
def initialize(apikey, cx, apiurl='https://www.googleapis.com/customsearch/v1')
|
8
|
+
@cx = cx
|
9
|
+
@apikey = apikey
|
10
|
+
@api = apiurl
|
11
|
+
@lastresult = nil
|
12
|
+
@querytime = 0
|
13
|
+
@error = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def search(q, args={})
|
17
|
+
args[:start] = (args.delete(:page) * 10 + 1) if args[:page]
|
18
|
+
|
19
|
+
start = Time.now
|
20
|
+
@lastresult = self.class.get(@api, :query => {:q => q, :key => @apikey, :cx => @cx}.merge(args))
|
21
|
+
|
22
|
+
@querytime = Time.now - start
|
23
|
+
|
24
|
+
if @lastresult["error"]
|
25
|
+
@error = @lastresult["error"]["message"]
|
26
|
+
else
|
27
|
+
@error = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
# Array of search results
|
34
|
+
def items
|
35
|
+
@lastresult["items"]
|
36
|
+
end
|
37
|
+
|
38
|
+
def total
|
39
|
+
Integer(@lastresult["queries"]["request"].first["totalResults"])
|
40
|
+
end
|
41
|
+
|
42
|
+
# Custom Search Engine title
|
43
|
+
def title
|
44
|
+
@lastresult["context"]["title"]
|
45
|
+
end
|
46
|
+
|
47
|
+
# number of total results found in the search
|
48
|
+
def total_results
|
49
|
+
@lastresults["request"]["totalResults"]
|
50
|
+
end
|
51
|
+
|
52
|
+
def has_previous?
|
53
|
+
!!@lastresult["queries"]["previousPage"]
|
54
|
+
end
|
55
|
+
|
56
|
+
def has_next?
|
57
|
+
!!@lastresult["queries"]["nextPage"]
|
58
|
+
end
|
59
|
+
|
60
|
+
def next_start
|
61
|
+
if has_next?
|
62
|
+
return @lastresult["queries"]["nextPage"].first["startIndex"]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def previous_start
|
67
|
+
if has_previous?
|
68
|
+
return @lastresult["queries"]["previousPage"].first["startIndex"]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# do search for the previous results
|
73
|
+
def previous
|
74
|
+
if (r = @lastresult["queries"]["previousPage"])
|
75
|
+
return search_by_query_hash(r.first)
|
76
|
+
else
|
77
|
+
return nil
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# do a search for the next results
|
82
|
+
def next
|
83
|
+
if (r = @lastresult["queries"]["nextPage"])
|
84
|
+
@previous = @lastresult.clone
|
85
|
+
|
86
|
+
return search_by_query_hash(r.first)
|
87
|
+
else
|
88
|
+
return nil
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def search_by_query_hash(query)
|
93
|
+
if query["startIndex"]
|
94
|
+
return search(query["searchTerms"], {:start => query["startIndex"]})
|
95
|
+
else
|
96
|
+
return search(query["searchTerms"])
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Oldskool
|
2
|
+
class GcseHandler
|
3
|
+
include Utils
|
4
|
+
|
5
|
+
def initialize(params, keyword, config)
|
6
|
+
@params = params
|
7
|
+
@keyword = keyword
|
8
|
+
@config = config
|
9
|
+
self
|
10
|
+
end
|
11
|
+
|
12
|
+
def handle_request(keyword, query)
|
13
|
+
gcse = GCSE.new(@config[:google_api_key], @keyword[:cx])
|
14
|
+
|
15
|
+
if @params["page"]
|
16
|
+
@params["page"] = Integer(@params["page"])
|
17
|
+
else
|
18
|
+
@params["page"] = 1
|
19
|
+
end
|
20
|
+
|
21
|
+
args = {:start => ((@params["page"] - 1) * 10 + 1)}
|
22
|
+
|
23
|
+
gcse.search @params[:q], args
|
24
|
+
|
25
|
+
sidemenu = [{:title => "Google", :url => "http://google.com/search?q=%s" % [ escape_html((query || keyword)) ]},
|
26
|
+
{:title => "Images", :url => "http://google.com/search?q=%s&tbm=isch" % [escape_html query]},
|
27
|
+
{:title => "Videos", :url => "http://google.com/search?q=%s&tbm=vid" % [ escape_html query ]} ]
|
28
|
+
|
29
|
+
{:template => plugin_template(:gcse, __FILE__), :gcse => gcse, :sidemenu => sidemenu}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/views/gcse.erb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
<% unless @error %>
|
2
|
+
<style type="text/css">
|
3
|
+
.gcse_results {
|
4
|
+
margin-left: auto;
|
5
|
+
margin-right: auto;
|
6
|
+
width: 100%;
|
7
|
+
background-color:#b0e0e6;
|
8
|
+
}
|
9
|
+
|
10
|
+
.gcse_stats {
|
11
|
+
font-size: 75%;
|
12
|
+
}
|
13
|
+
|
14
|
+
.gcse_result {
|
15
|
+
padding-bottom: 20px;
|
16
|
+
}
|
17
|
+
|
18
|
+
.gcse_linktext {
|
19
|
+
color: green;
|
20
|
+
font-size: 80%;
|
21
|
+
}
|
22
|
+
</style>
|
23
|
+
<% if @result[:gcse].items %>
|
24
|
+
<div class="gcse_results">
|
25
|
+
<% @result[:gcse].items.each do |item| %>
|
26
|
+
<div class="gcse_result">
|
27
|
+
<div class="gcse_title"><a href="<%= item["link"] %>"><%= item["htmlTitle"] %></a></div>
|
28
|
+
<div class="gcse_snippet"><%= item["htmlSnippet"] %></div>
|
29
|
+
<div class="gcse_linktext"><%= item["link"] %></div>
|
30
|
+
</div>
|
31
|
+
<% end %>
|
32
|
+
<div class="gcse_stats"><%= @result[:gcse].total %> items found in <%= @result[:gcse].querytime %> seconds</div>
|
33
|
+
</div>
|
34
|
+
<div class="pagination">
|
35
|
+
<ul>
|
36
|
+
<% if @result[:gcse].has_previous? %>
|
37
|
+
<li class="prev"><a href="/do?q=<%= @params["q"] %>&page=<%= @params[:page] - 1 %>">← Previous</a></li>
|
38
|
+
<% else %>
|
39
|
+
<li class="prev disabled"><a href="#">← Previous</a></li>
|
40
|
+
<% end %>
|
41
|
+
<% (1..10).each do |i| %>
|
42
|
+
<% break if (i * 10) >= @result[:gcse].total %>
|
43
|
+
<% if @params[:page] == i %>
|
44
|
+
<li class="active"><a href="/do?q=<%= @params["q"] %>&page=<%= i %>"><%= i %></a></li>
|
45
|
+
<% else %>
|
46
|
+
<li><a href="/do?q=<%= @params["q"] %>&page=<%= i %>"><%= i %></a></li>
|
47
|
+
<% end %>
|
48
|
+
<% end %>
|
49
|
+
<% if @result[:gcse].has_next? %>
|
50
|
+
<li class="next"><a href="/do?q=<%= @params["q"] %>&page=<%= @params[:page] + 1 %>">Next →</a></li>
|
51
|
+
<% else %>
|
52
|
+
<li class="next disabled"><a href="#">Next →</a></li>
|
53
|
+
<% end %>
|
54
|
+
</ul>
|
55
|
+
</div>
|
56
|
+
<% else %>
|
57
|
+
No results found, daily API limit might have been reached
|
58
|
+
<% end %>
|
59
|
+
<% end %>
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: oldskool-gcse
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- R.I.Pienaar
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-01-14 00:00:00 +00:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: httparty
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: "description: Google Custom Search plugin for Oldskool"
|
36
|
+
email: rip@devco.net
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- lib/oldskool/gcse.rb
|
45
|
+
- lib/oldskool/gcse_handler.rb
|
46
|
+
- lib/oldskool-gcse.rb
|
47
|
+
- views/gcse.erb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://devco.net/
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.3.7
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: oldskool-gcse
|
82
|
+
test_files: []
|
83
|
+
|