dioxide_rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ = Dioxide RPC Engine for Ruby on Rails
2
+
3
+ This is a server-side library designed for use with mobile applications built with Dioxide-Mobile ( https://github.com/developertown/dioxide-mobile ).
4
+ It provides a framework for RPC services that follow the Dioxide RPC protocol (Protocol documentation coming soon).
5
+
6
+ == Installation
7
+
8
+ 1. Include the dioxide_rails gem in your Gemfile:
9
+
10
+ gem 'dioxide_rails', '~> 0.1'
11
+
12
+ 2. Update the bundler managed gems:
13
+
14
+ $ bundle install
15
+
16
+ 3. Generate the necessary initializer:
17
+
18
+ $ rails g dioxide_rails
19
+ create config/initializers/dioxide_rails.rb
20
+
21
+ 4. Configure Dioxide via the initializer. Configuration documentation is specified within config/initializers/dioxide_rails.rb
22
+
23
+ == Service Implementation
24
+
25
+ Handlers are simply methods that take a Dioxide::RPCRequest object as a parameter, and return an instance of Dioxide::RPCResponse.
26
+ Dioxide::RPCResponse instances are automatically created by calling #new_response on an Dioxide::RPCRequest instance.
27
+ The simplest implementation would look like:
28
+
29
+ class MyHandler
30
+ def ping(req)
31
+ res = req.new_response
32
+ return res
33
+ end
34
+ end
35
+
36
+ Dixoide::RPCResponses default to a status_code of 200 and message of "OK", so this simple handler, in effect, implements a
37
+ ping service.
38
+
39
+ == License
40
+
41
+ Copyright 2011 Developer Town
42
+ Released under the Apache 2.0 License
@@ -0,0 +1,53 @@
1
+ # Copyright 2011 Developer Town
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Dioxide
16
+ # require 'dioxide_rails'
17
+ class ApiController < ApplicationController
18
+
19
+ unloadable
20
+
21
+ def call
22
+ puts "Here's mount_at? #{Dioxide::Engine::config.mount_at}"
23
+
24
+ req = RPCRequest.new(request.body)
25
+
26
+ begin
27
+ #Locate the handler class
28
+ handlerClass = Dioxide::Engine::config.handlers[req.uri]
29
+ logger.debug("Loaded handler class #{handlerClass} for uri #{req.uri}")
30
+
31
+ handler = handlerClass.new
32
+
33
+ method = handler.method(req.method.to_sym)
34
+
35
+ res = method.call req
36
+
37
+ render :json => res
38
+
39
+ rescue Exception => e
40
+ logger.error("Error processing RPCRequest:\n#{req}")
41
+ logger.error e.backtrace.join("\n")
42
+
43
+ res = req.new_response
44
+ res.status_code = 500
45
+ res.message = "Server Error"
46
+
47
+ render :json => res
48
+ return
49
+ end
50
+ end
51
+
52
+ end
53
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,21 @@
1
+ # Copyright 2011 Developer Town
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ Rails.application.routes.draw do |map|
16
+
17
+ mount_at = Dioxide::Engine.config.mount_at
18
+
19
+ match mount_at => 'dioxide/api#call', :via => [:post], :format => :json
20
+
21
+ end
@@ -0,0 +1,27 @@
1
+ # Copyright 2011 Developer Town
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Dioxide
16
+
17
+ # A simple handler class to server as a demonstration handler,
18
+ # and to fulfill a sometimes useful ping functionality, if desired.
19
+ class DioxideHandler
20
+
21
+ # This simple creates a new response and returns it
22
+ # (new responses default to status_code 200 and message 'OK')
23
+ def ping(req)
24
+ req.new_response
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ # Copyright 2011 Developer Town
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require File.dirname(__FILE__) + '/handlers/dioxide_handler'
@@ -0,0 +1,56 @@
1
+ # Copyright 2011 Developer Town
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'dioxide_rails/rpc_response'
16
+
17
+ module Dioxide
18
+ # This class wraps up an RPC Request of the following form (represented here as a hash):
19
+ #
20
+ # {
21
+ # :request_id => 0,
22
+ # :uri => "/users",
23
+ # :method => "create",
24
+ # :device_id => "235234-2342-234-234-2342",
25
+ # :session_id => "asdf980808as0df090a9d",
26
+ # :payload => {foo: 'bar'}
27
+ # }
28
+
29
+ class RPCRequest
30
+ attr_reader :request_id, :uri, :method, :device_id, :session_id, :payload
31
+
32
+ def initialize(req)
33
+ if (req.class != Hash)
34
+ req = ActiveSupport::JSON.decode(req)
35
+ end
36
+
37
+ raise Exception.new("RPC Request is missing required parameter: uri") unless req.has_key?("uri")
38
+ raise Exception.new("RPC Request is missing required parameter: method") unless req.has_key?("method")
39
+
40
+ @request_id = req["request_id"]
41
+ @uri = req["uri"]
42
+ @method = req["method"]
43
+ @device_id = req["device_id"]
44
+ @session_id = req["session_id"]
45
+ @payload = req["payload"]
46
+ end
47
+
48
+ def to_s
49
+ to_json
50
+ end
51
+
52
+ def new_response
53
+ RPCResponse.new(@request_id)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright 2011 Developer Town
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Dioxide
16
+ # This class wraps up an RPC Response of the following form (represented here as a hash):
17
+ #
18
+ # {
19
+ # :request_id => 0,
20
+ # :status_code => 200,
21
+ # :message => "OK",
22
+ # :payload => {foo: 'bar'}
23
+ # }
24
+
25
+ class RPCResponse
26
+
27
+ attr_accessor :request_id, :status_code, :message, :payload
28
+
29
+ def initialize(request_id)
30
+ @request_id = request_id
31
+ @status_code = 200
32
+ @message = "OK"
33
+ @payload = {}
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ # Copyright 2011 Developer Town
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Dioxide
16
+ require 'engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
17
+ require 'dioxide_rails/rpc_request'
18
+ require 'dioxide_rails/rpc_response'
19
+ require 'dioxide_rails/handlers'
20
+ end
21
+
data/lib/engine.rb ADDED
@@ -0,0 +1,37 @@
1
+ # Copyright 2011 Developer Town
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'dioxide_rails'
16
+ require 'rails'
17
+ require 'action_controller'
18
+
19
+ module Dioxide
20
+ class Engine < Rails::Engine
21
+
22
+ # Config defaults
23
+ config.mount_at = '/api'
24
+
25
+ # Load rake tasks
26
+ rake_tasks do
27
+ load File.join(File.dirname(__FILE__), 'rails/railties/tasks.rake')
28
+ end
29
+
30
+ # Check the gem config
31
+ initializer "check config" do |app|
32
+
33
+ # make sure mount_at ends with trailing slash
34
+ config.mount_at += '/' unless config.mount_at.last == '/'
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ # Copyright 2011 Developer Town
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'rails/generators'
16
+ require 'rails/generators/migration'
17
+
18
+ class DioxideRailsGenerator < Rails::Generators::Base
19
+
20
+ def self.source_root
21
+ File.join(File.dirname(__FILE__), 'templates')
22
+ end
23
+
24
+ def copy_initializer_file
25
+ copy_file 'initializer.rb', 'config/initializers/dioxide_rails.rb'
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ module Dioxide
2
+ class Engine < Rails::Engine
3
+
4
+ # Step 1:
5
+ # Configure the mount point for the RPC API handler
6
+ config.mount_at = '/api'
7
+
8
+
9
+ # Step 2:
10
+ # Add any necessary require statements here to include your handler classes
11
+ # e.g., for a handler class in RAILS_ROOT/lib/users_handler.rb:
12
+
13
+ # require 'users_handler'
14
+
15
+ # Step 3:
16
+ # Map URIs to handlers
17
+ config.handlers = {
18
+ "/dioxide" => Dioxide::DioxideHandler,
19
+ #"/users" => ::UsersHandler
20
+ }
21
+
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dioxide_rails
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Jason Vasquez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-10 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email: admin@developertown.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README.rdoc
25
+ files:
26
+ - app/controllers/dioxide/api_controller.rb
27
+ - config/routes.rb
28
+ - lib/dioxide_rails.rb
29
+ - lib/dioxide_rails/handlers.rb
30
+ - lib/dioxide_rails/handlers/dioxide_handler.rb
31
+ - lib/dioxide_rails/rpc_request.rb
32
+ - lib/dioxide_rails/rpc_response.rb
33
+ - lib/engine.rb
34
+ - lib/rails/generators/dioxide_rails/dioxide_rails_generator.rb
35
+ - lib/rails/generators/dioxide_rails/templates/initializer.rb
36
+ - README.rdoc
37
+ has_rdoc: true
38
+ homepage:
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.5.0
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Ruby On Rails implementation of the Dioxide RPC framework
65
+ test_files: []
66
+