rb-gae-support 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,90 @@
1
+ = rb-gae-support
2
+
3
+ A gem that wraps GAE functionality.
4
+
5
+ Currently provides easier access to the User service, Monkey Patches Net::HTTP
6
+ to work on google app engine, and provides a simple wrapper for the Memcache service.
7
+
8
+ This is very early work, so not very complete, and not production ready.
9
+
10
+ == Usage
11
+
12
+ === Installation
13
+
14
+ Must be installed into JRuby - this gem is useless under normal ruby.
15
+
16
+ jruby -S gem install ruby-gae-support
17
+
18
+ === Net::HTTP
19
+
20
+ In your code:
21
+
22
+ require 'rb-gae-support'
23
+
24
+ as early as possible in your application, to make sure that any usage of
25
+ Net::HTTP is caught.
26
+
27
+ You should then be able to use Net::HTTP and gems that require it like normal.
28
+
29
+ Note: It currently throws java exceptions rather than ruby exceptions - you
30
+ may need to adjust code to suit. Converting these to the expected ruby
31
+ exceptions is on the TODO list.
32
+
33
+ === GAE::User
34
+
35
+ This wraps the user service, which allows you to authenticate users against
36
+ Google accounts, and check if they have been marked as an Administrator in the
37
+ admin console.
38
+
39
+ <b>Get a URL for the login form</b>
40
+
41
+ Call GAE::User.login_url, passing in the URL you wish to return the user to
42
+ once they log in. This will return the URL for the login Form. e.g
43
+ redirect_to GAE::User.login_url('/home')
44
+
45
+ <b>Get a URL to log the user out</b>
46
+
47
+ Call GAE::User.logout_url, passing in the URL to return the user to after
48
+ logging out. This will return the logout URL
49
+
50
+ <b>Check if a User is logged in.</b>
51
+
52
+ Call GAE::User.logged_in? . Will return true if a user is logged in, false if
53
+ not.
54
+
55
+ <b>Check if a User has been set as an Application Admin</>
56
+
57
+ Call GAE::User.admin?
58
+
59
+ <b>Get info about the user</b>
60
+
61
+ Call GAE::User.current. This will return a GAE::User instance, which you can
62
+ call email, nickname and auth_domain on to get those details.
63
+
64
+ === GAE::Memcache
65
+
66
+ Allows you to store & retrieve items in Memcache. Resembles a hash
67
+
68
+ <b>Storing data</b>
69
+
70
+ GAE::Memcache['key'] = 'value'
71
+
72
+ <b>Storing data, with an expiry time</b>
73
+
74
+ # Expires in 1 minute
75
+ GAE::Memcache.put('key', 'value', 60000)
76
+
77
+ <b>Retrieving Data</b>
78
+
79
+ read_data = GAE::Memcache['key']
80
+
81
+ Returns nil if item not found.
82
+
83
+ <b>Deleting data</b>
84
+
85
+ GAE::Memcache.delete('key')
86
+
87
+ == Info
88
+ <b>Author:</b> Lincoln Stoll (lstoll@lstoll.net / http://lstoll.net)
89
+
90
+ <b>License:</b> MIT
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ - wrapper for mail
2
+ - maybe some datastore stuff. seeing how bumble goes.
3
+ - No-RDOC the net:http stuff? Or leave it, and detail the differences.
4
+ - catch java exceptions, convert to expected ruby ones
Binary file
@@ -0,0 +1,38 @@
1
+ require 'java'
2
+
3
+ module GAE
4
+ # Wraps the google app engine memcache service. Can be used like a big hash
5
+ class Memcache
6
+
7
+ # Retrieves an object from the Memcache store by it's key
8
+ def self.[](key)
9
+ get_svc.get(key)
10
+ end
11
+
12
+ # Sets an object into memcache.
13
+ def self.[]=(key,value)
14
+ put(key, value)
15
+ end
16
+
17
+ # Adds an object to the store, otionally setting it's expiry X milliseconds in the future.
18
+ def self.put(key, value, expire_millis=nil)
19
+ if expire_millis
20
+ exp = com.google.appengine.api.memcache.Expiration.byDeltaMillis(expire_millis)
21
+ get_svc.put(key, value, exp)
22
+ else
23
+ get_svc.put(key, value)
24
+ end
25
+ end
26
+
27
+ # Deletes the object from the store
28
+ def self.delete(key)
29
+ get_svc.delete(key)
30
+ end
31
+
32
+ private
33
+ def self.get_svc
34
+ com.google.appengine.api.memcache.MemcacheServiceFactory.getMemcacheService()
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,84 @@
1
+ # We need to monkey patch after these.
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'net/protocol'
5
+ require 'java'
6
+
7
+ module Net
8
+
9
+ # Monkey Patching bundled Net::HTTP Library for java.new.URLConnection
10
+ class HTTP < Protocol
11
+
12
+ # Creates an instance. Ignores proxy config, not needed
13
+ def self.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil)
14
+ # ignore proxy - google don't need it.
15
+ return newobj(address, port)
16
+ end
17
+
18
+ # Contructor - saves address and port
19
+ def initialize(address, port = nil)
20
+ #raise 'got initialize'
21
+ @address = address
22
+ @port = (port || HTTP.default_port)
23
+ end
24
+
25
+ # Actual request. Forms a URL, and uses a java.net.URLConnection to retrieve
26
+ # the body, which it inserts into a Net::HTTPResponse object. Actual
27
+ # network operation is completed here.
28
+ def request(req, body = nil, &block)
29
+ url = @use_ssl ? 'https://' : 'http://' + address + req.path
30
+ jurl = java.net.URL.new(url)
31
+ urlconn = jurl.openConnection()
32
+ inStream = java.io.InputStreamReader.new(urlconn.getInputStream())
33
+ buff = java.io.BufferedReader.new(inStream)
34
+ result = ''
35
+ while line = buff.readLine() do
36
+ result << line
37
+ end
38
+
39
+ resp = HTTPResponse.new("1.1", urlconn.getResponseCode(), urlconn.getResponseMessage())
40
+ resp.body = result
41
+ resp
42
+ end
43
+
44
+ # We are always started
45
+ def started?
46
+ puts 'started called'
47
+ true #always started.
48
+ end
49
+
50
+ # Avoids OpenSSL calls, sets the use SSL value which is checked when
51
+ # generating the URL
52
+ def use_ssl=(flag)
53
+ flag = (flag ? true : false)
54
+ @use_ssl = flag
55
+ end
56
+
57
+ # Does nothing at the moment - avoiding OpenSSL
58
+ def verify_mode=(mode)
59
+ # TODO - check the verify mode, and make sure
60
+ # the URL fetcher uses it.
61
+ end
62
+
63
+ end
64
+
65
+ # Monkey Patching bundled Net::HTTPResponse Library for java.new.URLConnection
66
+ class HTTPResponse
67
+ # This is where the body is put by Net::HTTP#request
68
+ attr_writer :body
69
+
70
+ # Reads the response body. In our case the body has already been set in the
71
+ # request.
72
+ def read_body(dest = nil, &block)
73
+ # TODO - handle block situation better, rather than one dump.
74
+ if block
75
+ yield @body
76
+ else
77
+ @body
78
+ end
79
+ end
80
+
81
+ end
82
+ end
83
+
84
+
@@ -0,0 +1,10 @@
1
+ # This module mocks out OpenSSL functionality to stop net/http erroring.
2
+ module OpenSSL
3
+ # Providing a dummy SSL module
4
+ module SSL
5
+ # Dummy value to prevent errors with Net::HTTP
6
+ VERIFY_NONE = nil
7
+ # Dummy value to prevent errors with Net::HTTP
8
+ VERIFY_PEER = nil
9
+ end
10
+ end
@@ -0,0 +1,49 @@
1
+ module GAE
2
+ # This class represents the User service. It can be used to detemine if
3
+ # a user is logged in, if they are an admin, and retrieve their email,
4
+ # nickname and auth domain. It also provides the login and logout URLs.
5
+ class User
6
+ import com.google.appengine.api.users.UserServiceFactory
7
+
8
+ attr_reader :email, :nickname, :auth_domain
9
+
10
+ # Returns the current logged in user object, or false if not logged in
11
+ def self.current
12
+ # load from GAE and return, set vars
13
+ if UserServiceFactory.user_service.user_logged_in?
14
+ gu = UserServiceFactory.user_service.get_current_user
15
+ return User.new(gu.getEmail, gu.getNickname, gu.getAuthDomain)
16
+ else
17
+ return false
18
+ end
19
+ end
20
+
21
+ # Returns the URL for the login form. Pass in the relative URL you
22
+ # wish to return to when the login is complete
23
+ def self.login_url(return_to)
24
+ UserServiceFactory.user_service.create_login_url(return_to)
25
+ end
26
+
27
+ # Returns the URL to log out the user. Pass in the relative URL you
28
+ # wish to return to when the logout is complete
29
+ def self.logout_url(return_to)
30
+ UserServiceFactory.user_service.create_logout_url(return_to)
31
+ end
32
+
33
+ # Returns true if the user is logged in, false if not
34
+ def self.logged_in?
35
+ UserServiceFactory.user_service.user_logged_in?
36
+ end
37
+
38
+ # Returns true if the user is marked as an admin, false if not.
39
+ def self.admin?
40
+ UserServiceFactory.user_service.user_logged_in? && UserServiceFactory.user_service.user_admin?
41
+ end
42
+
43
+ private
44
+ def initialize(email,nickname,auth_domain)
45
+ @email, @nickname, @auth_domain = email, nickname, auth_domain
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ # This is a wrapper around the google provided services on appengine.
2
+ # It is intended to make the services more 'ruby' like
3
+ #
4
+ # Author:: Lincoln Stoll (mailto:lstoll@lstoll.net)
5
+ # Copyright:: Copyright (c) 2009 Lincoln Stoll
6
+ # License:: MIT
7
+
8
+ require 'java'
9
+ require File.dirname(__FILE__) + '/appengine-api-1.0-sdk-1.2.0.jar'
10
+ directory = File.dirname(__FILE__)
11
+ $:.unshift(directory) unless $:.include?(directory)
12
+
13
+ require 'rb-gae-support/openssl'
14
+ require 'rb-gae-support/user_service'
15
+ require 'rb-gae-support/net_http'
16
+ require 'rb-gae-support/memcache'
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rb-gae-support
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lincoln Stoll
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-14 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Support utilities for ruby apps on Google App engine. Wraps GAE specific functionality, and modifies ruby standard libraries where needed (i.e Net:HTTP) to use GAE libraries
17
+ email: lstoll@lstoll.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.rdoc
26
+ - TODO
27
+ - lib/rb-gae-support
28
+ - lib/rb-gae-support.rb
29
+ - lib/rb-gae-support/net_http.rb
30
+ - lib/rb-gae-support/memcache.rb
31
+ - lib/rb-gae-support/openssl.rb
32
+ - lib/rb-gae-support/user_service.rb
33
+ - lib/appengine-api-1.0-sdk-1.2.0.jar
34
+ has_rdoc: true
35
+ homepage: http://github.com/lstoll/rb-gae-support
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --inline-source
39
+ - --charset=UTF-8
40
+ - --main
41
+ - README.rdoc
42
+ - --title
43
+ - Ruby Goole AppEngine Support
44
+ - README.rdoc
45
+ - lib
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: rb-gae-support
63
+ rubygems_version: 1.3.1
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: rb-gae-support is a support library for ruby apps on Google App Engine
67
+ test_files: []
68
+