tangzero-rgsearch 0.9 → 1.0

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/Rakefile CHANGED
@@ -1,11 +1,11 @@
1
- require 'rubygems'
2
- require 'rubygems/specification'
3
- require 'rake'
4
- require 'rake/gempackagetask'
5
- require 'spec/rake/spectask'
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.9"
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 = ['lib']
21
- s.files = FileList['lib/**/*.rb', '[A-Z]*'].to_a
20
+ s.require_paths = ["lib"]
21
+ s.files = FileList["lib/**/*.rb", "[A-Z]*"].to_a
22
22
 
23
- s.add_dependency(%q<json>, [">= 1.1.1"])
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't being build when this miss
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['spec/**/*_spec.rb']
34
+ t.spec_files = FileList["spec/**/*_spec.rb"]
35
35
  t.spec_opts = %w(-fs --color)
36
36
  end
37
37
 
@@ -1,18 +1,16 @@
1
- require 'rubygems'
2
- require 'net/http'
3
- require 'uri'
4
- require 'json'
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 = '1.0'
11
- API_URL = 'http://ajax.googleapis.com/ajax/services/search'
10
+ API_VERSION = "1.0"
11
+ API_URL = "http://ajax.googleapis.com/ajax/services/search"
12
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
13
+ attr_accessor :key
14
+ module_function :key, :key=
17
15
 
18
16
  end
@@ -1,29 +1,33 @@
1
1
  module RGSearch
2
2
 
3
- class Request
3
+ class Request
4
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
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.connection()
17
- Net::HTTP.Proxy RGSearch.proxy_host, RGSearch.proxy_port,
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(options = {})
18
+
19
+ def self.to_query(params = {})
22
20
  query = []
23
- options.each { |k, v| query << "#{k}=#{v}" }
24
- URI.encode query.join('&')
21
+ params.each { |k, v| query << "#{k}=#{v}" }
22
+ URI.encode(query.join("&"))
25
23
  end
26
24
 
27
- end
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
@@ -1,16 +1,43 @@
1
1
  module RGSearch
2
2
 
3
- class Result < BaseResult
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
- def initialize(data = {})
6
- @data = data
7
- @data.each { |k, v| set_parameter(k, v) }
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 to_s
11
- @data.inspect
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
@@ -1,54 +1,54 @@
1
1
  module RGSearch
2
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
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.9"
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-03-06 00:00:00 -08:00
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: json
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: 1.1.1
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/resultset.rb
37
+ - lib/rgsearch/search.rb
38
+ - lib/rgsearch/unicode.rb
40
39
  - lib/rgsearch.rb
41
40
  - Rakefile
42
41
  - README.textile
@@ -1,12 +0,0 @@
1
- module RGSearch
2
-
3
- class BaseResult
4
-
5
- def set_parameter(key, value)
6
- instance_variable_set("@#{key}", value)
7
- instance_eval("def #{key}; @#{key}; end")
8
- end
9
-
10
- end
11
-
12
- end
@@ -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