Class: AngularInitializer
- Inherits:
-
Object
- Object
- AngularInitializer
- Defined in:
- lib/oa_angular_setup/initialize_angular.rb
Instance Method Summary collapse
- #add_angular ⇒ Object
-
#initialize ⇒ AngularInitializer
constructor
A new instance of AngularInitializer.
- #output_to_angular(outfile_or_string, method, path) ⇒ Object
- #run ⇒ Object
- #update ⇒ Object
- #update_app_js(apis) ⇒ Object
- #update_controllers(apis, model_name, app_name) ⇒ Object
- #update_factory(apis, model, name) ⇒ Object
- #write_app_js(apis, model_name, outfile) ⇒ Object
- #write_backups ⇒ Object
- #write_controller(model, endpoint, op) ⇒ Object
- #write_controllers(apis, model_name, app_name) ⇒ Object
- #write_factories(apis, model, name) ⇒ Object
- #write_factory(endpoint, op) ⇒ Object
Constructor Details
#initialize ⇒ AngularInitializer
Returns a new instance of AngularInitializer.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 6 def initialize config = OaAngularSetup.configuration @app_name = config.app_name @factory_name = "#{@app_name}Services" @create_factories = config.create_factories @create_app_js = config.create_app_js @create_controllers = config.create_controllers @url = config.swagger_doc_url @destination = "#{Rails.root}#{config.destination}" end |
Instance Method Details
#add_angular ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 17 def add_angular #add in swagger ui if OaAngularSetup.configuration.add_swaggger_ui if !File.exists?("#{Rails.root}/public/api/docs/") #get files and move into place Dir.mkdir("#{Rails.root}/public/api") unless File.exists?("#{Rails.root}/public/api") system "npm install swagger-ui" unless File.exists?("#{Rails.root}/node_modules/swagger-ui/") system "cp -R #{Rails.root}/node_modules/swagger-ui/dist #{Rails.root}/public/api/docs" #replace dummy url file_name = "#{Rails.root}/public/api/docs/index.html" text = File.read(file_name) new_contents = text.gsub('http://petstore.swagger.io/v2/swagger.json', @url) File.open(file_name, "w") {|file| file.puts new_contents } end end end |
#output_to_angular(outfile_or_string, method, path) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 113 def output_to_angular(outfile_or_string, method, path) string = "" # assuming only called for PUT and GET methods if method == 'GET' command = 'show' elsif method == 'PUT' command = 'update' else puts "problem. method not GET or PUT" return end if path.include?("id") ep_path = path.split('/') if ep_path[4].nil? string += " #{command}: {method:'#{method}'}" else action = ep_path[4].split(".")[0] if method == 'GET' string += " #{action}: {method:'#{method}', isArray:true, params:{action:'#{action}'}}" else string += " #{action}: {method:'#{method}', params:{action:'#{action}'}}" end end else string += " query: {method:'#{method}', isArray:true}" end if outfile_or_string.is_a?(File) outfile_or_string.write(string) else return string end end |
#run ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 70 def run Dir.mkdir("#{@destination}") unless File.exists?("#{@destination}") add_angular mechanize = Mechanize.new page = mechanize.get(@url) body = JSON.parse(page.body) if @create_app_js app_js_file = File.open("#{@destination}#{@app_name}.js", 'w') if @create_factories app_js_file.write("var #{@app_name} = angular.module('#{@app_name}', ['ngRoute', '#{@factory_name}']); \n") else app_js_file.write("var #{@app_name} = angular.module('#{@app_name}', ['ngRoute']); \n") end app_js_file.write("\n "); app_js_file.write("#{@app_name}.config(['$routeProvider', function($routeProvider) { \n"); app_js_file.write(" $routeProvider.\n") end body["apis"].each do |api| model = api["path"].split('.')[0] api_page = mechanize.get(@url + model) api_body = JSON.parse(api_page.body) write_factories(api_body["apis"], model, @factory_name) if @create_factories write_app_js(api_body["apis"], model, app_js_file) if @create_app_js write_controllers(api_body["apis"], model, @app_name) if @create_controllers end if @create_app_js app_js_file.write(" otherwise({ \n") app_js_file.write(" redirectTo: '/' \n") app_js_file.write(" }); \n") app_js_file.write("}]); \n") if @create_factories app_js_file.write("\n") app_js_file.write("var #{@factory_name} = angular.module('#{@factory_name}', ['ngResource']); \n") end app_js_file.close end write_backups end |
#update ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 35 def update mechanize = Mechanize.new page = mechanize.get(@url) body = JSON.parse(page.body) app_bodies = {} body["apis"].each do |api| model = api["path"].split('.')[0] api_page = mechanize.get(@url + model) api_body = JSON.parse(api_page.body) app_bodies[model] = api_body["apis"] update_factory(api_body["apis"], model, @factory_name) if @create_factories update_controllers(api_body["apis"], model, @app_name) if @create_controllers end update_app_js(app_bodies) if @create_app_js write_backups end |
#update_app_js(apis) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 346 def update_app_js(apis) output, edited_routes = [],[] file_contents = File.read("#{@destination}#{@app_name}.js") routes = file_contents.scan(/when\(.*?}\)\./im) route_paths = file_contents.scan(/when\('(.*?)'/im).flatten if @create_app_js if @create_factories output.push("var #{@app_name} = angular.module('#{@app_name}', ['ngRoute', '#{@factory_name}']); \n") else output.push("var #{@app_name} = angular.module('#{@app_name}', ['ngRoute']); \n") end output.push("\n #{@app_name}.config(['$routeProvider', function($routeProvider) { \n $routeProvider.\n"); end routes.each_with_index do |route, index| output.push(" #{route} \n ") end apis.each do |model_name, model_api| model = model_name.delete('/') model_api.each do |endpoint| endpoint["operations"].each do |op| string , route_path = "","" case op["method"] when /GET/ ep_path = endpoint["path"].split('/') if ep_path[4].nil? if endpoint["path"].include?("id") route_path = "/#{model}/:id" string += " when('#{route_path}', { \n" string += " template@url: '/angular/templates/#{model}/show.html', \n" string += " controller: '#{model.capitalize}ShowCtrl' \n" else ctrl = 'index' route_path = "/#{model}" string += " when('#{route_path}', { \n" string += " template@url: '/angular/templates/#{model}/index.html',\n" string += " controller: '#{model.capitalize}IndexCtrl' \n" end string += " }). \n" end when /PUT/ ep_path = endpoint["path"].split('/') if ep_path[4].nil? route_path = "/#{model}/:id/edit" string += " when('#{route_path}', { \n" string += " template@url: '/angular/templates/#{model}/edit.html', \n" string += " controller: '#{model.capitalize}EditCtrl' \n" string += " }). \n" end when /POST/ route_path = "/#{model}/new" string += " when('#{route_path}', { \n" string += " template@url: '/angular/templates/#{model}/new.html', \n" string += " controller: '#{model.capitalize}NewCtrl' \n" string += " }). \n" end output.push(string) unless route_paths.include?(route_path) end end end output.push(" otherwise({ \n") output.push(" redirectTo: '/' \n") output.push(" }); \n") output.push("}]); \n") if @create_factories output.push("\n") output.push("var #{@factory_name} = angular.module('#{@factory_name}', ['ngResource']); \n") end app_js_file = File.open("#{@destination}#{@app_name}.js", 'w') app_js_file.write(output.join) app_js_file.close end |
#update_controllers(apis, model_name, app_name) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 321 def update_controllers(apis, model_name, app_name) Dir.mkdir("#{@destination}controllers") unless File.exists?("#{@destination}controllers") return write_controllers(apis, model_name, app_name) unless File.exists?("#{@destination}controllers/#{model_name}_controllers.js") output, edited_controller_titles = [], [] model = model_name.delete('/') file_contents = File.read("#{@destination}controllers/#{model}_controllers.js") controller_names = file_contents.scan(/controller\('(.*?)'/im).flatten controllers = file_contents.scan(/angular.module.*?}\]\);/im) controllers.each do |controller| output.push(controller+" \n \n") end apis.each do |endpoint| endpoint["operations"].each do |op| string,controller_title = write_controller(model, endpoint, op) output.push(string) unless controller_names.include?(controller_title) end end outfile = File.open("#{@destination}controllers/#{model}_controllers.js", 'w') outfile.write(output.join) outfile.close end |
#update_factory(apis, model, name) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 226 def update_factory(apis, model, name) Dir.mkdir("#{@destination}factories") unless File.exists?("#{@destination}factories") return write_factories(apis, model, name) unless File.exists?("#{@destination}factories/#{model}_factory.js") fh1 = name + ".factory('" fh2 = "', ['$resource', function($resource){\n" fname = "#{@destination}factories/" + model.gsub("/","") + "_factory.js" # backup_contents = File.read("#{@destination}backups/factories/" + model.gsub("/","") + "_factory.js").split("\n") file_contents = File.read(fname) file_contents_lines = file_contents.split("\n") first_lines = file_contents_lines.shift(2) output_string = first_lines.join("\n")+"\n" file_contents_lines.pop(2) #drop last two closing lines so they arent added twice paths = file_contents.scan(/\n\s*(\w*?):/i).flatten output = [] file_contents_lines.each_with_index do |line, index| output.push(line.chomp(",")) end apis.each do |endpoint| endpoint["operations"].each do |op| string = write_factory(endpoint, op) next if output.include?(string) next if(paths.include?(string.split(":")[0])) string = " // #{op['summary']} \n"+string output.push(string) end end output_string += output.join(",\n") output_string += "\n });\n" output_string += "}]);\n" outfile = File.open(fname, 'w') outfile.write(output_string) outfile.close end |
#write_app_js(apis, model_name, outfile) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 173 def write_app_js(apis, model_name, outfile) model = model_name.delete('/') apis.each do |endpoint| endpoint["operations"].each do |op| case op["method"] when /GET/ ep_path = endpoint["path"].split('/') if ep_path[4].nil? if endpoint["path"].include?("id") outfile.write(" when('/#{model}/:id', { \n") outfile.write(" template@url: '/angular/templates/#{model}/show.html', \n") outfile.write(" controller: '#{model.capitalize}ShowCtrl' \n") else ctrl = 'index' outfile.write(" when('/#{model}', { \n") outfile.write(" template@url: '/angular/templates/#{model}/index.html',\n") outfile.write(" controller: '#{model.capitalize}IndexCtrl' \n") end outfile.write(" }). \n") end when /PUT/ ep_path = endpoint["path"].split('/') if ep_path[4].nil? outfile.write(" when('/#{model}/:id/edit', { \n") outfile.write(" template@url: '/angular/templates/#{model}/edit.html', \n") outfile.write(" controller: '#{model.capitalize}EditCtrl' \n") outfile.write(" }). \n") end when /POST/ outfile.write(" when('/#{model}/new', { \n") outfile.write(" template@url: '/angular/templates/#{model}/new.html', \n") outfile.write(" controller: '#{model.capitalize}NewCtrl' \n") outfile.write(" }). \n") end end end end |
#write_backups ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 55 def write_backups Dir.mkdir("#{@destination}backups/") unless File.exists?("#{@destination}backups/") if @create_factories Dir.mkdir("#{@destination}backups/factories/") unless File.exists?("#{@destination}backups/factories/") FileUtils.cp_r "#{@destination}factories/.", "#{@destination}backups/factories/" end if @create_factories Dir.mkdir("#{@destination}backups/controllers/") unless File.exists?("#{@destination}backups/controllers/") FileUtils.cp_r "#{@destination}controllers/.", "#{@destination}backups/controllers/" end if @create_app_js FileUtils.cp "#{@destination}#{@app_name}.js", "#{@destination}backups/#{@app_name}.js" end end |
#write_controller(model, endpoint, op) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 265 def write_controller(model, endpoint, op) string = "" controller_title = "" service = model.chomp('s').capitalize ep_path = endpoint["path"].split('/') case op["method"] when /GET/ if ep_path[4].nil? if endpoint["path"].include?("id") controller_title = "#{model.capitalize}ShowCtrl" string += "angular.module('#{@app_name}').controller('#{controller_title}', ['$scope', '$routeParams', '#{service}', function ($scope, $routeParams, #{service}) { \n" string += " $scope.id = $routeParams.id; \n" string += "}]); \n" string += " \n" else controller_title = "#{model.capitalize}IndexCtrl" string += "angular.module('#{@app_name}').controller('#{controller_title}', ['$scope', '#{service}', function ($scope, #{service}) { \n" string += " $scope.#{model} = #{service}.query(); \n" string += "}]); \n" string += " \n" end end when /PUT/ if ep_path[4].nil? controller_title = "#{model.capitalize}EditCtrl" string += "angular.module('#{@app_name}').controller('#{controller_title}', ['$scope', '$routeParams', '#{service}', function ($scope, $routeParams, #{service}) { \n" string += " $scope.id = $routeParams.id; \n" string += "}]); \n" string += " \n" end when /POST/ controller_title = "#{model.capitalize}NewCtrl" string += "angular.module('#{@app_name}').controller('#{controller_title}', ['$scope', '#{service}', function ($scope, #{service}) { \n" string += "}]); \n" string += " \n" end return string, controller_title end |
#write_controllers(apis, model_name, app_name) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 211 def write_controllers(apis, model_name, app_name) Dir.mkdir("#{@destination}controllers") unless File.exists?("#{@destination}controllers") model = model_name.delete('/') output = [] apis.each do |endpoint| endpoint["operations"].each do |op| string,controller_title = write_controller(model, endpoint, op) output.push(string) end end outfile = File.open("#{@destination}controllers/#{model}_controllers.js", 'w') outfile.write(output.join) outfile.close end |
#write_factories(apis, model, name) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 146 def write_factories(apis, model, name) Dir.mkdir("#{@destination}factories") unless File.exists?("#{@destination}factories") fh1 = name + ".factory('" fh2 = "', ['$resource', function($resource){\n" output = [] fname = "#{@destination}factories/" + model.gsub("/","") + "_factory.js" output_string = fh1 + model.gsub("/","").chomp('s').capitalize + fh2 output_string += " return $resource('api/v1" + model + "/:id/:action', {}, { \n" apis.each do |endpoint| endpoint["operations"].each do |op| string = write_factory(endpoint, op) string = " // #{op['summary']} \n"+string output.push(string) end end output_string += output.join(",\n") output_string += "\n });\n" output_string += "}]);\n" outfile = File.open(fname, 'w') outfile.write(output_string) outfile.close end |
#write_factory(endpoint, op) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/oa_angular_setup/initialize_angular.rb', line 305 def write_factory(endpoint, op) string = "" case op["method"] when /GET/ string = output_to_angular(string, op["method"], endpoint["path"]) when /PUT/ string = output_to_angular(string, op["method"], endpoint["path"]) when /POST/ string = " create: {method:'POST'}" when /DELETE/ string = " destroy: {method:'DELETE'}" end return string end |