googleajax 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/VERSION +1 -1
- data/googleajax.gemspec +77 -0
- data/lib/googleajax.rb +1 -2
- data/lib/googleajax/api.rb +4 -0
- data/lib/googleajax/filters.rb +4 -0
- data/lib/googleajax/language.rb +10 -2
- data/lib/googleajax/request.rb +17 -9
- data/lib/googleajax/search.rb +1 -0
- data/spec/googleajax_common.rb +19 -1
- data/spec/temp_spec.rb +9 -0
- metadata +5 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/googleajax.gemspec
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{googleajax}
|
8
|
+
s.version = "1.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["geemus(Wesley Beary)", "Marc-Andre Lafortune"]
|
12
|
+
s.date = %q{2010-03-01}
|
13
|
+
s.email = %q{me@geemus.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README.txt"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".gitignore",
|
19
|
+
"History.txt",
|
20
|
+
"README.txt",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"googleajax.gemspec",
|
24
|
+
"lib/googleajax.rb",
|
25
|
+
"lib/googleajax/api.rb",
|
26
|
+
"lib/googleajax/as_hash.rb",
|
27
|
+
"lib/googleajax/as_open_struct.rb",
|
28
|
+
"lib/googleajax/base.rb",
|
29
|
+
"lib/googleajax/basic.rb",
|
30
|
+
"lib/googleajax/extensions/hash.rb",
|
31
|
+
"lib/googleajax/extensions/kernel.rb",
|
32
|
+
"lib/googleajax/extensions/string.rb",
|
33
|
+
"lib/googleajax/feed.rb",
|
34
|
+
"lib/googleajax/filters.rb",
|
35
|
+
"lib/googleajax/hash.rb",
|
36
|
+
"lib/googleajax/language.rb",
|
37
|
+
"lib/googleajax/request.rb",
|
38
|
+
"lib/googleajax/results.rb",
|
39
|
+
"lib/googleajax/search.rb",
|
40
|
+
"spec/googleajax_as_open_struct_spec.rb",
|
41
|
+
"spec/googleajax_basic_spec.rb",
|
42
|
+
"spec/googleajax_common.rb",
|
43
|
+
"spec/googleajax_spec.rb",
|
44
|
+
"spec/spec.opts",
|
45
|
+
"spec/spec_helper.rb"
|
46
|
+
]
|
47
|
+
s.homepage = %q{http://github.com/geemus/googleajax}
|
48
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
49
|
+
s.require_paths = ["lib"]
|
50
|
+
s.rubygems_version = %q{1.3.5}
|
51
|
+
s.summary = %q{Ruby wrapper to the Google AJAX API REST interfaces(Feeds, Language and Search).}
|
52
|
+
s.test_files = [
|
53
|
+
"spec/googleajax_as_open_struct_spec.rb",
|
54
|
+
"spec/googleajax_basic_spec.rb",
|
55
|
+
"spec/googleajax_common.rb",
|
56
|
+
"spec/googleajax_spec.rb",
|
57
|
+
"spec/spec_helper.rb",
|
58
|
+
"spec/temp_spec.rb"
|
59
|
+
]
|
60
|
+
|
61
|
+
if s.respond_to? :specification_version then
|
62
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
63
|
+
s.specification_version = 3
|
64
|
+
|
65
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
66
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
67
|
+
s.add_runtime_dependency(%q<json>, [">= 1.0.0"])
|
68
|
+
else
|
69
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
70
|
+
s.add_dependency(%q<json>, [">= 1.0.0"])
|
71
|
+
end
|
72
|
+
else
|
73
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
74
|
+
s.add_dependency(%q<json>, [">= 1.0.0"])
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
data/lib/googleajax.rb
CHANGED
data/lib/googleajax/api.rb
CHANGED
@@ -26,7 +26,11 @@ module GoogleAjax
|
|
26
26
|
Filters::Recursive.remap(data, self)
|
27
27
|
end
|
28
28
|
|
29
|
+
LOOKS_LIKE_HTML = /\w*<html>/i
|
29
30
|
def parse(data)
|
31
|
+
if data =~ LOOKS_LIKE_HTML
|
32
|
+
raise StandardError, /<title>(.*)<\/title>/.match(data)[1]
|
33
|
+
end
|
30
34
|
if defined? Rails
|
31
35
|
ActiveSupport::JSON::decode(data)
|
32
36
|
else
|
data/lib/googleajax/filters.rb
CHANGED
@@ -44,6 +44,7 @@ module GoogleAjax
|
|
44
44
|
TRUE_OR_FALSE = /^true|(false)$/i
|
45
45
|
INTEGER = /^\d+$/
|
46
46
|
FLOAT = /^\d+\.\d+$/
|
47
|
+
TIME = /^[A-Z][a-z]{2}, \d\d [A-Z][a-z]{2} \d{4} \d\d:\d\d:\d\d \S*$/ # Very strict date format
|
47
48
|
def initialize(h)
|
48
49
|
h.each do |key, value|
|
49
50
|
# Won't use Integer.try_convert for 1.8.6 & 7 compatibility
|
@@ -54,6 +55,9 @@ module GoogleAjax
|
|
54
55
|
h[key] = value.to_f
|
55
56
|
when TRUE_OR_FALSE
|
56
57
|
h[key] = Regexp.last_match[1].nil?
|
58
|
+
when TIME
|
59
|
+
require 'time'
|
60
|
+
h[key] = Time.parse(value)
|
57
61
|
end
|
58
62
|
end
|
59
63
|
super(h)
|
data/lib/googleajax/language.rb
CHANGED
@@ -14,9 +14,17 @@ module GoogleAjax
|
|
14
14
|
# source and destination can be the language code ("en") or a symbol (:english).
|
15
15
|
# source can also be an empty string, in which case Google does an auto-detection and
|
16
16
|
# will set :detected_source_language to the language it used as a source.
|
17
|
+
#
|
18
|
+
# Google limits GET request to 2000 chars, but allows POST for the translate request
|
19
|
+
# so we use this to allow texts up to 5000 chars (another limit imposed by Google).
|
20
|
+
#
|
21
|
+
# TODO: Give easy way to shorten text downto the acceptable limit.
|
17
22
|
def self.translate(query, source, destination, args = {})
|
18
|
-
langpair = [source, destination].map{|l| LANGUAGE_CODE[l] || l.to_s}.join("
|
19
|
-
args = {
|
23
|
+
langpair = [source, destination].map{|l| LANGUAGE_CODE[l] || l.to_s}.join("|")
|
24
|
+
args = {
|
25
|
+
:langpair => langpair,
|
26
|
+
:http_method => :post
|
27
|
+
}.merge(args)
|
20
28
|
get(:translate, query, args)
|
21
29
|
end
|
22
30
|
|
data/lib/googleajax/request.rb
CHANGED
@@ -1,21 +1,29 @@
|
|
1
1
|
module GoogleAjax
|
2
2
|
module Request
|
3
|
-
|
4
|
-
|
3
|
+
HOST = 'ajax.googleapis.com'
|
4
|
+
PATH_PREFIX = "/ajax/services/"
|
5
5
|
attr_accessor :api_key
|
6
6
|
attr_accessor :referer
|
7
7
|
alias_method :referrer, :referer # See mispelling section in http://en.wikipedia.org/wiki/HTTP_referrer
|
8
8
|
alias_method :referrer=, :referer=
|
9
9
|
|
10
10
|
# Api doc is at http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje
|
11
|
+
# We use one special option (not forwarded to google): :html_method => :post will use a Post instead of a Get.
|
11
12
|
def get(api, method, query, args = nil)
|
12
13
|
raise "You must assign a value to GoogleAjax.referer" unless referer
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
args ||= {}
|
15
|
+
use_post = args.delete(:http_method) == :post
|
16
|
+
args = args.merge!('q' => query)
|
17
|
+
args['key'] = api_key if api_key
|
18
|
+
if use_post
|
19
|
+
request = Net::HTTP::Post.new("#{PATH_PREFIX}#{api}/#{method}", "Referer" => referer)
|
20
|
+
request.set_form_data(args)
|
21
|
+
else
|
22
|
+
request = Net::HTTP::Get.new("#{PATH_PREFIX}#{api}/#{method}?" + args.map{|k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join("&"), "Referer" => referer)
|
23
|
+
end
|
24
|
+
Net::HTTP.new(HOST).start do |http|
|
25
|
+
http.request(request).body
|
26
|
+
end
|
19
27
|
end
|
20
28
|
end
|
21
|
-
end
|
29
|
+
end
|
data/lib/googleajax/search.rb
CHANGED
@@ -34,6 +34,7 @@ module GoogleAjax
|
|
34
34
|
# :call-seq:
|
35
35
|
# news(query, args = {})
|
36
36
|
# Arguments: http://code.google.com/apis/ajaxsearch/documentation/reference.html#_fonje_news
|
37
|
+
# Contrary to the doc, scoring=d is not an acceptable parameter (nor is scoring=date)
|
37
38
|
|
38
39
|
##
|
39
40
|
# :call-seq:
|
data/spec/googleajax_common.rb
CHANGED
@@ -17,6 +17,20 @@ shared_examples_for "GoogleAjax" do
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
+
describe ".news" do
|
21
|
+
before :each do
|
22
|
+
GoogleAjax.referer = "http://example.com"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns fresh news" do
|
26
|
+
one_day = 24 * 60 * 60
|
27
|
+
yesterday = Time.now - one_day
|
28
|
+
results = GoogleAjax::Search.news("", :topic => :h)[:results]
|
29
|
+
results.size.should >= 4
|
30
|
+
results.all?{|r| r[:published_date].should >= yesterday}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
20
34
|
{
|
21
35
|
:blogs => 100_000,
|
22
36
|
:books => 10_000,
|
@@ -65,6 +79,10 @@ shared_examples_for "GoogleAjax" do
|
|
65
79
|
it "does an approximate translation" do
|
66
80
|
GoogleAjax::Language.translate("Ruby rocks", "en", "fr")[:translated_text].should == "Ruby roches"
|
67
81
|
end
|
82
|
+
|
83
|
+
it "translates texts longer than the 2k limit on get requets" do
|
84
|
+
GoogleAjax::Language.translate("Ruby rocks. "*250, "en", "fr")[:translated_text].count("Ruby").should == 1000
|
85
|
+
end
|
68
86
|
end
|
69
87
|
end
|
70
88
|
|
@@ -77,7 +95,7 @@ shared_examples_for "GoogleAjax" do
|
|
77
95
|
it "returns the right feeds" do
|
78
96
|
feeds = GoogleAjax::Feed.find("Ruby")
|
79
97
|
feeds.size.should == 10
|
80
|
-
feeds.
|
98
|
+
feeds.map{|f| f[:url]}.should include "http://www.ruby-lang.org/en/feeds/news.rss"
|
81
99
|
end
|
82
100
|
end
|
83
101
|
|
data/spec/temp_spec.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/googleajax')
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
describe "GoogleAjax (temp)" do
|
5
|
+
it "returns results as a hash" do
|
6
|
+
GoogleAjax.referer = "http://example.com"
|
7
|
+
GoogleAjax::Language.translate("Ruby rocks", "en", "fr").should == "Ruby roches"
|
8
|
+
end
|
9
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: googleajax
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- geemus(Wesley Beary)
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2010-03-01 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -42,10 +42,12 @@ extensions: []
|
|
42
42
|
extra_rdoc_files:
|
43
43
|
- README.txt
|
44
44
|
files:
|
45
|
+
- .gitignore
|
45
46
|
- History.txt
|
46
47
|
- README.txt
|
47
48
|
- Rakefile
|
48
49
|
- VERSION
|
50
|
+
- googleajax.gemspec
|
49
51
|
- lib/googleajax.rb
|
50
52
|
- lib/googleajax/api.rb
|
51
53
|
- lib/googleajax/as_hash.rb
|
@@ -102,3 +104,4 @@ test_files:
|
|
102
104
|
- spec/googleajax_common.rb
|
103
105
|
- spec/googleajax_spec.rb
|
104
106
|
- spec/spec_helper.rb
|
107
|
+
- spec/temp_spec.rb
|