grapethor 0.2.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 (92) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.travis.yml +22 -0
  4. data/CHANGELOG.md +13 -0
  5. data/CODE_OF_CONDUCT.md +76 -0
  6. data/Gemfile +3 -0
  7. data/Gemfile.lock +53 -0
  8. data/LICENSE +19 -0
  9. data/README.md +165 -0
  10. data/Rakefile +9 -0
  11. data/_config.yml +1 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/exe/grapethor +5 -0
  15. data/grapethor.gemspec +47 -0
  16. data/lib/grapethor.rb +10 -0
  17. data/lib/grapethor/generators/api.rb +78 -0
  18. data/lib/grapethor/generators/cli.rb +35 -0
  19. data/lib/grapethor/generators/endpoint.rb +168 -0
  20. data/lib/grapethor/generators/new.rb +161 -0
  21. data/lib/grapethor/generators/resource.rb +89 -0
  22. data/lib/grapethor/templates/api/api/%api_version%/base.rb.tt +19 -0
  23. data/lib/grapethor/templates/api_minitest/test/support/api_%api_version%_test_case.rb.tt +12 -0
  24. data/lib/grapethor/templates/api_rspec/.gitkeep +0 -0
  25. data/lib/grapethor/templates/docker/Dockerfile.tt +18 -0
  26. data/lib/grapethor/templates/docker/docker-compose.yml.tt +43 -0
  27. data/lib/grapethor/templates/endpoint.rb.erb +14 -0
  28. data/lib/grapethor/templates/endpoint/api/%api_version%/%end_resource_plural%.rb.tt +14 -0
  29. data/lib/grapethor/templates/endpoint_minitest.rb.erb +13 -0
  30. data/lib/grapethor/templates/endpoint_minitest/test/api/%api_version%/%end_resource_plural%_test.rb.tt +14 -0
  31. data/lib/grapethor/templates/endpoint_rspec.rb.erb +11 -0
  32. data/lib/grapethor/templates/endpoint_rspec/spec/api/%api_version%/%end_resource_plural%_spec.rb.tt +12 -0
  33. data/lib/grapethor/templates/license/apache2/LICENSE.tt +13 -0
  34. data/lib/grapethor/templates/license/cddl1/LICENSE.tt +316 -0
  35. data/lib/grapethor/templates/license/epl1/LICENSE.tt +200 -0
  36. data/lib/grapethor/templates/license/freebsd/LICENSE.tt +22 -0
  37. data/lib/grapethor/templates/license/gpl2/LICENSE.tt +17 -0
  38. data/lib/grapethor/templates/license/gpl3/LICENSE.tt +16 -0
  39. data/lib/grapethor/templates/license/mit/LICENSE.tt +19 -0
  40. data/lib/grapethor/templates/license/mpl2/LICENSE.tt +5 -0
  41. data/lib/grapethor/templates/license/newbsd/LICENSE.tt +26 -0
  42. data/lib/grapethor/templates/minitest/lib/tasks/test.rake +14 -0
  43. data/lib/grapethor/templates/minitest/test/support/reporters.rb +11 -0
  44. data/lib/grapethor/templates/minitest/test/support/test_case.rb +18 -0
  45. data/lib/grapethor/templates/minitest/test/test_helper.rb +12 -0
  46. data/lib/grapethor/templates/mysql/config/database.yml.tt +33 -0
  47. data/lib/grapethor/templates/new/.gitignore +7 -0
  48. data/lib/grapethor/templates/new/.grapethor.yml.tt +13 -0
  49. data/lib/grapethor/templates/new/Gemfile.tt +44 -0
  50. data/lib/grapethor/templates/new/README.md.tt +138 -0
  51. data/lib/grapethor/templates/new/Rakefile.tt +30 -0
  52. data/lib/grapethor/templates/new/api/base.rb.tt +19 -0
  53. data/lib/grapethor/templates/new/app/exceptions/not_found_error.rb +1 -0
  54. data/lib/grapethor/templates/new/bin/console +13 -0
  55. data/lib/grapethor/templates/new/bin/server.tt +14 -0
  56. data/lib/grapethor/templates/new/bin/setup.tt +8 -0
  57. data/lib/grapethor/templates/new/config.ru.tt +19 -0
  58. data/lib/grapethor/templates/new/config/application.rb.tt +18 -0
  59. data/lib/grapethor/templates/new/config/boot.rb +2 -0
  60. data/lib/grapethor/templates/new/config/environment.rb +3 -0
  61. data/lib/grapethor/templates/new/db/seeds.rb +7 -0
  62. data/lib/grapethor/templates/new/lib/tasks/routes.rake +8 -0
  63. data/lib/grapethor/templates/postgresql/config/database.yml.tt +38 -0
  64. data/lib/grapethor/templates/resource/api/%api_version%/%res_name_plural%.rb.tt +95 -0
  65. data/lib/grapethor/templates/resource/api/entities/%res_name%.rb.tt +10 -0
  66. data/lib/grapethor/templates/resource/app/models/%res_name%.rb.tt +6 -0
  67. data/lib/grapethor/templates/resource/db/migrations/%res_migration%.rb.tt +10 -0
  68. data/lib/grapethor/templates/resource_minitest/test/api/%api_version%/%res_name_plural%_test.rb.tt +82 -0
  69. data/lib/grapethor/templates/resource_minitest/test/fixtures/%res_name_plural%.yml.tt +9 -0
  70. data/lib/grapethor/templates/resource_minitest/test/models/%res_name%_test.rb.tt +17 -0
  71. data/lib/grapethor/templates/resource_rspec/spec/api/%api_version%/%res_name_plural%_spec.rb.tt +91 -0
  72. data/lib/grapethor/templates/resource_rspec/spec/factories/%res_name_plural%.rb.tt +7 -0
  73. data/lib/grapethor/templates/resource_rspec/spec/models/%res_name%_spec.rb.tt +9 -0
  74. data/lib/grapethor/templates/rspec/.rspec +1 -0
  75. data/lib/grapethor/templates/rspec/lib/tasks/spec.rake +4 -0
  76. data/lib/grapethor/templates/rspec/spec/spec_helper.rb +24 -0
  77. data/lib/grapethor/templates/sqlite/config/database.yml.tt +22 -0
  78. data/lib/grapethor/templates/swagger/swagger-ui/favicon-16x16.png +0 -0
  79. data/lib/grapethor/templates/swagger/swagger-ui/favicon-32x32.png +0 -0
  80. data/lib/grapethor/templates/swagger/swagger-ui/index.html.tt +60 -0
  81. data/lib/grapethor/templates/swagger/swagger-ui/oauth2-redirect.html +67 -0
  82. data/lib/grapethor/templates/swagger/swagger-ui/swagger-ui-bundle.js +93 -0
  83. data/lib/grapethor/templates/swagger/swagger-ui/swagger-ui-bundle.js.map +1 -0
  84. data/lib/grapethor/templates/swagger/swagger-ui/swagger-ui-standalone-preset.js +14 -0
  85. data/lib/grapethor/templates/swagger/swagger-ui/swagger-ui-standalone-preset.js.map +1 -0
  86. data/lib/grapethor/templates/swagger/swagger-ui/swagger-ui.css +3 -0
  87. data/lib/grapethor/templates/swagger/swagger-ui/swagger-ui.css.map +1 -0
  88. data/lib/grapethor/templates/swagger/swagger-ui/swagger-ui.js +9 -0
  89. data/lib/grapethor/templates/swagger/swagger-ui/swagger-ui.js.map +1 -0
  90. data/lib/grapethor/utils/utils.rb +116 -0
  91. data/lib/grapethor/version.rb +3 -0
  92. metadata +235 -0
