ocean-rails 1.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +72 -0
  4. data/Rakefile +38 -0
  5. data/lib/generators/ocean_scaffold/USAGE +8 -0
  6. data/lib/generators/ocean_scaffold/ocean_scaffold_generator.rb +76 -0
  7. data/lib/generators/ocean_scaffold/templates/controller_specs/create_spec.rb +71 -0
  8. data/lib/generators/ocean_scaffold/templates/controller_specs/delete_spec.rb +47 -0
  9. data/lib/generators/ocean_scaffold/templates/controller_specs/index_spec.rb +45 -0
  10. data/lib/generators/ocean_scaffold/templates/controller_specs/show_spec.rb +43 -0
  11. data/lib/generators/ocean_scaffold/templates/controller_specs/update_spec.rb +85 -0
  12. data/lib/generators/ocean_scaffold/templates/model_spec.rb +76 -0
  13. data/lib/generators/ocean_scaffold/templates/resource_routing_spec.rb +27 -0
  14. data/lib/generators/ocean_scaffold/templates/view_specs/_resource_spec.rb +55 -0
  15. data/lib/generators/ocean_scaffold/templates/views/_resource.json.jbuilder +8 -0
  16. data/lib/generators/ocean_setup/USAGE +8 -0
  17. data/lib/generators/ocean_setup/ocean_setup_generator.rb +93 -0
  18. data/lib/generators/ocean_setup/templates/Gemfile +19 -0
  19. data/lib/generators/ocean_setup/templates/alive_controller.rb +18 -0
  20. data/lib/generators/ocean_setup/templates/alive_routing_spec.rb +11 -0
  21. data/lib/generators/ocean_setup/templates/alive_spec.rb +12 -0
  22. data/lib/generators/ocean_setup/templates/api_constants.rb +19 -0
  23. data/lib/generators/ocean_setup/templates/application_controller.rb +8 -0
  24. data/lib/generators/ocean_setup/templates/application_helper.rb +34 -0
  25. data/lib/generators/ocean_setup/templates/config.yml.example +57 -0
  26. data/lib/generators/ocean_setup/templates/errors_controller.rb +14 -0
  27. data/lib/generators/ocean_setup/templates/gitignore +37 -0
  28. data/lib/generators/ocean_setup/templates/hyperlinks.rb +22 -0
  29. data/lib/generators/ocean_setup/templates/ocean_constants.rb +36 -0
  30. data/lib/generators/ocean_setup/templates/routes.rb +8 -0
  31. data/lib/generators/ocean_setup/templates/spec_helper.rb +47 -0
  32. data/lib/generators/ocean_setup/templates/zeromq_logger.rb +15 -0
  33. data/lib/ocean-rails.rb +38 -0
  34. data/lib/ocean/api.rb +263 -0
  35. data/lib/ocean/api_resource.rb +135 -0
  36. data/lib/ocean/flooding.rb +29 -0
  37. data/lib/ocean/ocean_application_controller.rb +214 -0
  38. data/lib/ocean/ocean_resource_controller.rb +76 -0
  39. data/lib/ocean/ocean_resource_model.rb +61 -0
  40. data/lib/ocean/selective_rack_logger.rb +33 -0
  41. data/lib/ocean/version.rb +3 -0
  42. data/lib/ocean/zero_log.rb +184 -0
  43. data/lib/ocean/zeromq_logger.rb +42 -0
  44. data/lib/tasks/ocean_tasks.rake +4 -0
  45. data/lib/template.rb +31 -0
  46. data/lib/templates/rails/scaffold_controller/controller.rb +91 -0
  47. metadata +267 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ef1841120052a0246a8c515f222181d460175a5e
