rammer 1.1.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. data/Gemfile +27 -0
  2. data/MODULE_FILES +34 -0
  3. data/README.md +17 -5
  4. data/Rakefile +37 -0
  5. data/bin/rammer +2 -3
  6. data/bin/viber +37 -38
  7. data/lib/modules/authentication/authentication_apis.rb +64 -0
  8. data/lib/modules/authorization/authorization_apis.rb +89 -0
  9. data/lib/modules/common/Gemfile +43 -0
  10. data/lib/{template → modules/common}/Gemfile.lock +0 -0
  11. data/lib/{template → modules/common}/Procfile +0 -0
  12. data/lib/{template → modules/common}/Rakefile +27 -0
  13. data/lib/modules/common/application.rb +48 -0
  14. data/lib/{template → modules/common}/database.yml +1 -1
  15. data/lib/modules/common/server.rb +38 -0
  16. data/lib/modules/common/tree.rb +30 -0
  17. data/lib/{template → modules/migrations}/01_create_users.rb +27 -0
  18. data/lib/modules/migrations/02_create_sessions.rb +36 -0
  19. data/lib/modules/migrations/03_create_owners.rb +40 -0
  20. data/lib/modules/migrations/04_create_oauth2_authorizations.rb +50 -0
  21. data/lib/modules/migrations/05_create_oauth2_clients.rb +45 -0
  22. data/lib/modules/models/oauth2_authorization.rb +203 -0
  23. data/lib/modules/models/oauth2_client.rb +216 -0
  24. data/lib/modules/models/owner.rb +65 -0
  25. data/lib/modules/models/session.rb +30 -0
  26. data/lib/modules/models/user.rb +135 -0
  27. data/lib/modules/oauth/oauth_apis.rb +92 -0
  28. data/lib/rammer/module_generator.rb +236 -0
  29. data/lib/rammer/rammer_generator.rb +160 -0
  30. data/lib/rammer/version.rb +28 -1
  31. data/lib/rammer.rb +24 -278
  32. data/rammer.gemspec +37 -24
  33. data/test/helper.rb +49 -0
  34. data/test/test_rammer_root_structure.rb +80 -0
  35. data/test/test_viber_module_plugin.rb +104 -0
  36. data/test/test_viber_module_unplug.rb +87 -0
  37. metadata +77 -29
  38. data/lib/template/02_create_sessions.rb +0 -9
  39. data/lib/template/03_create_owners.rb +0 -13
  40. data/lib/template/04_create_oauth2_authorizations.rb +0 -23
  41. data/lib/template/05_create_oauth2_clients.rb +0 -18
  42. data/lib/template/Gemfile +0 -16
  43. data/lib/template/application.rb +0 -21
  44. data/lib/template/authentication_apis.rb +0 -35
  45. data/lib/template/authorization_apis.rb +0 -59
  46. data/lib/template/oauth2_authorization.rb +0 -113
  47. data/lib/template/oauth2_client.rb +0 -100
  48. data/lib/template/oauth_apis.rb +0 -138
  49. data/lib/template/owner.rb +0 -10
  50. data/lib/template/server.rb +0 -11
  51. data/lib/template/session.rb +0 -3
  52. data/lib/template/tree.rb +0 -3
  53. data/lib/template/user.rb +0 -78
