tangzero-rgsearch 0.9 → 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +11 -11
- data/lib/rgsearch.rb +8 -10
- data/lib/rgsearch/request.rb +20 -16
- data/lib/rgsearch/result.rb +37 -10
- data/lib/rgsearch/search.rb +50 -50
- data/lib/rgsearch/unicode.rb +20 -0
- metadata +6 -7
- data/lib/rgsearch/base_result.rb +0 -12
- data/lib/rgsearch/resultset.rb +0 -44
data/Rakefile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
require "rubygems"
|
2
|
+
require "rubygems/specification"
|
3
|
+
require "rake"
|
4
|
+
require "rake/gempackagetask"
|
5
|
+
require "spec/rake/spectask"
|
6
6
|
|
7
7
|
GEM = "rgsearch"
|
8
|
-
GEM_VERSION = "0
|
8
|
+
GEM_VERSION = "1.0"
|
9
9
|
SUMMARY = "Ruby Google Search Mashup Framework"
|
10
10
|
AUTHOR = "TangZero"
|
11
11
|
EMAIL = "t4ngz3r0@gmail.com"
|
@@ -17,21 +17,21 @@ spec = Gem::Specification.new do |s|
|
|
17
17
|
s.version = GEM_VERSION
|
18
18
|
s.platform = Gem::Platform::RUBY
|
19
19
|
s.summary = SUMMARY
|
20
|
-
s.require_paths = [
|
21
|
-
s.files = FileList[
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.files = FileList["lib/**/*.rb", "[A-Z]*"].to_a
|
22
22
|
|
23
|
-
s.add_dependency(%q<
|
23
|
+
s.add_dependency(%q<activesupport>, [">= 2.2.2"])
|
24
24
|
s.has_rdoc = false
|
25
25
|
|
26
26
|
s.author = AUTHOR
|
27
27
|
s.email = EMAIL
|
28
28
|
s.homepage = HOMEPAGE
|
29
29
|
|
30
|
-
s.rubyforge_project = GEM # GitHub bug, gem isn
|
30
|
+
s.rubyforge_project = GEM # GitHub bug, gem isn"t being build when this miss
|
31
31
|
end
|
32
32
|
|
33
33
|
Spec::Rake::SpecTask.new do |t|
|
34
|
-
t.spec_files = FileList[
|
34
|
+
t.spec_files = FileList["spec/**/*_spec.rb"]
|
35
35
|
t.spec_opts = %w(-fs --color)
|
36
36
|
end
|
37
37
|
|
data/lib/rgsearch.rb
CHANGED
@@ -1,18 +1,16 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require "rubygems"
|
2
|
+
require "uri"
|
3
|
+
require "open-uri"
|
4
|
+
require "active_support"
|
5
5
|
|
6
6
|
Dir["#{File.dirname(__FILE__)}/rgsearch/*.rb"].sort.each { |lib| require lib }
|
7
7
|
|
8
8
|
module RGSearch
|
9
9
|
|
10
|
-
API_VERSION =
|
11
|
-
API_URL =
|
10
|
+
API_VERSION = "1.0"
|
11
|
+
API_URL = "http://ajax.googleapis.com/ajax/services/search"
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
module_function att.to_sym, "#{att}=".to_sym
|
16
|
-
end
|
13
|
+
attr_accessor :key
|
14
|
+
module_function :key, :key=
|
17
15
|
|
18
16
|
end
|
data/lib/rgsearch/request.rb
CHANGED
@@ -1,29 +1,33 @@
|
|
1
1
|
module RGSearch
|
2
2
|
|
3
|
-
|
3
|
+
class Request
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
uri
|
8
|
-
|
9
|
-
|
10
|
-
response.body
|
5
|
+
def self.get(url, params = {}, options = {})
|
6
|
+
options = {:proxy => proxy?}.merge(options)
|
7
|
+
uri = get_uri(url, params)
|
8
|
+
response = uri.open(options).collect.join
|
9
|
+
Unicode.unescape(response)
|
11
10
|
rescue Exception => e
|
12
11
|
raise RGSearchException, e.message
|
13
12
|
end
|
14
13
|
|
15
|
-
private
|
16
|
-
def self.
|
17
|
-
|
18
|
-
RGSearch.proxy_user, RGSearch.proxy_pass
|
14
|
+
private
|
15
|
+
def self.proxy?
|
16
|
+
ENV.include?("http_proxy") || ENV.include?("HTTP_PROXY")
|
19
17
|
end
|
20
|
-
|
21
|
-
def self.to_query(
|
18
|
+
|
19
|
+
def self.to_query(params = {})
|
22
20
|
query = []
|
23
|
-
|
24
|
-
URI.encode
|
21
|
+
params.each { |k, v| query << "#{k}=#{v}" }
|
22
|
+
URI.encode(query.join("&"))
|
25
23
|
end
|
26
24
|
|
27
|
-
|
25
|
+
def self.get_uri(url, params)
|
26
|
+
uri = URI.parse(URI.encode(url))
|
27
|
+
uri.query = to_query(params)
|
28
|
+
return uri
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
28
32
|
|
29
33
|
end
|
data/lib/rgsearch/result.rb
CHANGED
@@ -1,16 +1,43 @@
|
|
1
1
|
module RGSearch
|
2
2
|
|
3
|
-
class Result
|
3
|
+
class Result
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
if data.kind_of?(Hash)
|
7
|
+
data.each do |key, value|
|
8
|
+
set_att(key, make_result(value))
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
4
12
|
|
5
|
-
|
6
|
-
|
7
|
-
|
13
|
+
private
|
14
|
+
def make_result(value)
|
15
|
+
if value.kind_of?(String) or value.kind_of?(Numeric)
|
16
|
+
value
|
17
|
+
elsif value.kind_of?(Hash)
|
18
|
+
Result.new(value)
|
19
|
+
elsif value.kind_of?(Array)
|
20
|
+
value.collect {|v| make_result(v)}
|
21
|
+
end
|
8
22
|
end
|
9
|
-
|
10
|
-
def
|
11
|
-
|
23
|
+
|
24
|
+
def set_att(key, value)
|
25
|
+
key = to_att(key)
|
26
|
+
instance_variable_set("@#{key}", value)
|
27
|
+
instance_eval("def #{key}; @#{key}; end")
|
12
28
|
end
|
13
|
-
|
29
|
+
|
30
|
+
def to_att(att)
|
31
|
+
i = 0
|
32
|
+
word = []
|
33
|
+
att.each_char do |c|
|
34
|
+
i = i + 1 if c =~ /[A-Z]/
|
35
|
+
word[i] = "" if word[i].nil?
|
36
|
+
word[i] << c.downcase
|
37
|
+
end
|
38
|
+
word.compact.join("_")
|
39
|
+
end
|
40
|
+
|
14
41
|
end
|
15
|
-
|
16
|
-
end
|
42
|
+
|
43
|
+
end
|
data/lib/rgsearch/search.rb
CHANGED
@@ -1,54 +1,54 @@
|
|
1
1
|
module RGSearch
|
2
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
3
|
+
class Search
|
4
|
+
|
5
|
+
def self.web(query, params = {})
|
6
|
+
find "web", query, params
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.images(query, params = {})
|
10
|
+
find "images", query, params
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.news(query, params = {})
|
14
|
+
find "news", query, params
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.blogs(query, params = {})
|
18
|
+
find "blogs", query, params
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.videos(query, params = {})
|
22
|
+
find "video", query, params
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.books(query, params = {})
|
26
|
+
find "books", query, params
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.local(query, params = {})
|
30
|
+
find "local", query, params
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def self.find(engine, query, params)
|
35
|
+
url = URI.encode "#{API_URL}/#{engine}"
|
36
|
+
validate params, query
|
37
|
+
data = Request.get(url, params, {})
|
38
|
+
Result.new ActiveSupport::JSON.decode(data)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.validate(params, query)
|
42
|
+
raise RGSearchException, "key is not specified" unless RGSearch.key
|
43
|
+
raise RGSearchException, "start is invalid" if params[:start] and params[:start] < 0
|
44
|
+
unless ["small", "large"].include? params[:rsz] || "small"
|
45
|
+
raise RGSearchException, "result size is invalid"
|
46
|
+
end
|
47
|
+
params[:q] = query
|
48
|
+
params[:v] = API_VERSION
|
49
|
+
params[:key] = RGSearch.key
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
53
|
|
54
54
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RGSearch
|
2
|
+
|
3
|
+
class Unicode
|
4
|
+
|
5
|
+
def self.escape(str)
|
6
|
+
ary = str.unpack("U*").map!{|i| "\\u#{i.to_s(16)}"}
|
7
|
+
ary.join
|
8
|
+
end
|
9
|
+
|
10
|
+
UNESCAPE_ARRAY = []
|
11
|
+
def self.unescape(str)
|
12
|
+
str.gsub(/\\u([0-9a-f]{4})/) {
|
13
|
+
UNESCAPE_ARRAY[0] = $1.hex
|
14
|
+
UNESCAPE_ARRAY.pack("U")
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
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: "1.0"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TangZero
|
@@ -9,18 +9,18 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-04-17 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
16
|
+
name: activesupport
|
17
17
|
type: :runtime
|
18
18
|
version_requirement:
|
19
19
|
version_requirements: !ruby/object:Gem::Requirement
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: 2.2.2
|
24
24
|
version:
|
25
25
|
description:
|
26
26
|
email: t4ngz3r0@gmail.com
|
@@ -32,11 +32,10 @@ extra_rdoc_files: []
|
|
32
32
|
|
33
33
|
files:
|
34
34
|
- lib/rgsearch/exception.rb
|
35
|
-
- lib/rgsearch/search.rb
|
36
|
-
- lib/rgsearch/base_result.rb
|
37
35
|
- lib/rgsearch/request.rb
|
38
36
|
- lib/rgsearch/result.rb
|
39
|
-
- lib/rgsearch/
|
37
|
+
- lib/rgsearch/search.rb
|
38
|
+
- lib/rgsearch/unicode.rb
|
40
39
|
- lib/rgsearch.rb
|
41
40
|
- Rakefile
|
42
41
|
- README.textile
|
data/lib/rgsearch/base_result.rb
DELETED
data/lib/rgsearch/resultset.rb
DELETED
@@ -1,44 +0,0 @@
|
|
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
|