tangzero-rgsearch 0.8.5 → 0.9
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/README.textile +32 -0
- data/Rakefile +52 -0
- data/lib/rgsearch/base_result.rb +12 -0
- data/lib/rgsearch/exception.rb +17 -0
- data/lib/rgsearch/request.rb +29 -0
- data/lib/rgsearch/result.rb +16 -0
- data/lib/rgsearch/resultset.rb +44 -0
- data/lib/rgsearch/search.rb +54 -0
- data/lib/rgsearch.rb +18 -0
- metadata +14 -5
data/README.textile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
h2. INSTALLATION
|
2
|
+
|
3
|
+
@sudo gem install tangzero-rgsearch -s http://gems.github.com@
|
4
|
+
|
5
|
+
h2. USAGE
|
6
|
+
|
7
|
+
@coming soon@
|
8
|
+
|
9
|
+
h2. LICENSE:
|
10
|
+
|
11
|
+
(The MIT License)
|
12
|
+
|
13
|
+
Copyright (c) 2009 TangZero
|
14
|
+
|
15
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
16
|
+
a copy of this software and associated documentation files (the
|
17
|
+
'Software'), to deal in the Software without restriction, including
|
18
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
19
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
20
|
+
permit persons to whom the Software is furnished to do so, subject to
|
21
|
+
the following conditions:
|
22
|
+
|
23
|
+
The above copyright notice and this permission notice shall be
|
24
|
+
included in all copies or substantial portions of the Software.
|
25
|
+
|
26
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
27
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
28
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
29
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
30
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
31
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
32
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rubygems/specification'
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
require 'spec/rake/spectask'
|
6
|
+
|
7
|
+
GEM = "rgsearch"
|
8
|
+
GEM_VERSION = "0.9"
|
9
|
+
SUMMARY = "Ruby Google Search Mashup Framework"
|
10
|
+
AUTHOR = "TangZero"
|
11
|
+
EMAIL = "t4ngz3r0@gmail.com"
|
12
|
+
HOMEPAGE = "http://github.com/tangzero/rgsearch"
|
13
|
+
|
14
|
+
|
15
|
+
spec = Gem::Specification.new do |s|
|
16
|
+
s.name = GEM
|
17
|
+
s.version = GEM_VERSION
|
18
|
+
s.platform = Gem::Platform::RUBY
|
19
|
+
s.summary = SUMMARY
|
20
|
+
s.require_paths = ['lib']
|
21
|
+
s.files = FileList['lib/**/*.rb', '[A-Z]*'].to_a
|
22
|
+
|
23
|
+
s.add_dependency(%q<json>, [">= 1.1.1"])
|
24
|
+
s.has_rdoc = false
|
25
|
+
|
26
|
+
s.author = AUTHOR
|
27
|
+
s.email = EMAIL
|
28
|
+
s.homepage = HOMEPAGE
|
29
|
+
|
30
|
+
s.rubyforge_project = GEM # GitHub bug, gem isn't being build when this miss
|
31
|
+
end
|
32
|
+
|
33
|
+
Spec::Rake::SpecTask.new do |t|
|
34
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
35
|
+
t.spec_opts = %w(-fs --color)
|
36
|
+
end
|
37
|
+
|
38
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
39
|
+
pkg.gem_spec = spec
|
40
|
+
end
|
41
|
+
|
42
|
+
desc "Install the gem locally"
|
43
|
+
task :install => [:package] do
|
44
|
+
sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
|
45
|
+
end
|
46
|
+
|
47
|
+
desc "Create a gemspec file"
|
48
|
+
task :make_spec do
|
49
|
+
File.open("#{GEM}.gemspec", "w") do |file|
|
50
|
+
file.puts spec.to_ruby
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module RGSearch
|
2
|
+
|
3
|
+
class Request
|
4
|
+
|
5
|
+
def self.get(url, options = {})
|
6
|
+
uri = URI.parse URI.encode(url)
|
7
|
+
uri.query = to_query options
|
8
|
+
request = Net::HTTP::Get.new uri.request_uri
|
9
|
+
response = connection.start(uri.host) { |http| http.request request }
|
10
|
+
response.body
|
11
|
+
rescue Exception => e
|
12
|
+
raise RGSearchException, e.message
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def self.connection()
|
17
|
+
Net::HTTP.Proxy RGSearch.proxy_host, RGSearch.proxy_port,
|
18
|
+
RGSearch.proxy_user, RGSearch.proxy_pass
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.to_query(options = {})
|
22
|
+
query = []
|
23
|
+
options.each { |k, v| query << "#{k}=#{v}" }
|
24
|
+
URI.encode query.join('&')
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module RGSearch
|
2
|
+
|
3
|
+
class ResultSet < BaseResult
|
4
|
+
|
5
|
+
attr_reader :results, :cursor
|
6
|
+
|
7
|
+
def initialize(data)
|
8
|
+
@results = []
|
9
|
+
@cursor = Result.new
|
10
|
+
data.each do |k, v|
|
11
|
+
if k == 'responseData' and v != nil
|
12
|
+
v.each do |k, v|
|
13
|
+
if k == 'results'
|
14
|
+
v.each do |result|
|
15
|
+
@results << Result.new(result)
|
16
|
+
end
|
17
|
+
elsif k == 'cursor'
|
18
|
+
pages = []
|
19
|
+
cursor_data = { :pages => pages }
|
20
|
+
v.each do |k, v|
|
21
|
+
if k == 'pages'
|
22
|
+
v.each do |page|
|
23
|
+
pages << Result.new(page)
|
24
|
+
end
|
25
|
+
else
|
26
|
+
cursor_data[k] = v
|
27
|
+
end
|
28
|
+
end
|
29
|
+
@cursor = Result.new(cursor_data)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
else
|
33
|
+
set_parameter(k, v)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_s
|
39
|
+
@results.inspect
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module RGSearch
|
2
|
+
|
3
|
+
class Search
|
4
|
+
|
5
|
+
def self.web(query, options = {})
|
6
|
+
find 'web', query, options
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.images(query, options = {})
|
10
|
+
find 'images', query, options
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.news(query, options = {})
|
14
|
+
find 'news', query, options
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.blogs(query, options = {})
|
18
|
+
find 'blogs', query, options
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.videos(query, options = {})
|
22
|
+
find 'video', query, options
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.books(query, options = {})
|
26
|
+
find 'books', query, options
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.local(query, options = {})
|
30
|
+
find 'local', query, options
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def self.find(engine, query, options)
|
35
|
+
url = URI.encode "#{API_URL}/#{engine}"
|
36
|
+
validate options, query
|
37
|
+
data = Request.get(url, options)
|
38
|
+
ResultSet.new JSON.parse(data)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.validate(options, query)
|
42
|
+
raise RGSearchException, 'key is not specified' unless RGSearch.key
|
43
|
+
raise RGSearchException, 'start is invalid' if options[:start] and options[:start] < 0
|
44
|
+
unless ["small", "large"].include? options[:rsz] || "small"
|
45
|
+
raise RGSearchException, 'result size is invalid'
|
46
|
+
end
|
47
|
+
options[:q] = query
|
48
|
+
options[:v] = API_VERSION
|
49
|
+
options[:key] = RGSearch.key
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/lib/rgsearch.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
Dir["#{File.dirname(__FILE__)}/rgsearch/*.rb"].sort.each { |lib| require lib }
|
7
|
+
|
8
|
+
module RGSearch
|
9
|
+
|
10
|
+
API_VERSION = '1.0'
|
11
|
+
API_URL = 'http://ajax.googleapis.com/ajax/services/search'
|
12
|
+
|
13
|
+
%w(proxy_host proxy_port proxy_user proxy_pass key).each do |att|
|
14
|
+
attr_accessor att.to_sym
|
15
|
+
module_function att.to_sym, "#{att}=".to_sym
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tangzero-rgsearch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: "0.9"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TangZero
|
@@ -9,17 +9,18 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-03-06 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: json
|
17
|
+
type: :runtime
|
17
18
|
version_requirement:
|
18
19
|
version_requirements: !ruby/object:Gem::Requirement
|
19
20
|
requirements:
|
20
21
|
- - ">="
|
21
22
|
- !ruby/object:Gem::Version
|
22
|
-
version: 1.1.
|
23
|
+
version: 1.1.1
|
23
24
|
version:
|
24
25
|
description:
|
25
26
|
email: t4ngz3r0@gmail.com
|
@@ -29,8 +30,16 @@ extensions: []
|
|
29
30
|
|
30
31
|
extra_rdoc_files: []
|
31
32
|
|
32
|
-
files:
|
33
|
-
|
33
|
+
files:
|
34
|
+
- lib/rgsearch/exception.rb
|
35
|
+
- lib/rgsearch/search.rb
|
36
|
+
- lib/rgsearch/base_result.rb
|
37
|
+
- lib/rgsearch/request.rb
|
38
|
+
- lib/rgsearch/result.rb
|
39
|
+
- lib/rgsearch/resultset.rb
|
40
|
+
- lib/rgsearch.rb
|
41
|
+
- Rakefile
|
42
|
+
- README.textile
|
34
43
|
has_rdoc: false
|
35
44
|
homepage: http://github.com/tangzero/rgsearch
|
36
45
|
post_install_message:
|