rest_engine 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Yehezkiel Syamsuhadi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = rest_engine
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to rest_engine
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Yehezkiel Syamsuhadi. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,4 @@
1
+ module RestEngine
2
+ class ApplicationController < ::ApplicationController
3
+ end
4
+ end
@@ -0,0 +1,73 @@
1
+ require 'application_controller'
2
+
3
+ module RestEngine
4
+ class MainController < RestEngine::ApplicationController
5
+ before_filter :get_model
6
+
7
+ def list
8
+ options = {}
9
+ if params[:page]
10
+ page = params[:page].to_i
11
+ page = 1 if page <= 0
12
+
13
+ options[:limit] = params[:limit].to_i
14
+ options[:offset] = (page - 1) * options[:limit]
15
+ end
16
+
17
+ items = @model.all(options)
18
+
19
+ respond(:success => true, :data => items)
20
+ end
21
+
22
+ def show
23
+ item = @model.find(params[:id])
24
+ respond(:success => true, :data => item)
25
+ end
26
+
27
+ def destroy
28
+ item = @model.find(params[:id])
29
+ if item.destroy
30
+ respond(:success => true, :message => "Destroyed #{@model_name} #{item.id}")
31
+ else
32
+ respond(:success => false, :message => "Failed to destroy #{@model_name} #{item.id}")
33
+ end
34
+ end
35
+
36
+ def create
37
+ item = @model.new(request.POST)
38
+ if item.save
39
+ respond(:success => true, :message => "Created new #{@model_name} #{item.id}", :data => item)
40
+ else
41
+ respond(:success => false, :message => "Failed to create #{@model_name}")
42
+ end
43
+ end
44
+
45
+ def update
46
+ item = @model.find(params[:id])
47
+ if item.update_attributes(request.POST)
48
+ respond(:success => true, :message => "Updated #{@model_name} #{item.id}", :data => item)
49
+ else
50
+ respond(:success => false, :message => "Failed to update #{@model_name}")
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def respond data
57
+ ActiveRecord::Base.include_root_in_json = false
58
+ respond_to do |format|
59
+ format.any(:xml, :json) { render(request.format.to_sym => data) }
60
+ end
61
+ end
62
+
63
+ def get_model
64
+ @model_name = to_model_name(params[:resources])
65
+ @model = @model_name.constantize
66
+ end
67
+
68
+ def to_model_name(resources_name)
69
+ parts = resources_name.split('~')
70
+ parts.map { |x| x == parts.last ? x.classify : x.camelize }.join('::')
71
+ end
72
+ end
73
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,11 @@
1
+ Rails.application.routes.draw do
2
+ scope 'api', :as => 'rest_engine' do
3
+ controller 'rest_engine/main' do
4
+ get '/:resources', :to => :list, :as => 'list'
5
+ post '/:resources', :to => :create, :as => 'create'
6
+ get '/:resources/:id', :to => :show, :as => 'show'
7
+ put '/:resources/:id', :to => :update, :as => 'update'
8
+ delete '/:resources/:id', :to => :destroy, :as => 'destroy'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ require 'rest_engine'
2
+ require 'rails'
3
+
4
+ module RestEngine
5
+ class Engine < Rails::Engine
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ require 'rest_engine/engine'
2
+
3
+ module RestEngine
4
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest_engine
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Yehezkiel Syamsuhadi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-25 00:00:00 +10:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 3.0.9
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.3.3
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.3.0
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec-rails
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 2.3.1
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: bundler
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 1.0.15
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: jeweler
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 1.6.2
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: rcov
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id007
93
+ - !ruby/object:Gem::Dependency
94
+ name: factory_girl
95
+ requirement: &id008 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ~>
99
+ - !ruby/object:Gem::Version
100
+ version: 1.3.3
101
+ type: :development
102
+ prerelease: false
103
+ version_requirements: *id008
104
+ description:
105
+ email: yehezkielbs@gmail.com
106
+ executables: []
107
+
108
+ extensions: []
109
+
110
+ extra_rdoc_files:
111
+ - LICENSE.txt
112
+ - README.rdoc
113
+ files:
114
+ - app/controllers/rest_engine/application_controller.rb
115
+ - app/controllers/rest_engine/main_controller.rb
116
+ - config/routes.rb
117
+ - lib/rest_engine.rb
118
+ - lib/rest_engine/engine.rb
119
+ - LICENSE.txt
120
+ - README.rdoc
121
+ has_rdoc: true
122
+ homepage: http://github.com/yehezkielbs/rest_engine
123
+ licenses:
124
+ - MIT
125
+ post_install_message:
126
+ rdoc_options: []
127
+
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ hash: -2714594228656389374
136
+ segments:
137
+ - 0
138
+ version: "0"
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: "0"
145
+ requirements: []
146
+
147
+ rubyforge_project:
148
+ rubygems_version: 1.6.2
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: A Rails 3 Engine to provide a restful API
152
+ test_files: []
153
+