Permalink
Browse files

initial import of googlebase gem

git-svn-id: http://svn.addictedtonew.com/public/gems/googlebase@125 fe7eae16-9a24-0410-a59d-9e59979e88be
  • Loading branch information...
0 parents commit 3bbd4dbe80d079c1e45308714ce524e4349ee410 jnunemaker committed Nov 20, 2007
@@ -0,0 +1 @@
+* 0.1.0 - initial release
@@ -0,0 +1,20 @@
+Copyright (c) 2007 John Nunemaker
+
+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,20 @@
+History.txt
+License.txt
+Manifest.txt
+README.txt
+Rakefile
+config/hoe.rb
+config/requirements.rb
+lib/google/base.rb
+lib/google/version.rb
+log/debug.log
+script/destroy
+script/generate
+setup.rb
+tasks/deployment.rake
+tasks/environment.rake
+tasks/website.rake
+test/test_googlebase.rb
+test/test_helper.rb
+website/css/common.css
+website/index.html
@@ -0,0 +1,35 @@
+Google Base Class is a base for authenticating to google and making requests to google services.
+
+=Installation
+
+sudo gem install googlebase
+
+=Usage
+
+===Establish A Connection
+
+The code below shows how to use the gem by itself. It checks if username and password are correct (raising Google::LoginError on FAIL) and stores the session id internally. Then you can make requests and the session id is automatically passed in a cookie.
+
+ require 'google/base'
+ Google::Base.establish_connection('username', 'password')
+ Google::Base.get('http://google.com/reader/path/to/whatever/')
+
+===Inheritance
+
+This example takes things a bit farther and shows how to use this class simply as a base to get some methods for free and then wrap whatever google service you would like.
+
+ require 'google/base'
+ Google::Base.establish_connection('username', 'password')
+ module Google
+ module Reader
+ class Base < Google::Base
+ class << self
+ def get_token
+ get("http://www.google.com/reader/api/0/token")
+ end
+ end
+ end
+ end
+ end
+
+ puts Google::Reader::Base.get_token
@@ -0,0 +1,4 @@
+require 'config/requirements'
+require 'config/hoe' # setup Hoe + all gem configuration
+
+Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,71 @@
+require 'google/version'
+
+AUTHOR = 'John' # can also be an array of Authors
+EMAIL = "nunemaker@gmail.com"
+DESCRIPTION = "Base class which handles authentication and requests for google services"
+GEM_NAME = 'googlebase' # what ppl will type to install your gem
+RUBYFORGE_PROJECT = 'googlebase' # The unix name for your project
+HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
+DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
+
+@config_file = "~/.rubyforge/user-config.yml"
+@config = nil
+RUBYFORGE_USERNAME = "jnunemaker"
+def rubyforge_username
+ unless @config
+ begin
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
+ rescue
+ puts <<-EOS
+ERROR: No rubyforge config file found: #{@config_file}
+Run 'rubyforge setup' to prepare your env for access to Rubyforge
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
+ EOS
+ exit
+ end
+ end
+ RUBYFORGE_USERNAME.replace @config["username"]
+end
+
+
+REV = nil
+# UNCOMMENT IF REQUIRED:
+# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
+VERS = Google::VERSION::STRING + (REV ? ".#{REV}" : "")
+RDOC_OPTS = ['--quiet', '--title', 'googlebase documentation',
+ "--opname", "index.html",
+ "--line-numbers",
+ "--main", "README",
+ "--inline-source"]
+
+class Hoe
+ def extra_deps
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
+ @extra_deps
+ end
+end
+
+# Generate all the Rake tasks
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
+hoe = Hoe.new(GEM_NAME, VERS) do |p|
+ p.author = AUTHOR
+ p.description = DESCRIPTION
+ p.email = EMAIL
+ p.summary = DESCRIPTION
+ p.url = HOMEPATH
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
+ p.test_globs = ["test/**/test_*.rb"]
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
+
+ # == Optional
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
+
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
+
+end
+
+CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
+PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
+hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
+hoe.rsync_args = '-av --delete --ignore-errors'
@@ -0,0 +1,17 @@
+require 'fileutils'
+include FileUtils
+
+require 'rubygems'
+%w[rake hoe newgem rubigen].each do |req_gem|
+ begin
+ require req_gem
+ rescue LoadError
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
+ puts "Installation: gem install #{req_gem} -y"
+ exit
+ end
+end
+
+$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
+
+require 'google/base'
@@ -0,0 +1,96 @@
+# Base class that does all the work and can be inherited from for goodies.
+require 'uri'
+require 'net/https'
+require 'net/http'
+
+module Google
+ # Exception raised upon login problem. Most likely incorrect username or
+ # password but could mean problem with google service.
+ class LoginError < Exception; end
+ URL = 'http://www.google.com'
+ LOGIN_URL = 'https://www.google.com:443/accounts/ClientLogin'
+ SOURCE = 'GReader Ruby API'
+
+ class Base
+ class << self
+ # Given an email and password it creates a new connection
+ # which will be used for this class and all sub classes
+ def establish_connection(email, password)
+ @@connection = new(email, password)
+ end
+
+ # Returns the current connection
+ def connection
+ @@connection
+ end
+
+ # Changes the current connection to the one provided
+ def connection=(new_connection)
+ @@connection = new_connection
+ end
+
+ # Makes a get request to a google service using
+ # the session id from the connection's session
+ def get(url)
+ request 'get', url
+ end
+
+ private
+ # This will eventually implement get and post
+ # but I haven't needed post yet
+ def request(method, url)
+ url = URI.parse(url)
+ req = Net::HTTP::Get.new(url.request_uri, @@connection.headers)
+ http = Net::HTTP.new(url.host, url.port)
+ result = http.start() { |conn| conn.request(req) }
+ result.body
+ end
+ end
+
+ # Session id returned from google login request
+ attr_accessor :sid
+
+ # Creates a new instance of the connection class using
+ # the given email and password and attempts to login
+ def initialize(email, password)
+ @email, @password = email, password
+ login
+ end
+
+ # Makes authentication request to google and sets the sid
+ # to be passed in a cookie with each authenticated request.
+ #
+ # Raises Google::LoginError if login is unsuccessful
+ def login
+ url = URI.parse(LOGIN_URL)
+ req = Net::HTTP::Post.new(url.request_uri)
+ req.set_form_data({
+ 'Email' => @email,
+ 'Passwd' => @password,
+ 'source' => SOURCE,
+ 'continue' => URL,
+ })
+ http = Net::HTTP.new(url.host, url.port)
+ http.use_ssl = true
+ result = http.start() { |conn| conn.request(req) }
+ @sid = extract_sid(result.body)
+ raise LoginError, "Most likely your username and password are wrong." unless logged_in?
+ end
+
+ # Returns true or false based on whether or not the session id is set
+ def logged_in?
+ @sid ? true : false
+ end
+
+ # Outputs the headers that are needed to make an authenticated request
+ def headers
+ {'Cookie' => "Name=#{@sid};SID=#{@sid};Domain=.google.com;Path=/;Expires=160000000000"}
+ end
+
+ private
+ def extract_sid(body)
+ matches = body.match(/SID=(.*)/)
+ matches.nil? ? nil : matches[0].gsub('SID=', '')
+ end
+ end
+end
@@ -0,0 +1,9 @@
+module Google #:nodoc:
+ module VERSION #:nodoc:
+ MAJOR = 0
+ MINOR = 1
+ TINY = 0
+
+ STRING = [MAJOR, MINOR, TINY].join('.')
+ end
+end
No changes.
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.join(File.dirname(__FILE__), '..')
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/destroy'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.join(File.dirname(__FILE__), '..')
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/generate'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Generate.new.run(ARGV)
Oops, something went wrong.

0 comments on commit 3bbd4db

Please sign in to comment.