Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 7042db113c5a99184101fa9247d923d2262d33d9 Wesley Beary committed May 20, 2008
@@ -0,0 +1,6 @@
+=== 1.0.0 / 2008-05-19
+
+* 1 major enhancement
+
+ * Birthday!
+
@@ -0,0 +1,11 @@
+History.txt
+Manifest.txt
+README.txt
+Rakefile
+lib/google_ajax.rb
+lib/google_ajax/feed.rb
+lib/google_ajax/language.rb
+lib/google_ajax/parser.rb
+lib/google_ajax/search.rb
+lib/google_ajax/version.rb
+test/test_google_ajax.rb
@@ -0,0 +1,59 @@
+= GoogleAjax
+
+* Docs: http://googleajax.rubyforge.org
+* Code: http://github.com/monki/google_ajax/tree/master
+
+== DESCRIPTION:
+
+Ruby wrapper to the Google AJAX API REST interfaces(Feeds, Language and Search).
+
+== SYNOPSIS:
+
+* First, setup referer: GoogleAjax.referer = 'http://mydomain.com'
+* Optionally, you can also set an api_key: GoogleAjax.api_key = 'YOUR_API_KEY'
+* Now you are set to go, so a few examples:
+* * GoogleAjax::Feed.find('ruby') # Find top 10 feeds for 'ruby'
+* * GoogleAjax::Feed.load('http://monki.geemus.com/feed/atom.xml') # Load 4 most recent entries from feed 'http://monki.geemus.com/feed/atom.xml'
+* * GoogleAjax::Feed.lookup('http://monki.geemus.com') # Find feed for 'http://monki.geemus.com'
+* * GoogleAjax::Language.detect('Ciao mondo') # Find the language of the string 'Ciao mondo'
+* * GoogleAjax::Language.translate('Hello world', 'en', 'it') # Translate 'Hello world' to italian
+* * GoogleAjax::Search.blogs('ruby') # Find top 4 blogs for 'ruby'
+* * GoogleAjax::Search.books('ruby') # Find top 4 books for 'ruby'
+* * GoogleAjax::Search.images('ruby') # Find top 4 images for 'ruby'
+* * GoogleAjax::Search.local('ruby', 48.8565, 2.3509) # Find top 4 local results for 'ruby' at latitude 48.8565, longitude 2.3509
+* * GoogleAjax::Search.news('ruby') # Find top 4 news results for 'ruby'
+* * GoogleAjax::Search.video('ruby') # Find top 4 video results for 'ruby'
+* * GoogleAjax::Search.web('Hello world') # Find top 4 web page results for 'Hello world'
+
+== REQUIREMENTS:
+
+* JSON gem to parse the responses
+
+== INSTALL:
+
+* sudo gem install googleajax --include-dependencies
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2008 monki(Wesley Beary) => monki@geemus.com
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,15 @@
+# -*- ruby -*-
+
+require 'rubygems'
+require 'hoe'
+$: << File.dirname(__FILE__) + '/lib'
+require './lib/google_ajax.rb'
+
+Hoe.new('GoogleAjax', GoogleAjax::VERSION::STRING) do |p|
+ p.rubyforge_name = 'googleajax'
+ p.developer('monki(Wesley Beary)', 'monki@geemus.com')
+ p.summary = 'Ruby wrapper to the Google AJAX API REST interfaces(Feeds, Language and Search).'
+ p.extra_deps << ['json', '>= 1.0.0']
+end
+
+# vim: syntax=Ruby
@@ -0,0 +1,40 @@
+require 'cgi'
+require 'open-uri'
+require 'ostruct'
+require 'rubygems'
+require 'json'
+
+require 'google_ajax/feed'
+require 'google_ajax/language'
+require 'google_ajax/parser'
+require 'google_ajax/search'
+require 'google_ajax/version'
+
+class GoogleAjax
+ API_BASE = 'http://ajax.googleapis.com/ajax/services/'
+ @@api_key = @@referer = nil
+
+ def self.api_key
+ @@api_key
+ end
+ def self.api_key=(key)
+ @@api_key = key
+ end
+ def self.referer
+ @@referer
+ end
+ def self.referer=(referer)
+ @@referer = referer
+ end
+
+ def self.get(api, method, query, args = nil)
+ raise "You must assign a value to GoogleAjax.referer" unless @@referer
+ url = "#{API_BASE}#{api}/"
+ url += "#{method}?"
+ url += "&q=#{CGI::escape(query)}"
+ url += "&key=" if @@api_key
+ url += "&" + args.collect {|key, value| "#{key}=#{value}"}.join('&') if args && !args.empty?
+ data = open(url, "Referer" => @@referer).read
+ Parser.parse(api, method, data)
+ end
+end
@@ -0,0 +1,38 @@
+class GoogleAjax
+ class Feed
+ FEED_VERSION = 1.0
+ def self.get(method, query, args = {})
+ args = { :v => FEED_VERSION }.merge!(args)
+ GoogleAjax::get(:feed, method, query, args)
+ end
+
+ # will return a list of feeds that match the given query
+ def self.find(query, args = {})
+ self.get(:find, query)
+ end
+
+ # downloads this feed from Google's servers
+ # Optional: args { :num => number of entries to download(default is 4, maximum is 100) }
+ def self.load(query, args = {})
+ self.get(:load, query, args)
+ end
+
+ # will return the associated feed if it exists for a given url
+ def self.lookup(query, args = {})
+ self.get(:lookup, query)
+ end
+
+ class Entry < OpenStruct
+ def initialize(data)
+ super(data)
+ end
+ end
+
+ class Feed < OpenStruct
+ def initialize(data)
+ super(data)
+ self.entries = entries.collect {|data| Entry.new(data)} if entries
+ end
+ end
+ end
+end
@@ -0,0 +1,82 @@
+class GoogleAjax
+ class Language
+ LANGUAGE_VERSION = 1.0
+ def self.get(method, query, args = {})
+ args = { :v => LANGUAGE_VERSION }.merge!(args)
+ GoogleAjax::get(:language, method, query, args)
+ end
+
+ # will return the language code that describes the language of the given text
+ def self.detect(query, args = {})
+ self.get(:detect, query)
+ end
+
+ # will return translated text for the given text supplied, matching the destination language.
+ def self.translate(query, source, destination, args = {})
+ args = { :langpair => "#{source}%7C#{destination}"}.merge!(args)
+ self.get(:translate, query, args)
+ end
+
+ class Language < OpenStruct
+ def initialize(data)
+ super(data)
+ end
+
+ def name
+ LANGUAGES.invert[self.language]
+ end
+ end
+
+ class Translation < OpenStruct
+ def initialize(data)
+ super(data)
+ end
+ end
+
+ LANGUAGES =
+ {
+ 'ARABIC' => 'ar',
+ 'BULGARIAN' => 'bg',
+ 'CATALAN' => 'ca',
+ 'CHINESE' => 'zh',
+ 'CHINESE_SIMPLIFIED' => 'zh-CN',
+ 'CHINESE_TRADITIONAL' => 'zh-TW',
+ 'CROATIAN' => 'hr',
+ 'CZECH' => 'cs',
+ 'DANISH' => 'da',
+ 'DUTCH'=> 'nl',
+ 'ENGLISH' => 'en',
+ 'ESTONIAN' => 'et',
+ 'FILIPINO' => 'tl',
+ 'FINISH' => 'fi',
+ 'FRENCH' => 'fr',
+ 'GERMAN' => 'de',
+ 'GREK' => 'el',
+ 'HEBREW' => 'iw',
+ 'HINDI' => 'hi',
+ 'HUNGARIAN' => 'hu',
+ 'INDONESIAN' => 'id',
+ 'ITALIAN' => 'it',
+ 'JAPANESE' => 'ja',
+ 'KOREAN' => 'ko',
+ 'LATVIAN' => 'lv',
+ 'LITHUANIAN' => 'lt',
+ 'NORWEGIAN' => 'no',
+ 'PERSIAN' => 'fa',
+ 'POLISH' => 'pl',
+ 'PORTUGUESE' => 'pt-PT',
+ 'ROMANIAN' => 'ro',
+ 'RUSIAN' => 'ru',
+ 'SERBIAN' => 'sr',
+ 'SLOVAK' => 'sk',
+ 'SLOVENIAN' => 'sl',
+ 'SPANISH' => 'es',
+ 'SWEDISH' => 'sv',
+ 'THAI' => 'th',
+ 'TURKISH' => 'tr',
+ 'UKRAINIAN' => 'uk',
+ 'VIETNAMESE' => 'vi',
+ 'UNKNOWN' => ''
+ }
+ end
+end
@@ -0,0 +1,90 @@
+class GoogleAjax
+ class Parser
+ def self.parse(api, method, data)
+ data = JSON.parse(data)
+ Errors.process(data)
+ parser = Parser::PARSERS[api][method]
+ parser.process(data['responseData'])
+ end
+ end
+
+ class FeedFind < Parser#:nodoc:
+ def self.process(data)
+ data['entries'].collect {|data| GoogleAjax::Feed::Feed.new(data)} if data
+ end
+ end
+ class FeedLoad < Parser#:nodoc:
+ def self.process(data)
+ GoogleAjax::Feed::Feed.new(data['feed']) if data
+ end
+ end
+ class FeedLookup < Parser#:nodoc:
+ def self.process(data)
+ GoogleAjax::Feed::Feed.new(data) if data
+ end
+ end
+
+ class LanguageDetect < Parser#:nodoc
+ def self.process(data)
+ GoogleAjax::Language::Language.new(data) if data
+ end
+ end
+ class LanguageTranslate < Parser#:nodoc
+ def self.process(data)
+ GoogleAjax::Language::Translation.new(data) if data
+ end
+ end
+
+ class SearchBlogs < Parser#:nodoc
+ def self.process(data)
+ GoogleAjax::Search::Results.new(data) if data
+ end
+ end
+ class SearchBooks < Parser#:nodoc
+ def self.process(data)
+ GoogleAjax::Search::Results.new(data) if data
+ end
+ end
+ class SearchImages < Parser#:nodoc
+ def self.process(data)
+ GoogleAjax::Search::Results.new(data) if data
+ end
+ end
+ class SearchLocal < Parser#:nodoc
+ def self.process(data)
+ GoogleAjax::Search::Results.new(data) if data
+ end
+ end
+ class SearchNews < Parser#:nodoc
+ def self.process(data)
+ GoogleAjax::Search::Results.new(data) if data
+ end
+ end
+ class SearchVideo < Parser#:nodoc
+ def self.process(data)
+ GoogleAjax::Search::Results.new(data) if data
+ end
+ end
+ class SearchWeb < Parser#:nodoc
+ def self.process(data)
+ GoogleAjax::Search::Results.new(data) if data
+ end
+ end
+
+ class Parser
+ PARSERS = {
+ :feed => { :find => FeedFind, :load => FeedLoad, :lookup => FeedLookup },
+ :language => { :detect => LanguageDetect, :translate => LanguageTranslate, },
+ :search => { :blogs => SearchBlogs, :books => SearchBooks, :images => SearchImages, :local => SearchLocal, :news => SearchNews, :video => SearchVideo, :web => SearchWeb }
+ }
+ end
+
+ class Errors
+ def self.process(data)
+ status = data['responseStatus']
+ unless [200, 201, 202, 203, 204, 205, 206].include? status
+ raise StandardError.new(data['responseDetails'])
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 7042db1

Please sign in to comment.