googleajax 0.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/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2008-05-19
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/google_ajax.rb
6
+ lib/google_ajax/feed.rb
7
+ lib/google_ajax/language.rb
8
+ lib/google_ajax/parser.rb
9
+ lib/google_ajax/search.rb
10
+ lib/google_ajax/version.rb
11
+ test/test_google_ajax.rb
data/README.txt ADDED
@@ -0,0 +1,60 @@
1
+ = GoogleAjax
2
+
3
+ * Docs: http://googleajax.rubyforge.org
4
+ * Code: http://github.com/monki/google_ajax/tree/master
5
+
6
+ == DESCRIPTION:
7
+
8
+ Ruby wrapper to the Google AJAX API REST interfaces(Feeds, Language and Search).
9
+
10
+ == SYNOPSIS:
11
+
12
+ * First, setup referer: GoogleAjax.referer = 'http://mydomain.com'
13
+ * Optionally, you can also set an api_key: GoogleAjax.api_key = 'YOUR_API_KEY'
14
+
15
+ === Now you are set to go, so a few examples:
16
+ * GoogleAjax::Feed.find('ruby') # Find top 10 feeds for 'ruby'
17
+ * GoogleAjax::Feed.load('http://monki.geemus.com/feed/atom.xml') # Load 4 most recent entries from feed 'http://monki.geemus.com/feed/atom.xml'
18
+ * GoogleAjax::Feed.lookup('http://monki.geemus.com') # Find feed for 'http://monki.geemus.com'
19
+ * GoogleAjax::Language.detect('Ciao mondo') # Find the language of the string 'Ciao mondo'
20
+ * GoogleAjax::Language.translate('Hello world', 'en', 'it') # Translate 'Hello world' to italian
21
+ * GoogleAjax::Search.blogs('ruby') # Find top 4 blogs for 'ruby'
22
+ * GoogleAjax::Search.books('ruby') # Find top 4 books for 'ruby'
23
+ * GoogleAjax::Search.images('ruby') # Find top 4 images for 'ruby'
24
+ * GoogleAjax::Search.local('ruby', 48.8565, 2.3509) # Find top 4 local results for 'ruby' at latitude 48.8565, longitude 2.3509
25
+ * GoogleAjax::Search.news('ruby') # Find top 4 news results for 'ruby'
26
+ * GoogleAjax::Search.video('ruby') # Find top 4 video results for 'ruby'
27
+ * GoogleAjax::Search.web('Hello world') # Find top 4 web page results for 'Hello world'
28
+
29
+ == REQUIREMENTS:
30
+
31
+ * JSON gem to parse the responses
32
+
33
+ == INSTALL:
34
+
35
+ * sudo gem install googleajax --include-dependencies
36
+
37
+ == LICENSE:
38
+
39
+ (The MIT License)
40
+
41
+ Copyright (c) 2008 monki(Wesley Beary) => monki@geemus.com
42
+
43
+ Permission is hereby granted, free of charge, to any person obtaining
44
+ a copy of this software and associated documentation files (the
45
+ 'Software'), to deal in the Software without restriction, including
46
+ without limitation the rights to use, copy, modify, merge, publish,
47
+ distribute, sublicense, and/or sell copies of the Software, and to
48
+ permit persons to whom the Software is furnished to do so, subject to
49
+ the following conditions:
50
+
51
+ The above copyright notice and this permission notice shall be
52
+ included in all copies or substantial portions of the Software.
53
+
54
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
55
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
56
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
57
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
58
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
59
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
60
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ $: << File.dirname(__FILE__) + '/lib'
6
+ require './lib/google_ajax.rb'
7
+
8
+ Hoe.new('googleajax', GoogleAjax::VERSION::STRING) do |p|
9
+ p.rubyforge_name = 'googleajax'
10
+ p.developer('monki(Wesley Beary)', 'monki@geemus.com')
11
+ p.summary = 'Ruby wrapper to the Google AJAX API REST interfaces(Feeds, Language and Search).'
12
+ p.url = 'http://googleajax.rubyforge.com'
13
+ p.remote_rdoc_dir = ''
14
+ p.extra_deps << ['json', '>= 1.0.0']
15
+ end
16
+
17
+ # vim: syntax=Ruby
@@ -0,0 +1,41 @@
1
+ require 'cgi'
2
+ require 'open-uri'
3
+ require 'ostruct'
4
+ require 'rubygems'
5
+ require 'json'
6
+
7
+ require 'google_ajax/feed'
8
+ require 'google_ajax/language'
9
+ require 'google_ajax/parser'
10
+ require 'google_ajax/search'
11
+ require 'google_ajax/version'
12
+
13
+ class GoogleAjax
14
+ API_BASE = 'http://ajax.googleapis.com/ajax/services/'
15
+ @@api_key = @@referer = nil
16
+
17
+ def self.api_key
18
+ @@api_key
19
+ end
20
+ def self.api_key=(key)
21
+ @@api_key = key
22
+ end
23
+ def self.referer
24
+ @@referer
25
+ end
26
+ def self.referer=(referer)
27
+ @@referer = referer
28
+ end
29
+
30
+ # TODO: Pass query to parser, so it knows where it came from. Needed for search paging and useful in general.
31
+ def self.get(api, method, query, args = nil)
32
+ raise "You must assign a value to GoogleAjax.referer" unless @@referer
33
+ url = "#{API_BASE}#{api}/"
34
+ url += "#{method}?"
35
+ url += "&q=#{CGI::escape(query)}"
36
+ url += "&key=" if @@api_key
37
+ url += "&" + args.collect {|key, value| "#{key}=#{value}"}.join('&') if args && !args.empty?
38
+ data = open(url, "Referer" => @@referer).read
39
+ Parser.parse(api, method, data)
40
+ end
41
+ end
@@ -0,0 +1,38 @@
1
+ class GoogleAjax
2
+ class Feed
3
+ FEED_VERSION = 1.0
4
+ def self.get(method, query, args = {})
5
+ args = { :v => FEED_VERSION }.merge!(args)
6
+ GoogleAjax::get(:feed, method, query, args)
7
+ end
8
+
9
+ # will return a list of feeds that match the given query
10
+ def self.find(query, args = {})
11
+ self.get(:find, query)
12
+ end
13
+
14
+ # downloads this feed from Google's servers
15
+ # Optional: args { :num => number of entries to download(default is 4, maximum is 100) }
16
+ def self.load(query, args = {})
17
+ self.get(:load, query, args)
18
+ end
19
+
20
+ # will return the associated feed if it exists for a given url
21
+ def self.lookup(query, args = {})
22
+ self.get(:lookup, query)
23
+ end
24
+
25
+ class Entry < OpenStruct
26
+ def initialize(data)
27
+ super(data)
28
+ end
29
+ end
30
+
31
+ class Feed < OpenStruct
32
+ def initialize(data)
33
+ super(data)
34
+ self.entries = entries.collect {|data| Entry.new(data)} if entries
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,82 @@
1
+ class GoogleAjax
2
+ class Language
3
+ LANGUAGE_VERSION = 1.0
4
+ def self.get(method, query, args = {})
5
+ args = { :v => LANGUAGE_VERSION }.merge!(args)
6
+ GoogleAjax::get(:language, method, query, args)
7
+ end
8
+
9
+ # will return the language code that describes the language of the given text
10
+ def self.detect(query, args = {})
11
+ self.get(:detect, query)
12
+ end
13
+
14
+ # will return translated text for the given text supplied, matching the destination language.
15
+ def self.translate(query, source, destination, args = {})
16
+ args = { :langpair => "#{source}%7C#{destination}"}.merge!(args)
17
+ self.get(:translate, query, args)
18
+ end
19
+
20
+ class Language < OpenStruct
21
+ def initialize(data)
22
+ super(data)
23
+ end
24
+
25
+ def name
26
+ LANGUAGES.invert[self.language]
27
+ end
28
+ end
29
+
30
+ class Translation < OpenStruct
31
+ def initialize(data)
32
+ super(data)
33
+ end
34
+ end
35
+
36
+ LANGUAGES =
37
+ {
38
+ 'ARABIC' => 'ar',
39
+ 'BULGARIAN' => 'bg',
40
+ 'CATALAN' => 'ca',
41
+ 'CHINESE' => 'zh',
42
+ 'CHINESE_SIMPLIFIED' => 'zh-CN',
43
+ 'CHINESE_TRADITIONAL' => 'zh-TW',
44
+ 'CROATIAN' => 'hr',
45
+ 'CZECH' => 'cs',
46
+ 'DANISH' => 'da',
47
+ 'DUTCH'=> 'nl',
48
+ 'ENGLISH' => 'en',
49
+ 'ESTONIAN' => 'et',
50
+ 'FILIPINO' => 'tl',
51
+ 'FINISH' => 'fi',
52
+ 'FRENCH' => 'fr',
53
+ 'GERMAN' => 'de',
54
+ 'GREK' => 'el',
55
+ 'HEBREW' => 'iw',
56
+ 'HINDI' => 'hi',
57
+ 'HUNGARIAN' => 'hu',
58
+ 'INDONESIAN' => 'id',
59
+ 'ITALIAN' => 'it',
60
+ 'JAPANESE' => 'ja',
61
+ 'KOREAN' => 'ko',
62
+ 'LATVIAN' => 'lv',
63
+ 'LITHUANIAN' => 'lt',
64
+ 'NORWEGIAN' => 'no',
65
+ 'PERSIAN' => 'fa',
66
+ 'POLISH' => 'pl',
67
+ 'PORTUGUESE' => 'pt-PT',
68
+ 'ROMANIAN' => 'ro',
69
+ 'RUSIAN' => 'ru',
70
+ 'SERBIAN' => 'sr',
71
+ 'SLOVAK' => 'sk',
72
+ 'SLOVENIAN' => 'sl',
73
+ 'SPANISH' => 'es',
74
+ 'SWEDISH' => 'sv',
75
+ 'THAI' => 'th',
76
+ 'TURKISH' => 'tr',
77
+ 'UKRAINIAN' => 'uk',
78
+ 'VIETNAMESE' => 'vi',
79
+ 'UNKNOWN' => ''
80
+ }
81
+ end
82
+ end
@@ -0,0 +1,90 @@
1
+ class GoogleAjax
2
+ class Parser
3
+ def self.parse(api, method, data)
4
+ data = JSON.parse(data)
5
+ Errors.process(data)
6
+ parser = Parser::PARSERS[api][method]
7
+ parser.process(data['responseData'])
8
+ end
9
+ end
10
+
11
+ class FeedFind < Parser#:nodoc:
12
+ def self.process(data)
13
+ data['entries'].collect {|data| GoogleAjax::Feed::Feed.new(data)} if data
14
+ end
15
+ end
16
+ class FeedLoad < Parser#:nodoc:
17
+ def self.process(data)
18
+ GoogleAjax::Feed::Feed.new(data['feed']) if data
19
+ end
20
+ end
21
+ class FeedLookup < Parser#:nodoc:
22
+ def self.process(data)
23
+ GoogleAjax::Feed::Feed.new(data) if data
24
+ end
25
+ end
26
+
27
+ class LanguageDetect < Parser#:nodoc
28
+ def self.process(data)
29
+ GoogleAjax::Language::Language.new(data) if data
30
+ end
31
+ end
32
+ class LanguageTranslate < Parser#:nodoc
33
+ def self.process(data)
34
+ GoogleAjax::Language::Translation.new(data) if data
35
+ end
36
+ end
37
+
38
+ class SearchBlogs < Parser#:nodoc
39
+ def self.process(data)
40
+ GoogleAjax::Search::Results.new(data) if data
41
+ end
42
+ end
43
+ class SearchBooks < Parser#:nodoc
44
+ def self.process(data)
45
+ GoogleAjax::Search::Results.new(data) if data
46
+ end
47
+ end
48
+ class SearchImages < Parser#:nodoc
49
+ def self.process(data)
50
+ GoogleAjax::Search::Results.new(data) if data
51
+ end
52
+ end
53
+ class SearchLocal < Parser#:nodoc
54
+ def self.process(data)
55
+ GoogleAjax::Search::Results.new(data) if data
56
+ end
57
+ end
58
+ class SearchNews < Parser#:nodoc
59
+ def self.process(data)
60
+ GoogleAjax::Search::Results.new(data) if data
61
+ end
62
+ end
63
+ class SearchVideo < Parser#:nodoc
64
+ def self.process(data)
65
+ GoogleAjax::Search::Results.new(data) if data
66
+ end
67
+ end
68
+ class SearchWeb < Parser#:nodoc
69
+ def self.process(data)
70
+ GoogleAjax::Search::Results.new(data) if data
71
+ end
72
+ end
73
+
74
+ class Parser
75
+ PARSERS = {
76
+ :feed => { :find => FeedFind, :load => FeedLoad, :lookup => FeedLookup },
77
+ :language => { :detect => LanguageDetect, :translate => LanguageTranslate, },
78
+ :search => { :blogs => SearchBlogs, :books => SearchBooks, :images => SearchImages, :local => SearchLocal, :news => SearchNews, :video => SearchVideo, :web => SearchWeb }
79
+ }
80
+ end
81
+
82
+ class Errors
83
+ def self.process(data)
84
+ status = data['responseStatus']
85
+ unless [200, 201, 202, 203, 204, 205, 206].include? status
86
+ raise StandardError.new(data['responseDetails'])
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,69 @@
1
+ class GoogleAjax
2
+ class Search
3
+ SEARCH_VERSION = 1.0
4
+ def self.get(method, query, args = {})
5
+ args = { :v => SEARCH_VERSION }.merge!(args)
6
+ GoogleAjax::get(:search, method, query, args)
7
+ end
8
+
9
+ def self.blogs(query, args = {})
10
+ self.get(:blogs, query, args)
11
+ end
12
+
13
+ def self.books(query, args = {})
14
+ self.get(:books, query, args)
15
+ end
16
+
17
+ def self.images(query, args = {})
18
+ self.get(:images, query, args)
19
+ end
20
+
21
+ def self.local(query, latitude, longitude, args = {})
22
+ args = { :sll => "#{latitude},#{longitude}" }.merge!(args)
23
+ self.get(:local, query, args)
24
+ end
25
+
26
+ def self.news(query, args = {})
27
+ self.get(:news, query, args)
28
+ end
29
+
30
+ def self.video(query, args = {})
31
+ self.get(:video, query, args)
32
+ end
33
+
34
+ def self.web(query, args = {})
35
+ self.get(:web, query, args)
36
+ end
37
+
38
+ class Results < OpenStruct
39
+ def initialize(data)
40
+ super(data)
41
+ self.results = results.collect {|data| Result.new(data)}
42
+ self.cursor = Cursor.new(cursor) if self.cursor
43
+ end
44
+
45
+ def count
46
+ self.cursor.estimatedResultCount
47
+ end
48
+ end
49
+
50
+ class Result < OpenStruct
51
+ def initialize(data)
52
+ super(data)
53
+ end
54
+ end
55
+
56
+ class Cursor < OpenStruct
57
+ def initialize(data)
58
+ super(data)
59
+ self.pages = pages.collect {|data| Page.new(data)}
60
+ end
61
+ end
62
+
63
+ class Page < OpenStruct
64
+ def initialize(data)
65
+ super(data)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,8 @@
1
+ class GoogleAjax
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+ STRING = [MAJOR, MINOR, TINY].join('.')
7
+ end
8
+ end
File without changes
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: googleajax
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - monki(Wesley Beary)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-20 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hoe
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.5.1
32
+ version:
33
+ description: Ruby wrapper to the Google AJAX API REST interfaces(Feeds, Language and Search).
34
+ email:
35
+ - monki@geemus.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - History.txt
42
+ - Manifest.txt
43
+ - README.txt
44
+ files:
45
+ - History.txt
46
+ - Manifest.txt
47
+ - README.txt
48
+ - Rakefile
49
+ - lib/google_ajax.rb
50
+ - lib/google_ajax/feed.rb
51
+ - lib/google_ajax/language.rb
52
+ - lib/google_ajax/parser.rb
53
+ - lib/google_ajax/search.rb
54
+ - lib/google_ajax/version.rb
55
+ - test/test_google_ajax.rb
56
+ has_rdoc: true
57
+ homepage: http://googleajax.rubyforge.com
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --main
61
+ - README.txt
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project: googleajax
79
+ rubygems_version: 1.1.1
80
+ signing_key:
81
+ specification_version: 2
82
+ summary: Ruby wrapper to the Google AJAX API REST interfaces(Feeds, Language and Search).
83
+ test_files:
84
+ - test/test_google_ajax.rb