Permalink
Browse files

Merge branch 'v1candidate'

  • Loading branch information...
2 parents 240bc56 + 30440ea commit 01f4dbe728bc5475d290077fb18f2576ad5e006a Wesley Beary committed Dec 25, 2009
View
@@ -1,3 +1,14 @@
+=== 1.0.0 / 2009-12-17
+
+* Incompatible changes:
+ * Returns Hashes instead of OpenStruct by default (require googleajax/as_open_struct if you need OpenStruct)
+ * Indices are symbols with underscores instead of camelcased string, (e.g. :estimated_count instead of "estimatedCount")
+ * Values are converted automatically for Floats, Integers and true and false.
+
+* translate accepts :english, :french, etc... as source or destination languages in addition to the language codes
+
+* Much more flexible structure.
+
=== 0.2.0 / 2009-11-17
* New features:
View
@@ -9,6 +9,8 @@ Ruby wrapper for Google AJAX API REST interfaces(Feeds, Language and Search).
== SYNOPSIS:
+ require "googleajax"
+
# First, setup referer:
GoogleAjax.referer = [your domain name here]
@@ -53,6 +55,37 @@ Ruby wrapper for Google AJAX API REST interfaces(Feeds, Language and Search).
# Find top 4 web page results for 'Hello world'
GoogleAjax::Search.web('Hello world')
+ # Find top 10 feeds for 'ruby'
+ GoogleAjax::Feed.find('ruby')
+
+
+
+=== Results
+
+Results from the methods are hashes, automatically converted from the response from Google.
+
+For example:
+
+ # Find top 4 web page results for 'Hello world'
+ GoogleAjax::Search.web('Hello world')
+ # => { :results=>[
+ # { :title=>"<b>Hello world</b> program - Wikipedia, the free encyclopedia",
+ # # ... more data ...
+ # :url=>"http://en.wikipedia.org/wiki/Hello_world_program"
+ # },
+ # # ... more results
+ # ],
+ # :cursor=>{
+ # :current_page_index=>0,
+ # # ... more data ...
+ # :estimated_result_count=>57800000
+ # }
+ # }
+
+When possible, the result is simplified:
+* Feed.find will return an array instead of {:entries => array}
+* Feed.load will return the feed instead of {:feed => feed}
+
== REQUIREMENTS:
* JSON gem to parse responses, or rails
@@ -66,6 +99,7 @@ Ruby wrapper for Google AJAX API REST interfaces(Feeds, Language and Search).
(The MIT License)
Copyright (c) 2009 {geemus (Wesley Beary)}[http://github.com/geemus]
+and {Marc-Andre Lafortune}[http://github.com/marcandre]
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
@@ -1 +1 @@
-0.2.0
+1.0.0
View
@@ -1,31 +1,23 @@
require 'cgi'
require 'open-uri'
-require 'ostruct'
require 'rubygems'
require 'json' unless defined?(Rails)
-module GoogleAjax
- require 'googleajax/base' unless const_defined?("Base")
- require 'googleajax/feed'
- require 'googleajax/language'
- require 'googleajax/search'
- require 'googleajax/parser'
+# The following extensions are standard in either Ruby 1.8.7, 1.9 or rails:
+require 'googleajax/extensions/kernel'
+require 'googleajax/extensions/string'
+require 'googleajax/extensions/hash'
- API_BASE = 'http://ajax.googleapis.com/ajax/services/'
- class << self
- attr_accessor :api_key
- attr_accessor :referer
+# GoogleAjax lib:
+require 'googleajax/hash'
+require 'googleajax/filters'
+require 'googleajax/api'
+require 'googleajax/request'
+require 'googleajax/base' unless GoogleAjax.const_defined?("Base")
+require 'googleajax/results'
+require 'googleajax/feed'
+require 'googleajax/language'
+require 'googleajax/search'
- # Api doc is at http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje
- def 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=#{api_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
-end
+# Final touch
+GoogleAjax.extend GoogleAjax::Request
View
@@ -0,0 +1,57 @@
+module GoogleAjax
+ # The API module provides generic methods to call the Google API
+ # These are specialized in the different APIs (Language, Search & Feed)
+ module API
+
+ # All APIs have a default version of 1.0 (override otherwise)
+ def version
+ 1.0
+ end
+
+ # All APIs have, by default version a kind corresponding to their name (e.g. "language")
+ def kind
+ @kind ||= name.demodulize.downcase.to_sym
+ end
+
+ # Sends the request to google and converts it to Ruby
+ # A block can be passed to alter the received hash
+ def get(method, query, args = {})
+ args = { :v => version }.merge(args)
+ response = GoogleAjax::get(kind, method, query, args)
+ data = extract_data(
+ parse(response)
+ )
+ data = yield data if block_given?
+ # Convert hashes to an instance of ourself from this data:
+ Filters::Recursive.remap(data, self)
+ end
+
+ def parse(data)
+ if defined? Rails
+ ActiveSupport::JSON::decode(data)
+ else
+ JSON.parse(data)
+ end
+ end
+
+ # verifies the responses' status and either raises an error
+ # or else returns the response data.
+ def extract_data(hash)
+ status = hash['responseStatus']
+ unless (200..206).include? status
+ raise StandardError, hash['responseDetails'] || "Wrong status code (#{status.inspect})"
+ end
+ hash['responseData']
+ end
+
+ # Since most api have the same form, this helper can create these calls easily.
+ # If a block is given, it will be passed to #get
+ def standard_api(*methods, &block)
+ methods.each do |method|
+ define_singleton_method(method) do |*args|
+ get(method, *args, &block)
+ end
+ end
+ end
+ end
+end
View
@@ -1,15 +1,2 @@
-# Include this file instead of googleajax so that results are simply hashes
-
-module GoogleAjax
- class Base < Hash
- def initialize(h)
- super()
- replace(h)
- end
-
- def self.remap(h)
- end
- end
-end
-
-require 'googleajax'
+warn "requiring 'googleajax/as_hash' is deprecated. Require 'googleajax' instead"
+require 'googleajax'
@@ -0,0 +1,20 @@
+# If you prefer getting back OpenStructs instead of Hashes,
+# then require 'googleajax/as_open_struct' *instead* of 'googleajax'
+
+# These OpenStruct will also have the [] and []= operators defined
+require 'ostruct'
+require 'googleajax/filters'
+module GoogleAjax
+ class Base < ::OpenStruct
+ include Filters::Default
+
+ def [](key)
+ send(key)
+ end
+
+ def []=(key, value)
+ send("#{key}=", value)
+ end
+ end
+end
+require 'googleajax'
View
@@ -1,25 +1,6 @@
module GoogleAjax
- class Base < OpenStruct
- def [](key)
- send(key)
- end
-
- def []=(key, value)
- send("#{key}=", value)
- end
-
- def self.remap(h)
- define_method(:initialize) do |arg|
- super(arg)
- h.each do |key, klass|
- case self[key]
- when Array
- self[key] = self[key].map(&klass.method(:new))
- when Hash
- self[key] = klass.new(self[key])
- end
- end
- end
- end
+ # See Results
+ class Base < Hash
+ include Filters::Default
end
-end
+end
View
@@ -0,0 +1,9 @@
+# If you prefer getting unfiltered Hashes,
+# then require 'googleajax/basic' *instead* of 'googleajax'
+# (not recommended)
+require 'googleajax/hash'
+module GoogleAjax
+ class Base < Hash
+ end
+end
+require 'googleajax'
@@ -0,0 +1,21 @@
+class Hash
+ # From the 'backports' gem
+ # New Ruby 1.8.7+ constructor -- not documented, see redmine # 1385
+ # <tt>Hash[[[:foo, :bar],[:hello, "world"]]] ==> {:foo => :bar, :hello => "world"}</tt>
+ unless (Hash[[[:test, :test]]] rescue false)
+ class << self
+ alias_method :constructor_without_key_value_pair_form, :[]
+ def [](*args)
+ return constructor_without_key_value_pair_form(*args) unless args.length == 1 && args.first.is_a?(Array)
+ h = {}
+ args.first.each do |arr|
+ next unless arr.respond_to? :to_ary
+ arr = arr.to_ary
+ next unless (1..2).include? arr.size
+ h[arr.at(0)] = arr.at(1)
+ end
+ h
+ end
+ end
+ end
+end
@@ -0,0 +1,9 @@
+module Kernel
+ # From the backports gem
+ # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
+ def define_singleton_method(*args, &block)
+ class << self
+ self
+ end.send(:define_method, *args, &block)
+ end unless method_defined? :define_singleton_method
+end
@@ -0,0 +1,15 @@
+class String
+ # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
+ def underscore
+ gsub(/::/, '/').
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
+ tr("-", "_").
+ downcase
+ end unless method_defined? :underscore
+
+ # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
+ def demodulize
+ gsub(/^.*::/, '')
+ end unless method_defined? :demodulize
+end
View
@@ -1,35 +1,24 @@
module GoogleAjax
- module Feed
- FEED_VERSION = 1.0
- # Arguments: http://code.google.com/apis/ajaxfeeds/documentation/reference.html#_intro_fonje
- def self.get(method, query, args)
- args = { :v => FEED_VERSION }.merge(args)
- GoogleAjax::get(:feed, method, query, args)
- end
-
+ class Feed < Results
+ ##
+ # :call-seq:
+ # find(query, args = {})
# will return a list of feeds that match the given query
# Arguments: http://code.google.com/apis/ajaxfeeds/documentation/reference.html#_fonje_find
- def self.find(query, args = {})
- self.get(:find, query, args)
- end
+ standard_api(:find){|h| h['entries']}
+ ##
+ # :call-seq:
+ # load(url, args = {})
# downloads this feed from Google's servers
# Arguments: http://code.google.com/apis/ajaxfeeds/documentation/reference.html#_fonje_load
- def self.load(url, args = {})
- self.get(:load, url, args)
- end
+ standard_api(:load){|h| h['feed']}
+ ##
+ # :call-seq:
+ # lookup(url, args = {})
# will return the associated feed if it exists for a given url
# Arguments: http://code.google.com/apis/ajaxfeeds/documentation/reference.html#_intro_fonje
- def self.lookup(url, args = {})
- self.get(:lookup, url, args)
- end
-
- class Entry < Base
- end
-
- class Feed < Base
- remap :entries => Entry
- end
+ standard_api :lookup
end
end
Oops, something went wrong.

0 comments on commit 01f4dbe

Please sign in to comment.