@@ -0,0 +1,138 @@
1
+ # <%= app_name %>
2
+
3
+ ## Introduction
4
+
5
+ <%= app_name %> information
6
+
7
+ ## Dependencies
8
+
9
+ - Ruby <%= app_ruby %>
10
+
11
+ ## Installation
12
+
13
+ Clone poject
14
+
15
+ Run the setup script:
16
+
17
+ ```
18
+ $ bin/setup
19
+ ```
20
+
21
+ Run application:
22
+
23
+ ```
24
+ $ bin/server
25
+ ```
26
+
27
+ ## Console
28
+
29
+ To use console, run the following command:
30
+
31
+ ```
32
+ $ bin/console
33
+ ```
34
+
35
+ ## Tests
36
+
37
+ To execute tests, run the following command:
38
+
39
+ ```
40
+ $ bundle exec rake test
41
+ ```
42
+
43
+ ## Routes
44
+
45
+ To show the application routes, run the following command:
46
+
47
+ ```
48
+ $ bundle exec rake routes
49
+ ```
50
+
51
+ <% if app_swagger? -%>
52
+ ## Swagger Documentation
53
+
54
+ To access swagger documentation, enter the root application address in the browser:
55
+
56
+ ```
57
+ http://localhost:9292
58
+ ```
59
+ <% end -%>
60
+
61
+ <% if app_docker? -%>
62
+ ## Docker
63
+
64
+ Install Docker and Docker-Compose
65
+
66
+ Clone the project
67
+
68
+ Build the local Dockerfile
69
+
70
+ ```
71
+ $ docker-compose build
72
+ ```
73
+
74
+ Setup the application:
75
+
76
+ ```
77
+ $ docker-compose run app bundle exec bin/setup
78
+ ```
79
+
80
+ Initialize database
81
+
82
+ ```
83
+ $ docker-compose run app bundle exec rake [db:drop] db:create db:migrate db:seed
84
+ ```
85
+
86
+ Get the application cluster running (`-d` - in the background)
87
+
88
+ ```
89
+ $ docker-compose up -d
90
+ ```
91
+
92
+ Enter application console (within running `app` container)
93
+
94
+ ```
95
+ $ docker-compose exec app bundle exec bin/console
96
+ ```
97
+
98
+ Run the test suite
99
+
100
+ ```
101
+ docker-compose exec app bundle exec rake db:setup
102
+ docker-compose exec app bundle exec rake test
103
+ ```
104
+
105
+ Inspect logs:
106
+
107
+ ```
108
+ $ docker-compose logs -f
109
+ ```
110
+
111
+ Restart the cluster
112
+
113
+ ```
114
+ $ docker-compose restart
115
+ ```
116
+
117
+ Stop (but not destroy) the cluster
118
+
119
+ ```
120
+ $ docker-compose stop
121
+ ```
122
+
123
+ Start (a stopped) cluster
124
+
125
+ ```
126
+ $ docker-compose start
127
+ ```
128
+
129
+ Destroy the cluster (usefull if config or state gets messed up)
130
+
131
+ ```
132
+ $ docker-compose down
133
+ ```
134
+ <% end -%>
135
+
136
+ ## License
137
+
138
+ The software is available as open source under the terms of the <%= app_license_link %>.
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts 'Run `bundle install` to install missing gems'
9
+ exit e.status_code
10
+ end
11
+
12
+ <% if app_orm == 'activerecord' -%>
13
+ load 'tasks/otr-activerecord.rake'
14
+
15
+ OTR::ActiveRecord.db_dir = 'db'
16
+ OTR::ActiveRecord.migrations_paths = ['db/migrations']
17
+ OTR::ActiveRecord.fixtures_path = 'test/fixtures'
18
+ OTR::ActiveRecord.seed_file = 'seeds.rb'
19
+
20
+ namespace :db do
21
+ # Some db tasks require your app code to be loaded; they'll expect to find it here
22
+ task :environment do
23
+ ENV['RACK_ENV'] ||= 'development'
24
+ require File.expand_path('config/environment', __dir__)
25
+ end
26
+ end
27
+ <% end -%>
28
+
29
+ #Load tasks
30
+ Dir.glob('lib/tasks/*.rake').each { |task| load task }
@@ -0,0 +1,19 @@
1
+ module API
2
+ class Base < Grape::API
3
+ <% unless app_prefix.empty? -%>
4
+ prefix :<%= app_prefix %>
5
+ <% end -%>
6
+
7
+ # mount API<VERSION>::Base
8
+
9
+ <% if app_swagger? -%>
10
+ add_swagger_documentation \
11
+ info: {
12
+ title: '<%= app_name %>',
13
+ description: '<%= app_name %> - generated with Grapethor'
14
+ }
15
+ <% end -%>
16
+
17
+ end
18
+ end
19
+
@@ -0,0 +1 @@
1
+ class NotFoundError < StandardError; end
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path('../config/environment', __dir__)
4
+
5
+ # You can add fixtures and/or initialization code here to make experimenting
6
+ # with your gem easier. You can also use a different console, if you like.
7
+
8
+ # (If you use this, don't forget to add pry to your Gemfile!)
9
+ # require "pry"
10
+ # Pry.start
11
+
12
+ require "irb"
13
+ IRB.start
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ while getopts p: option
6
+ do
7
+ case "${option}"
8
+ in
9
+ p) PORT=${OPTARG};;
10
+ esac
11
+ done
12
+
13
+ echo "Starting <%= app_name %> application server..."
14
+ bundle exec rackup -p ${PORT:-9292}
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -v
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ require File.expand_path('config/environment', __dir__)
2
+
3
+ <% if app_orm == 'activerecord' -%>
4
+ use OTR::ActiveRecord::ConnectionManagement
5
+ <% end -%>
6
+
7
+ use Rack::Cors do
8
+ allow do
9
+ origins '*'
10
+ resource '*', headers: :any, methods: [ :get, :post, :put, :delete, :options ]
11
+ end
12
+ end
13
+
14
+ use Rack::Static,
15
+ root: File.expand_path('./swagger-ui', __dir__),
16
+ urls: ["/swagger"],
17
+ index: 'index.html'
18
+
19
+ run API::Base
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift File.expand_path('../api', __dir__)
2
+ $LOAD_PATH.unshift File.expand_path('../app', __dir__)
3
+ $LOAD_PATH.unshift __dir__
4
+
5
+ require 'boot'
6
+ require 'erb'
7
+
8
+ Bundler.require :default, ENV['RACK_ENV']
9
+
10
+ <% if app_orm == 'activerecord' -%>
11
+ OTR::ActiveRecord.configure_from_file! "config/database.yml"
12
+ <% end -%>
13
+
14
+ require_rel '../app'
15
+ require_rel '../lib'
16
+ require_rel Dir.glob('../api/*/*', base: __dir__).reject { |f| f =~ /base\.rb/ }
17
+ require_rel '../api/*/base.rb'
18
+ require_rel '../api/base.rb'
@@ -0,0 +1,2 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
@@ -0,0 +1,3 @@
1
+ ENV['RACK_ENV'] ||= 'development'
2
+
3
+ require File.expand_path('application', __dir__)
@@ -0,0 +1,7 @@
1
+ # This file should contain all the record creation needed to seed the database with its default values.
2
+ # The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
3
+ #
4
+ # Examples:
5
+ #
6
+ # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
7
+ # Character.create(name: 'Luke', movie: movies.first)
@@ -0,0 +1,8 @@
1
+ desc "API Routes"
2
+ task routes: :environment do
3
+ API::Base.routes.each do |api|
4
+ method = api.request_method.ljust(10)
5
+ path = api.path
6
+ puts " #{method} #{path}"
7
+ end
8
+ end
@@ -0,0 +1,38 @@
1
+ default: &default
2
+ adapter: postgresql
3
+ encoding: unicode
4
+ port: 5432
5
+
6
+ local: &local
7
+ <<: *default
8
+ <% if app_docker? -%>
9
+ username: postgres
10
+ password:
11
+ host: db
12
+ <% else -%>
13
+ username: <%= app_name.downcase %>
14
+ password: <%= app_name.downcase %>
15
+ host: localhost
16
+ <% end -%>
17
+
18
+ remote: &remote
19
+ <<: *default
20
+ database: <%%= ENV['DB'] %%>
21
+ username: <%%= ENV['DB_USER'] %%>
22
+ password: <%%= ENV['DB_PASSWORD'] %%>
23
+ host: <%%= ENV['DB_HOST'] %%>
24
+ pool: 5
25
+
26
+ development:
27
+ <<: *local
28
+ database: <%= app_name.downcase %>_development
29
+
30
+ test:
31
+ <<: *local
32
+ database: <%= app_name.downcase %>_test
33
+
34
+ staging:
35
+ <<: *remote
36
+
37
+ production:
38
+ <<: *remote
@@ -0,0 +1,95 @@
1
+ module API<%= api_version %>
2
+ class <%= res_name.classify.pluralize %> < Grape::API
3
+ rescue_from NotFoundError, ActiveRecord::RecordNotFound do |exception|
4
+ error!('<%= res_name.classify %> not found', 404)
5
+ end
6
+
7
+ rescue_from ActiveRecord::RecordInvalid do |exception|
8
+ error!(exception.message, 409)
9
+ end
10
+
11
+
12
+ helpers do
13
+ def return_no_content_status
14
+ status :no_content
15
+ ''
16
+ end
17
+
18
+ def <%= res_name %>_params(params)
19
+ {
20
+ <% res_attrs.each_key do |name| -%>
21
+ <%= name %>: params[:<%= name %>],
22
+ <% end -%>
23
+ }
24
+ end
25
+ end
26
+
27
+
28
+ resource :<%= res_name.pluralize %> do
29
+ # GET '<%= api_version%>/<%= res_name.pluralize %>'
30
+ desc 'Returns <%= res_name.pluralize %> list'
31
+ get do
32
+ present <%= res_name.classify %>.all, with: API::Entities::<%= res_name.classify %>
33
+ end
34
+
35
+
36
+ # GET '<%= api_version%>/<%= res_name.pluralize %>/:id'
37
+ desc 'Returns <%= res_name %> by id'
38
+ params do
39
+ requires :id, type: Integer
40
+ end
41
+ get ':id' do
42
+ <%= res_name %> = <%= res_name.classify %>.find(params[:id])
43
+ raise NotFoundError if <%= res_name %>.nil?
44
+
45
+ present <%= res_name %>, with: API::Entities::<%= res_name.classify %>
46
+ end
47
+
48
+
49
+ # POST '<%= api_version%>/<%= res_name.pluralize %>'
50
+ desc 'Creates new <%= res_name %>'
51
+ <% unless res_attrs.empty? -%>
52
+ params do
53
+ <% res_attrs.each do |name, type| -%>
54
+ requires :<%= name %>, type: <%= param_to_type(type) %>
55
+ <% end -%>
56
+ end
57
+ <% end -%>
58
+ post do
59
+ <%= res_name %> = <%= res_name.classify %>.new(<%= res_name %>_params(params))
60
+ <%= res_name %>.save!
61
+
62
+ present <%= res_name %>, with: API::Entities::<%= res_name.classify %>
63
+ end
64
+
65
+
66
+ # PUT '<%= api_version%>/<%= res_name.pluralize %>/:id'
67
+ desc 'Updates <%= res_name %>'
68
+ params do
69
+ requires :id, type: Integer
70
+ <% res_attrs.each do |name, type| -%>
71
+ requires :<%= name %>, type: <%= param_to_type(type) %>
72
+ <% end -%>
73
+ end
74
+ put ':id' do
75
+ <%= res_name %> = <%= res_name.classify %>.find(params[:id])
76
+ <%= res_name %>.update_attributes!(<%= res_name %>_params(params))
77
+
78
+ return_no_content_status
79
+ end
80
+
81
+
82
+ # DELETE '<%= api_version%>/<%= res_name.pluralize %>/:id'
83
+ desc 'Deletes <%= res_name %>'
84
+ params do
85
+ requires :id, type: Integer
86
+ end
87
+ delete ':id' do
88
+ <%= res_name %> = <%= res_name.classify %>.find(params[:id])
89
+ <%= res_name %>.destroy!
90
+
91
+ return_no_content_status
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,10 @@
1
+ module API
2
+ module Entities
3
+ class <%= res_name.classify %> < Grape::Entity
4
+ expose :id
5
+ <% res_attrs.each_key do |name| -%>
6
+ expose :<%= name %>
7
+ <% end -%>
8
+ end
9
+ end
10
+ end