4
+ data.tar.gz: e415725dd7ff365f19b8feb03c2922c1b101e41c
5
+ SHA512:
6
+ metadata.gz: c744ca8f3eb1ae1b349d6c5098c579b22a86e502141ca315788fb874f7298ea3390ab62c0c561e6215ad6b7c07fa5249611363a80cc1919f01a777810913905e
7
+ data.tar.gz: 1f81c189825763f0de71f19df10438ae6d8d0d09b9d848091715b982b9a199fd3fcc6b6b823b84c56590da51882beb203cad5bbefed0ac9e6a8eff078d6ccbdc
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
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,72 @@
1
+ == The Ocean gem
2
+
3
+ This repository contains the Ocean ruby gem, containing common framework functionality
4
+ for the Ruby on Rails part of the architecture.
5
+
6
+ Ocean requires Ruby 2.0 and Ruby on Rails 4.0.0 or later.
7
+
8
+ === Documentation
9
+ * Ocean gem API: http://rdoc.info/github/OceanDev/ocean-rails
10
+ * Ocean framework: http://wiki.oceanframework.net
11
+ * Ocean back end tutorial: http://wiki.oceanframework.net/index.php/Tutorial
12
+
13
+
14
+ === Creating an Ocean Rails app
15
+
16
+ The ocean gem provides an application template and generator to quickly and easily set up
17
+ a complete Rails application for Ocean. Simply execute the following in your terminal:
18
+
19
+ rails new the_app_name -m https://raw.github.com/OceanDev/ocean-rails/master/template.rb
20
+
21
+ Answer yes to all overwrite queries.
22
+
23
+ There is only one piece of manual setup to perform. You must supply the Ocean app with
24
+ your site-specific data: the base domain name, the password for Auth, etc. To do this,
25
+ simply edit <code>config/config.yml</code>.
26
+
27
+ There is also a file called <code>config.yml.example</code> in the
28
+ same directory for reference. Don't change it: it is under version control. The file
29
+ you should change, <code>config/config.yml</code>, isn't, as it will contain site-specific and/or
30
+ confidential data.
31
+
32
+ Verify that your setup is OK by executing
33
+
34
+ rspec
35
+
36
+ All tests should pass.
37
+
38
+
39
+ === Creating an Ocean Resource
40
+
41
+ To create an aggressively cached Ocean resource based on an SQL model, do the following:
42
+
43
+ rails g scaffold quux name:string description:string \
44
+ lock_version:integer created_by:string updated_by:string
45
+
46
+ This will create the basic model and associated model and controller scaffolding code,
47
+ which we will modify shortly. Now run
48
+
49
+ rake db:migrate
50
+
51
+ This will create the SQL table for the resource. Next, we need to replace the HTML-centric
52
+ scaffold code generated above with RESTful JSON scaffold code for Ocean:
53
+
54
+ rails g ocean_scaffold quux
55
+
56
+ Answer yes to all overwrite queries. Now examine +config/routes.rb+. You will find a new resource
57
+ declaration for quuxes. Move it inside the versioned scope and add and +except:+ clause to exclude
58
+ the Rails controller actions not used in a REST Api:
59
+
60
+ scope "v1" do
61
+ resource :quuxes, except: [:new, :edit]
62
+ end
63
+
64
+ To verify that everything works as it should, run the tests:
65
+
66
+ rspec
67
+
68
+ All tests should pass. The test coverage should be very close to 100%. A FactoryGirl factory for the new model will be created, there will be model unit tests to check for the presence of all attributes and to verify collection searches, routing tests, controller tests for each action, and view tests to verify that the JSON representation is complete and correct.
69
+
70
+ You can now proceed to tailor the new resource to your needs. You will want to add other attributes to the model or remove some or all of the default ones; you can change the JSON representation by modifying the view; and you might want to add or remove controller actions, e.g. to support secondary collections and relations to other resources. And as you no doubt are a responsible, informed developer, you will of course do all this using TDD and/or BDD techniques.
71
+
72
+
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Ocean'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate ocean_scaffold Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,76 @@
1
+ class OceanScaffoldGenerator < Rails::Generators::NamedBase #:nodoc: all
2
+
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def extend_model
6
+ inject_into_class "#{Rails.root}/app/models/#{singular_name}.rb",
7
+ class_name.constantize,
8
+ "
9
+ ocean_resource_model index: [:name], search: :description
10
+
11
+
12
+ # Relations
13
+
14
+
15
+ # Attributes
16
+
17
+
18
+ # Validations
19
+
20
+
21
+ "
22
+ end
23
+
24
+ def add_model_specs
25
+ template "model_spec.rb", "#{Rails.root}/spec/models/#{singular_name}_spec.rb"
26
+ end
27
+
28
+ def remove_html_controller_specs
29
+ remove_file "spec/controllers/#{plural_name}_controller_spec.rb"
30
+ end
31
+
32
+ def add_json_controller_specs
33
+ template "controller_specs/delete_spec.rb", "#{Rails.root}/spec/controllers/#{plural_name}/delete_spec.rb"
34
+ template "controller_specs/show_spec.rb", "#{Rails.root}/spec/controllers/#{plural_name}/show_spec.rb"
35
+ template "controller_specs/index_spec.rb", "#{Rails.root}/spec/controllers/#{plural_name}/index_spec.rb"
36
+ template "controller_specs/create_spec.rb", "#{Rails.root}/spec/controllers/#{plural_name}/create_spec.rb"
37
+ template "controller_specs/update_spec.rb", "#{Rails.root}/spec/controllers/#{plural_name}/update_spec.rb"
38
+ end
39
+
40
+ def remove_html_views
41
+ remove_file "app/views/#{plural_name}/_form.html.erb"
42
+ remove_file "app/views/#{plural_name}/edit.html.erb"
43
+ remove_file "app/views/#{plural_name}/index.html.erb"
44
+ remove_file "app/views/#{plural_name}/index.json.jbuilder"
45
+ remove_file "app/views/#{plural_name}/new.html.erb"
46
+ remove_file "app/views/#{plural_name}/show.html.erb"
47
+ remove_file "app/views/#{plural_name}/show.json.jbuilder"
48
+ end
49
+
50
+ def add_json_views
51
+ template "views/_resource.json.jbuilder", "#{Rails.root}/app/views/#{plural_name}/_#{singular_name}.json.jbuilder"
52
+ end
53
+
54
+ def remove_html_view_specs
55
+ remove_file "spec/views/#{plural_name}/index.html.erb_spec.rb"
56
+ remove_file "spec/views/#{plural_name}/show.html.erb_spec.rb"
57
+ remove_file "spec/views/#{plural_name}/new.html.erb_spec.rb"
58
+ remove_file "spec/views/#{plural_name}/create.html.erb_spec.rb"
59
+ remove_file "spec/views/#{plural_name}/update.html.erb_spec.rb"
60
+ remove_file "spec/views/#{plural_name}/edit.html.erb_spec.rb"
61
+ end
62
+
63
+ def add_json_view_spec
64
+ template "view_specs/_resource_spec.rb", "#{Rails.root}/spec/views/#{plural_name}/_#{singular_name}_spec.rb"
65
+ end
66
+
67
+ def remove_request_specs
68
+ remove_file "spec/requests/#{plural_name}_spec.rb"
69
+ end
70
+
71
+ def update_routing_specs
72
+ remove_file "spec/routing/#{plural_name}_routing_spec.rb"
73
+ template "resource_routing_spec.rb", "#{Rails.root}/spec/routing/#{plural_name}_routing_spec.rb"
74
+ end
75
+
76
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= class_name.pluralize %>Controller do
4
+
5
+ render_views
6
+
7
+ describe "POST" do
8
+
9
+ before :each do
10
+ permit_with 200
11
+ request.headers['HTTP_ACCEPT'] = "application/json"
12
+ request.headers['X-API-Token'] = "incredibly-fake!"
13
+ @args = build(:<%= singular_name %>).attributes
14
+ end
15
+
16
+
17
+ it "should return JSON" do
18
+ post :create, @args
19
+ response.content_type.should == "application/json"
20
+ end
21
+
22
+ it "should return a 400 if the X-API-Token header is missing" do
23
+ request.headers['X-API-Token'] = nil
24
+ post :create, @args
25
+ response.status.should == 400
26
+ end
27
+
28
+ it "should return a 201 when successful" do
29
+ post :create, @args
30
+ response.should render_template(partial: "_<%= singular_name %>", count: 1)
31
+ response.status.should == 201
32
+ end
33
+
34
+ it "should contain a Location header when successful" do
35
+ post :create, @args
36
+ response.headers['Location'].should be_a String
37
+ end
38
+
39
+ it "should return the new resource in the body when successful" do
40
+ post :create, @args
41
+ response.body.should be_a String
42
+ end
43
+
44
+ #
45
+ # Uncomment this test as soon as there is one or more DB attributes that define
46
+ # the uniqueness of a record.
47
+ #
48
+ # it "should return a 422 if the <%= singular_name %> already exists" do
49
+ # post :create, @args
50
+ # response.status.should == 201
51
+ # response.content_type.should == "application/json"
52
+ # post :create, @args
53
+ # response.status.should == 422
54
+ # response.content_type.should == "application/json"
55
+ # JSON.parse(response.body).should == {"_api_error" => ["<%= class_name %> already exists"]}
56
+ # end
57
+
58
+ #
59
+ # Uncomment this test as soon as there is one or more DB attributes that need
60
+ # validating.
61
+ #
62
+ # it "should return a 422 when there are validation errors" do
63
+ # post :create, @args.merge('name' => "qz")
64
+ # response.status.should == 422
65
+ # response.content_type.should == "application/json"
66
+ # JSON.parse(response.body).should == {"name"=>["is too short (minimum is 3 characters)"]}
67
+ # end
68
+
69
+ end
70
+
71
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= class_name.pluralize %>Controller do
4
+
5
+ render_views
6
+
7
+ describe "DELETE" do
8
+
9
+ before :each do
10
+ permit_with 200
11
+ @<%= singular_name %> = create :<%= singular_name %>
12
+ request.headers['HTTP_ACCEPT'] = "application/json"
13
+ request.headers['X-API-Token'] = "so-totally-fake"
14
+ end
15
+
16
+
17
+ it "should return JSON" do
18
+ delete :destroy, id: @<%= singular_name %>
19
+ response.content_type.should == "application/json"
20
+ end
21
+
22
+ it "should return a 400 if the X-API-Token header is missing" do
23
+ request.headers['X-API-Token'] = nil
24
+ delete :destroy, id: @<%= singular_name %>
25
+ response.status.should == 400
26
+ end
27
+
28
+ it "should return a 204 when successful" do
29
+ delete :destroy, id: @<%= singular_name %>
30
+ response.status.should == 204
31
+ response.content_type.should == "application/json"
32
+ end
33
+
34
+ it "should return a 404 when the <%= class_name %> can't be found" do
35
+ delete :destroy, id: -1
36
+ response.status.should == 404
37
+ end
38
+
39
+ it "should destroy the <%= class_name %> when successful" do
40
+ delete :destroy, id: @<%= singular_name %>
41
+ response.status.should == 204
42
+ <%= class_name %>.find_by_id(@<%= singular_name %>.id).should be_nil
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= class_name.pluralize %>Controller do
4
+
5
+ render_views
6
+
7
+ describe "INDEX" do
8
+
9
+ before :each do
10
+ permit_with 200
11
+ create :<%= singular_name %>
12
+ create :<%= singular_name %>
13
+ create :<%= singular_name %>
14
+ request.headers['HTTP_ACCEPT'] = "application/json"
15
+ request.headers['X-API-Token'] = "boy-is-this-fake"
16
+ end
17
+
18
+
19
+ it "should return JSON" do
20
+ get :index
21
+ response.content_type.should == "application/json"
22
+ end
23
+
24
+ it "should return a 400 if the X-API-Token header is missing" do
25
+ request.headers['X-API-Token'] = nil
26
+ get :index
27
+ response.status.should == 400
28
+ response.content_type.should == "application/json"
29
+ end
30
+
31
+ it "should return a 200 when successful" do
32
+ get :index
33
+ response.status.should == 200
34
+ response.should render_template(partial: "_<%= singular_name %>", count: 3)
35
+ end
36
+
37
+ it "should return a collection" do
38
+ get :index
39
+ response.status.should == 200
40
+ JSON.parse(response.body).should be_an Array
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= class_name.pluralize %>Controller do
4
+
5
+ render_views
6
+
7
+ describe "GET" do
8
+
9
+ before :each do
10
+ permit_with 200
11
+ @<%= singular_name %> = create :<%= singular_name %>
12
+ request.headers['HTTP_ACCEPT'] = "application/json"
13
+ request.headers['X-API-Token'] = "totally-fake"
14
+ end
15
+
16
+
17
+ it "should return JSON" do
18
+ get :show, id: @<%= singular_name %>
19
+ response.content_type.should == "application/json"
20
+ end
21
+
22
+ it "should return a 400 if the X-API-Token header is missing" do
23
+ request.headers['X-API-Token'] = nil
24
+ get :show, id: @<%= singular_name %>
25
+ response.status.should == 400
26
+ response.content_type.should == "application/json"
27
+ end
28
+
29
+ it "should return a 404 when the user can't be found" do
30
+ get :show, id: -1
31
+ response.status.should == 404
32
+ response.content_type.should == "application/json"
33
+ end
34
+
35
+ it "should return a 200 when successful" do
36
+ get :show, id: @<%= singular_name %>
37
+ response.status.should == 200
38
+ response.should render_template(partial: "_<%= singular_name %>", count: 1)
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= class_name.pluralize %>Controller do
4
+
5
+ render_views
6
+
7
+ describe "PUT" do
8
+
9
+ before :each do
10
+ permit_with 200
11
+ request.headers['HTTP_ACCEPT'] = "application/json"
12
+ request.headers['X-API-Token'] = "incredibly-fake!"
13
+ @u = create :<%= singular_name %>
14
+ @args = @u.attributes
15
+ end
16
+
17
+
18
+ it "should return JSON" do
19
+ put :update, @args
20
+ response.content_type.should == "application/json"
21
+ end
22
+
23
+ it "should return a 400 if the X-API-Token header is missing" do
24
+ request.headers['X-API-Token'] = nil
25
+ put :update, @args
26
+ response.status.should == 400
27
+ end
28
+
29
+ it "should return a 404 if the resource can't be found" do
30
+ put :update, id: -1
31
+ response.status.should == 404
32
+ response.content_type.should == "application/json"
33
+ end
34
+
35
+ it "should return a 422 when resource properties are missing (all must be set simultaneously)" do
36
+ put :update, id: @u.id
37
+ response.status.should == 422
38
+ response.content_type.should == "application/json"
39
+ end
40
+
41
+ it "should return a 409 when there is an update conflict" do
42
+ @u.update_attributes!({:updated_at => 1.week.from_now}, :without_protection => true)
43
+ put :update, @args
44
+ response.status.should == 409
45
+ end
46
+
47
+ it "should return a 200 when successful" do
48
+ put :update, @args
49
+ response.status.should == 200
50
+ response.should render_template(partial: "_<%= singular_name %>", count: 1)
51
+ end
52
+
53
+ it "should return the updated resource in the body when successful" do
54
+ put :update, @args
55
+ response.status.should == 200
56
+ JSON.parse(response.body).should be_a Hash
57
+ end
58
+
59
+ #
60
+ # Uncomment this test as soon as there is one or more DB attributes that need
61
+ # validating.
62
+ #
63
+ # it "should return a 422 when there are validation errors" do
64
+ # put :update, @args.merge('name' => "qz")
65
+ # response.status.should == 422
66
+ # response.content_type.should == "application/json"
67
+ # JSON.parse(response.body).should == {"name"=>["is too short (minimum is 3 characters)"]}
68
+ # end
69
+
70
+
71
+ # it "should alter the <%= singular_name %> when successful" do
72
+ # @u.name.should == @args['name']
73
+ # @u.description.should == @args['description']
74
+ # @args['name'] = "zalagadoola"
75
+ # @args['description'] = "menchikaboola"
76
+ # put :update, @args
77
+ # response.status.should == 200
78
+ # @u.reload
79
+ # @u.name.should == "zalagadoola"
80
+ # @u.description.should == "menchikaboola"
81
+ # end
82
+
83
+ end
84
+
85
+ end