@@ -0,0 +1,135 @@
1
+ =begin
2
+ **************************************************************************
3
+ * The MIT License (MIT)
4
+
5
+ * Copyright (c) 2013-2014 QBurst Technologies Inc.
6
+
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ * THE SOFTWARE.
24
+
25
+ **************************************************************************
26
+ =end
27
+
28
+ class User < ActiveRecord::Base
29
+ =begin
30
+ Validation method for all params.
31
+ =end
32
+ def self.validate_params?(params,base_api)
33
+ case(base_api)
34
+ when "register"
35
+ if params.name && params.redirect_uri && params.callback_url && !params.callback_url.empty?
36
+ return true if User.valid_redirect_uri?(params.redirect_uri)
37
+ end
38
+ when "sign_up", "sign_in"
39
+ if params.email && params.password && params.redirect_uri
40
+ return true if User.valid_email?(params.email) && User.valid_password?(params.password) && User.valid_redirect_uri?(params.redirect_uri)
41
+ end
42
+ when "sign_out"
43
+ if params.email && params.session_token && params.redirect_uri
44
+ return true if User.valid_email?(params.email) && User.valid_redirect_uri?(params.redirect_uri)
45
+ end
46
+ when "access_token"
47
+ if params.username && params.redirect_uri && params.request_token
48
+ return true if User.validate_oauth_params(params,base_api)
49
+ end
50
+ when "authorize", "request_token"
51
+ if params.username && params.redirect_uri
52
+ return true if User.validate_oauth_params(params,base_api)
53
+ end
54
+ when "token", "invalidate_token"
55
+ if params.host_name && params.authorization && params.redirect_uri
56
+ return true if User.validate_oauth_params(params,base_api)
57
+ end
58
+ else
59
+ return false
60
+ end
61
+ end
62
+ =begin
63
+ Validation method for oauth params.
64
+ =end
65
+ def self.validate_oauth_params(params,base_api)
66
+ if params.client_id && params.response_type && User.valid_redirect_uri?(params.redirect_uri)
67
+ if params.response_type == "code" && (base_api == "request_token" || base_api == "authorize")
68
+ return true
69
+ elsif params.response_type == "token" && base_api != "request_token"
70
+ return true
71
+ end
72
+ end
73
+ end
74
+ =begin
75
+ Creates a user and returns session id.
76
+ =end
77
+ def self.sign_up(params)
78
+ @user = User.create!(:email => params.email, :encrypted_password => params.password)
79
+ @session = @user.sign_in(params)
80
+ return @user, @session
81
+ end
82
+ =begin
83
+ Validation method for email params.
84
+ =end
85
+ def self.valid_email?(email)
86
+ return true if email =~ RubyRegex::Email
87
+ end
88
+ =begin
89
+ Validation method for password params.
90
+ =end
91
+ def self.valid_password?(password)
92
+ return true if password =~ /^[0-9a-f]{32}$/i
93
+ end
94
+ =begin
95
+ Validation method for redirect uri params.
96
+ =end
97
+ def self.valid_redirect_uri?(redirect_uri)
98
+ return true if !redirect_uri.empty? && redirect_uri =~ RubyRegex::Url
99
+ end
100
+ =begin
101
+ Enables a user log in and returns session id.
102
+ =end
103
+ def sign_in(params)
104
+ token = Digest::SHA1.hexdigest("#{SecureRandom.base64}" + "#{self.id}")
105
+ @session = Session.create!(:user_id => self.id, :session_token => token)
106
+ self.update_attribute(:sign_in_count, self.sign_in_count+1)
107
+ return @session
108
+ end
109
+ =begin
110
+ Enables user sign out.
111
+ =end
112
+ def sign_out(params)
113
+ @session = Session.find_by_session_token_and_user_id(params.session_token,self.id)
114
+ @session.destroy
115
+ end
116
+ =begin
117
+ Checks for user sign in.
118
+ =end
119
+ def signed_in?(params)
120
+ Session.find_by_user_id_and_session_token(self.id,params.session_token)
121
+ end
122
+ =begin
123
+ Checks or user sign in using session id for oauth endpoints.
124
+ =end
125
+ def self.logged_in?(params)
126
+ Session.find_by_session_token(params.username)
127
+ end
128
+ =begin
129
+ Builds the redirect uri for returning session token.
130
+ =end
131
+ def redirect_url(params,session)
132
+ redirect_to_url = params.redirect_uri + "?session_token=#{session.session_token}"
133
+ end
134
+
135
+ end
@@ -0,0 +1,92 @@
1
+ =begin
2
+ **************************************************************************
3
+ * The MIT License (MIT)
4
+
5
+ * Copyright (c) 2013-2014 QBurst Technologies Inc.
6
+
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ * THE SOFTWARE.
24
+
25
+ **************************************************************************
26
+ =end
27
+
28
+ require 'oauth2'
29
+ require 'songkick/oauth2/provider'
30
+ require 'oauth'
31
+ require 'ruby_regex'
32
+
33
+ module Rammer
34
+
35
+ class OauthApis < Grape::API
36
+ Songkick::OAuth2::Provider.realm = 'PocketAPI Oauth Server'
37
+ version 'v1', :using => :path
38
+ format :json
39
+
40
+ =begin
41
+ This handles api calls for oauth requests with the request parameters according to each api called:
42
+ For register --> This handles api calls for request token generation with the request parameters:
43
+ {"name"=> Client's name,
44
+ "redirect_uri" => URL to which the oauth should be redirected,
45
+ "callback_url" => Url to which the details should be redirected
46
+ }
47
+ For request_token --> This handles api calls for request token generation with the request parameters:
48
+ {"client_id"=> Client's registered ID,
49
+ "username" => Authorized user's session id,
50
+ "redirect_uri" => URL to which the oauth should be redirected,
51
+ "response_type" => "code" (Keyword to return request token)
52
+ }
53
+ For authorize --> This handles api calls for request token generation with the request parameters:
54
+ {"client_id"=> Client's registered ID,
55
+ "username" => Authorized user's session id,
56
+ "redirect_uri" => URL to which the oauth should be redirected,
57
+ "response_type" => "code" (Keyword to return request token),
58
+ }
59
+ For access_token --> This handles api calls for access token generation with the request parameters:
60
+ {"client_id"=> Client's registered ID,
61
+ "username" => Authorized user's session id,
62
+ "redirect_uri" => URL to which the oauth should be redirected,
63
+ "response_type" => "token" (Keyword to return access token),
64
+ "request_token" => "Request token received from /request_token endpoint."
65
+ }
66
+ Optional parameters:
67
+ {"scope" => Indicates the API's the application is requesting,
68
+ "duration" => Lifetime of bearer token
69
+ }
70
+ For token --> This handles api calls for bearer token generation with the request parameters:
71
+ {"client_id"=> Client's registered ID,
72
+ "authorization" => Basic authorization key generated while client registration,
73
+ "host_name" => Thirs party client's name,
74
+ "redirect_uri" => URL to which the oauth should be redirected,
75
+ "response_type" => "token" (Keyword for invalidation of bearer token only),
76
+ }
77
+ For invalidate_token --> This handles api calls for invalidating the bearer token generated with the request parameters:
78
+ {"client_id"=> Client's registered ID,
79
+ "authorization" => Basic authorization key generated while client registration,
80
+ "host_name" => Thirs party client's name,
81
+ "redirect_uri" => URL to which the oauth should be redirected,
82
+ "response_type" => "token" (Keyword for invalidation of bearer token only),
83
+ }
84
+ =end
85
+ [:get, :post].each do |method|
86
+ __send__ method, '/oauth/:oauth_request' do
87
+ expected_response,response_message = Oauth2Authorization.api_call(params,env,params[:oauth_request])
88
+ if response_message then redirect expected_response else expected_response end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,236 @@
1
+ =begin
2
+ **************************************************************************
3
+ * The MIT License (MIT)
4
+
5
+ * Copyright (c) 2013-2014 QBurst Technologies Inc.
6
+
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ * THE SOFTWARE.
24
+
25
+ **************************************************************************
26
+ =end
27
+
28
+ require "rammer/version"
29
+ require 'fileutils'
30
+
31
+ $gem_file_name = "rammer-"+Rammer::VERSION
32
+
33
+ module Rammer
34
+ =begin
35
+ Generator class for mounting the rammer modules. It mounts the module, creates required files and configurations ,
36
+ and the runs bundle install.
37
+ =end
38
+ class ModuleGenerator
39
+ attr_accessor :target_dir, :project_name, :module_class, :module_name, :gem_path, :action
40
+ AUTH_MIGRATE = ['01_create_users.rb','02_create_sessions.rb']
41
+ OAUTH_MIGRATE = ['03_create_owners.rb', '04_create_oauth2_authorizations.rb', '05_create_oauth2_clients.rb']
42
+ AUTH_MODELS = ['user.rb', 'session.rb', 'oauth2_authorization.rb']
43
+ OAUTH_MODELS = ['oauth2_client.rb', 'owner.rb']
44
+
45
+ =begin
46
+ Initiliazes the following attributes :
47
+ project_name (application name), module_class (class name for the application), target_dir (new application path),
48
+ module_name (rammer module name), action (viber action to plugin or unplug) and gem_path (path at which the gem is installed)
49
+ =end
50
+ def initialize(options)
51
+ self.project_name = options[:project_name]
52
+ self.module_class = options[:module_class]
53
+ self.module_name = options[:module_name]
54
+ self.action = options[:action]
55
+ self.target_dir = Dir.pwd
56
+ path = `gem which rammer`
57
+ self.gem_path = path.split($gem_file_name,2).first + "/" + $gem_file_name
58
+ end
59
+
60
+ =begin
61
+ Creates the required files and configuration setup while module plugin or unplug.
62
+ =end
63
+ def run
64
+ case action
65
+ when "-p","-plugin"
66
+ flag = require_module_to_base
67
+ mount_module unless flag
68
+ copy_module
69
+ create_migrations_and_models
70
+ add_gems
71
+ oauth_message if module_name == "oauth" && !flag
72
+ when "-u","-unplug"
73
+ unmount_module
74
+ end
75
+ end
76
+
77
+ =begin
78
+ Mounts the module onto the application.
79
+ =end
80
+ def mount_module
81
+ file = File.open("#{target_dir}/app/apis/#{project_name}/base.rb", "r+")
82
+ file.each do |line|
83
+ while line == "\tclass Base < Grape::API\n" do
84
+ pos = file.pos
85
+ rest = file.read
86
+ file.seek pos
87
+ file.write("\t\tmount ")
88
+ file.puts(module_class)
89
+ file.write(rest)
90
+ break
91
+ end
92
+ end
93
+ $stdout.puts "\e[1;35m\tmounted\e[0m\t#{module_class}"
94
+ end
95
+
96
+ =begin
97
+ Checks whether the module is already mounted and if not then configures for mounting.
98
+ =end
99
+ def require_module_to_base
100
+ file = File.open("#{target_dir}/app/apis/#{project_name}/base.rb", "r+")
101
+ file.each do |line|
102
+ while line == "require_relative './modules/#{module_name}_apis'\n" do
103
+ $stdout.puts "\e[33mModule already mounted.\e[0m"
104
+ return true
105
+ end
106
+ end
107
+
108
+ File.open("#{target_dir}/app/apis/#{project_name}/base.rb", "r+") do |f|
109
+ pos = f.pos
110
+ rest = f.read
111
+ f.seek pos
112
+ f.write("require_relative './modules/")
113
+ f.write(module_name)
114
+ f.write("_apis'\n")
115
+ f.write(rest)
116
+ end
117
+ return false
118
+ end
119
+
120
+ =begin
121
+ Function to copy the module of interest to project location.
122
+ =end
123
+ def copy_module
124
+ src = "#{gem_path}/lib/modules/#{module_name}/#{module_name}_apis.rb"
125
+ dest = "#{target_dir}/app/apis/#{project_name}/modules"
126
+ presence = File.exists?("#{dest}/#{module_name}_apis.rb")? true : false
127
+ FileUtils.mkdir dest unless File.exists?(dest)
128
+ FileUtils.cp(src,dest) unless presence
129
+ configure_module_files
130
+ $stdout.puts "\e[1;32m \tcreate\e[0m\tapp/apis/#{project_name}/modules/#{module_name}_apis.rb" unless presence
131
+ end
132
+
133
+ =begin
134
+ Function to create the necessary migrations and models.
135
+ =end
136
+ def create_migrations_and_models
137
+ src = "#{gem_path}/lib/modules/migrations"
138
+ dest = "#{target_dir}/db/migrate"
139
+ copy_files(src,dest,AUTH_MIGRATE)
140
+ if module_name == "oauth"
141
+ copy_files(src,dest,OAUTH_MIGRATE)
142
+ end
143
+ src_path = "#{gem_path}/lib/modules/models"
144
+ dest_path = "#{target_dir}/app/models"
145
+ copy_files(src_path,dest_path,AUTH_MODELS)
146
+ if module_name == "oauth"
147
+ copy_files(src_path,dest_path,OAUTH_MODELS)
148
+ end
149
+ end
150
+
151
+ =begin
152
+ Function to copy the module files to project location.
153
+ =end
154
+ def copy_files(src,dest,module_model)
155
+ module_model.each do |file|
156
+ presence = File.exists?("#{dest}/#{file}")? true : false
157
+ unless presence
158
+ FileUtils.cp("#{src}/#{file}",dest)
159
+ path = if dest.include? "app" then "app/models" else "db/migrate" end
160
+ $stdout.puts "\e[1;32m \tcreate\e[0m\t#{path}/#{file}"
161
+ end
162
+ end
163
+ end
164
+
165
+ =begin
166
+ Function to configure the module files.
167
+ =end
168
+ def configure_module_files
169
+ source = "#{target_dir}/app/apis/#{project_name}/modules/#{module_name}_apis.rb"
170
+ application_module = project_name.split('_').map(&:capitalize)*''
171
+ file = File.read(source)
172
+ replace = file.gsub(/module Rammer/, "module #{application_module}")
173
+ File.open(source, "w"){|f|
174
+ f.puts replace
175
+ }
176
+ end
177
+
178
+ =begin
179
+ Function to add the module dependency gems to project Gemfile.
180
+ =end
181
+ def add_gems
182
+ file = File.open("#{target_dir}/Gemfile", "r+")
183
+ file.each do |line|
184
+ while line == "gem 'oauth2'\n" do
185
+ return
186
+ end
187
+ end
188
+ File.open("#{target_dir}/Gemfile", "a+") do |f|
189
+ f.write("gem 'multi_json'\ngem 'oauth2'\ngem 'songkick-oauth2-provider'\ngem 'ruby_regex'\ngem 'oauth'\n")
190
+ end
191
+ $stdout.puts "\e[1;35m \tGemfile\e[0m\tgem 'multi_json'\n\t\tgem 'oauth2'
192
+ \t\tgem 'songkick-oauth2-provider'\n\t\tgem 'ruby_regex'\n\t\tgem 'oauth'\n"
193
+ $stdout.puts "\e[1;32m \trun\e[0m\tbundle install"
194
+ system("bundle install")
195
+ end
196
+
197
+ =begin
198
+ Unmounts the modules by removing the respective module files.
199
+ =end
200
+ def unmount_module
201
+ path = "#{target_dir}/app/apis/#{project_name}"
202
+ temp_file = "#{path}/tmp.rb"
203
+ source = "#{path}/base.rb"
204
+ delete_file = "#{path}/modules/#{module_name}_apis.rb"
205
+
206
+ File.open(temp_file, "w") do |out_file|
207
+ File.foreach(source) do |line|
208
+ unless line == "require_relative './modules/#{module_name}_apis'\n"
209
+ out_file.puts line unless line == "\t\tmount #{module_class}\n"
210
+ end
211
+ end
212
+ FileUtils.mv(temp_file, source)
213
+ end
214
+
215
+ if File.exists?(delete_file)
216
+ FileUtils.rm(delete_file)
217
+ $stdout.puts "\e[1;35m\tunmounted\e[0m\t#{module_class}"
218
+ $stdout.puts "\e[1;31m\tdelete\e[0m\t\tapp/apis/#{project_name}/modules/#{module_name}_apis.rb"
219
+ else
220
+ $stdout.puts "\e[33mModule already unmounted.\e[0m"
221
+ end
222
+ end
223
+
224
+ =begin
225
+ Notification for oauth module api functionality access.
226
+ =end
227
+ def oauth_message
228
+ $stdout.puts "\e[33m
229
+ In app/apis/<APP_NAME>/modules/oauth_apis.rb
230
+ Specify redirection url to the respective authorization page into 'redirect_to_url'
231
+ and uncomment the code to enable /oauth/authorize endpoint functionality.
232
+
233
+ \e[0m"
234
+ end
235
+ end
236
+ end
@@ -0,0 +1,160 @@
1
+ =begin
2
+ **************************************************************************
3
+ * The MIT License (MIT)
4
+
5
+ * Copyright (c) 2013-2014 QBurst Technologies Inc.
6
+
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ * THE SOFTWARE.
24
+
25
+ **************************************************************************
26
+ =end
27
+
28
+ require "rammer/version"
29
+ require 'fileutils'
30
+
31
+ $gem_file_name = "rammer-"+Rammer::VERSION
32
+
33
+ module Rammer
34
+
35
+ =begin
36
+ Generator class for creating application basic folder structure
37
+ =end
38
+ class RammerGenerator
39
+ attr_accessor :project_name, :target_dir, :module_name, :gem_path, :valid_name
40
+ BASE_DIR = ['app', 'app/apis', 'config', 'db', 'db/migrate', 'app/models']
41
+ COMMON_RAMMER_FILES = ['Gemfile','Gemfile.lock','Procfile','Rakefile','server.rb', 'tree.rb']
42
+ RESERVED_WORDS = ['rammer', 'viber', 'test', 'lib', 'template', 'authorization', 'authentication', 'app', 'apis', 'models', 'migrate', 'oauth', 'oauth2']
43
+
44
+ =begin
45
+ Initiliazes the following attributes :
46
+ project_name (application name), valid_name (boolean value for validation), target_dir (new application path)
47
+ and gem_path (path at which the gem is installed)
48
+ =end
49
+ def initialize(dir_name)
50
+ self.project_name = dir_name
51
+ self.valid_name = true
52
+ if self.project_name.nil? || self.project_name.squeeze.strip == ""
53
+ $stdout.puts "\e[1;31mError:\e[0m Please specify an application name."
54
+ self.valid_name = false
55
+ elsif RESERVED_WORDS.include? self.project_name
56
+ $stdout.puts "\e[1;31mError:\e[0m Invalid application name #{project_name}. Please give a name which does not match one of the reserved rammer words."
57
+ self.valid_name = false
58
+ end
59
+
60
+ self.target_dir = Dir.pwd + "/" + self.project_name
61
+ path = `gem which rammer`
62
+ self.gem_path = path.split($gem_file_name,2).first + $gem_file_name
63
+ end
64
+
65
+ =begin
66
+ Creates a basic folder structure with required files and configuration setup.
67
+ =end
68
+ def run
69
+ unless !valid_name || File.exists?(project_name) || File.directory?(project_name)
70
+ $stdout.puts "Creating goliath application under the directory #{project_name}"
71
+ FileUtils.mkdir project_name
72
+
73
+ create_base_dirs
74
+ copy_files_to_target
75
+ setup_api_module
76
+ copy_files_to_dir 'application.rb','config'
77
+ copy_files_to_dir 'database.yml','config'
78
+ $stdout.puts "\e[33mRun `bundle install` to install missing gems.\e[0m"
79
+ else
80
+ unless !valid_name
81
+ $stdout.puts "\e[1;31mError:\e[0m The directory #{project_name} already exists, aborting. Maybe move it out of the way before continuing."
82
+ end
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ =begin
89
+ Creates the application base directories.
90
+ =end
91
+ def create_base_dirs
92
+ BASE_DIR.each do |dir|
93
+ FileUtils.mkdir "#{project_name}/#{dir}"
94
+ $stdout.puts "\e[1;32m \tcreate\e[0m\t#{dir}"
95
+ end
96
+ FileUtils.mkdir "#{project_name}/app/apis/#{project_name}"
97
+ $stdout.puts "\e[1;32m \tcreate\e[0m\tapp/apis/#{project_name}"
98
+ end
99
+
100
+ =begin
101
+ Function to setup the API modules.
102
+ =end
103
+ def setup_api_module
104
+ self.module_name = project_name.split('_').map(&:capitalize).join('')
105
+ create_api_module
106
+ config_server
107
+ end
108
+
109
+ =begin
110
+ Function to create the API modules.
111
+ =end
112
+ def create_api_module
113
+ File.open("#{project_name}/app/apis/#{project_name}/base.rb", "w") do |f|
114
+ f.write('module ')
115
+ f.puts(module_name)
116
+ f.write("\tclass Base < Grape::API\n\tend\nend")
117
+ end
118
+ $stdout.puts "\e[1;32m \tcreate\e[0m\tapp/apis/#{project_name}/base.rb"
119
+ end
120
+
121
+ =begin
122
+ Function to configure the Goliath server.
123
+ =end
124
+ def config_server
125
+ file = File.open("#{project_name}/server.rb", "r+")
126
+ file.each do |line|
127
+ while line == " def response(env)\n" do
128
+ pos = file.pos
129
+ rest = file.read
130
+ file.seek pos
131
+ file.write("\t::")
132
+ file.write(module_name)
133
+ file.write("::Base.call(env)\n")
134
+ file.write(rest)
135
+ $stdout.puts "\e[1;35m \tconfig\e[0m\tserver.rb"
136
+ return
137
+ end
138
+ end
139
+ end
140
+
141
+ =begin
142
+ Function to copy the template files project location.
143
+ =end
144
+ def copy_files_to_target
145
+ COMMON_RAMMER_FILES.each do |file|
146
+ source = File.join("#{gem_path}/lib/modules/common/",file)
147
+ FileUtils.cp(source,"#{project_name}")
148
+ $stdout.puts "\e[1;32m \tcreate\e[0m\t#{file}"
149
+ end
150
+ end
151
+
152
+ =begin
153
+ Creates api modules, required files and configures the server with respect to new application.
154
+ =end
155
+ def copy_files_to_dir(file,destination)
156
+ FileUtils.cp("#{gem_path}/lib/modules/common/#{file}","#{project_name}/#{destination}")
157
+ $stdout.puts "\e[1;32m \tcreate\e[0m\t#{destination}/#{file}"
158
+ end
159
+ end
160
+ end
@@ -1,3 +1,30 @@
1
+ =begin
2
+ **************************************************************************
3
+ * The MIT License (MIT)
4
+
5
+ * Copyright (c) 2013-2014 QBurst Technologies Inc.
6
+
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ * THE SOFTWARE.
24
+
25
+ **************************************************************************
26
+ =end
27
+
1
28
  module Rammer
2
- VERSION = "1.1.2"
29
+ VERSION = "2.0.0"
3
30
  end