simple_google_custom_search 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +1 -0
- data/lib/simple_google_custom_search.rb +54 -0
- data/lib/simple_google_custom_search/result.rb +9 -0
- data/lib/simple_google_custom_search/result_set.rb +15 -0
- data/lib/simple_google_custom_search/sgcs_config.rb +17 -0
- data/lib/simple_google_custom_search/version.rb +3 -0
- data/simple_google_custom_search.gemspec +20 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Cher Wei Soh
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# SimpleGoogleCustomSearch
|
2
|
+
|
3
|
+
With Simple Google Custom Search, help people search what they need in your website just like in official google site.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simple_google_custom_search'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simple_google_custom_search
|
18
|
+
|
19
|
+
## Configuration
|
20
|
+
|
21
|
+
You need to define a <tt>constant variable</tt> for the API to search the matching words to your domain:
|
22
|
+
|
23
|
+
SGCS_CONFIG = {
|
24
|
+
domain: 'YOUR_SITE_URL'
|
25
|
+
}
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
To perform a search:
|
30
|
+
|
31
|
+
results = SimpleGoogleCustomSearch.search("simple", offset +optional+)
|
32
|
+
|
33
|
+
The +results+ variable is now a SimpleGoogleCustomSearch::ResultSet object:
|
34
|
+
|
35
|
+
results.total # number of results (integer)
|
36
|
+
results.item # array of result objects (SimpleGoogleCustomSearch::Result)
|
37
|
+
|
38
|
+
Iterate through the results:
|
39
|
+
|
40
|
+
results.item.each do |result|
|
41
|
+
result.title # result title
|
42
|
+
result.link # result URL
|
43
|
+
result.description # excerpt, with terms highlighted
|
44
|
+
end
|
45
|
+
|
46
|
+
## TODO
|
47
|
+
|
48
|
+
1. Unit Test
|
49
|
+
2. Add Pagination
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "simple_google_custom_search/version"
|
2
|
+
require 'timeout'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'cgi'
|
5
|
+
require 'hpricot'
|
6
|
+
|
7
|
+
module SimpleGoogleCustomSearch
|
8
|
+
autoload :ResultSet, 'simple_google_custom_search/result_set'
|
9
|
+
autoload :Result, 'simple_google_custom_search/result'
|
10
|
+
autoload :SgcsConfig, 'simple_google_custom_search/sgcs_config'
|
11
|
+
extend self
|
12
|
+
|
13
|
+
def search(query, offset=0)
|
14
|
+
config = SimpleGoogleCustomSearch::SgcsConfig.new
|
15
|
+
site = config.domain + ' -"tagged with" -"posts by date" -page'
|
16
|
+
uri = url(site, CGI.escape(query), offset)
|
17
|
+
return nil unless resp = fetch(uri)
|
18
|
+
|
19
|
+
if resp.status.first == '200'
|
20
|
+
parse(resp)
|
21
|
+
else
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def url(site, query, offset)
|
28
|
+
"http://www.google.com/search?q=#{URI.escape('site:'+site+' '+query+'&start='+offset.to_s)}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def fetch(uri)
|
32
|
+
begin
|
33
|
+
resp = nil
|
34
|
+
Timeout::timeout(3) do
|
35
|
+
resp = open(uri)
|
36
|
+
end
|
37
|
+
rescue Timeout::Error; end
|
38
|
+
resp
|
39
|
+
end
|
40
|
+
|
41
|
+
def parse(resp)
|
42
|
+
parsed = Hpricot(resp.read.encode("UTF-8"))
|
43
|
+
number_of_results = (parsed/'div#resultStats').first.inner_text.to_s[/(\S*) result/i, 1].try(:gsub, ',', '').try(:to_i) || 0
|
44
|
+
result_set = SimpleGoogleCustomSearch::ResultSet.new(total: number_of_results)
|
45
|
+
result_set.item = (parsed/"li.g").map do |ele|
|
46
|
+
SimpleGoogleCustomSearch::Result.new({
|
47
|
+
:title => ele.at("a").inner_text,
|
48
|
+
:link => "http://www.google.com#{ele.at("a")['href']}",
|
49
|
+
:description => (ele/("span.st")).to_s
|
50
|
+
})
|
51
|
+
end
|
52
|
+
result_set
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class SimpleGoogleCustomSearch::SgcsConfig
|
2
|
+
attr_reader :domain
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
raise 'Please set constant variable SGCS_CONFIG' unless defined?(SGCS_CONFIG)
|
6
|
+
config = SGCS_CONFIG.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
7
|
+
raise 'Please set constant variable SGCS_CONFIG[:domain]' if config[:domain].nil?
|
8
|
+
|
9
|
+
@domain = config[:domain]
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_config
|
13
|
+
@config ||= {
|
14
|
+
domain: @domain
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_google_custom_search/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "simple_google_custom_search"
|
8
|
+
gem.version = SimpleGoogleCustomSearch::VERSION
|
9
|
+
gem.authors = ["Cher Wei Soh"]
|
10
|
+
gem.email = ["cherwei.soh@gmail.com"]
|
11
|
+
gem.description = %q{With Simple Google Custom Search, add a search box to your homepage to help people find what they need on your website}
|
12
|
+
gem.summary = %q{Simple Google Query Search API}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_dependency "hpricot"
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_google_custom_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cher Wei Soh
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: hpricot
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: With Simple Google Custom Search, add a search box to your homepage to
|
31
|
+
help people find what they need on your website
|
32
|
+
email:
|
33
|
+
- cherwei.soh@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- lib/simple_google_custom_search.rb
|
44
|
+
- lib/simple_google_custom_search/result.rb
|
45
|
+
- lib/simple_google_custom_search/result_set.rb
|
46
|
+
- lib/simple_google_custom_search/sgcs_config.rb
|
47
|
+
- lib/simple_google_custom_search/version.rb
|
48
|
+
- simple_google_custom_search.gemspec
|
49
|
+
homepage: ''
|
50
|
+
licenses: []
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.8.25
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Simple Google Query Search API
|
73
|
+
test_files: []
|