grape-generator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +104 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/grape +7 -0
- data/grape-generator.gemspec +34 -0
- data/lib/grape/generator.rb +7 -0
- data/lib/grape/generator/cli.rb +160 -0
- data/lib/grape/generator/templates/.gitignore.erb +8 -0
- data/lib/grape/generator/templates/base/Gemfile.erb +5 -0
- data/lib/grape/generator/templates/base/Rakefile.erb +12 -0
- data/lib/grape/generator/templates/base/app/apis/app_api.rb.erb +31 -0
- data/lib/grape/generator/templates/base/app/apis/errors.rb.erb +25 -0
- data/lib/grape/generator/templates/base/app/apis/resources/.keep +0 -0
- data/lib/grape/generator/templates/base/config.ru.erb +10 -0
- data/lib/grape/generator/templates/base/config/boot.rb.erb +25 -0
- data/lib/grape/generator/templates/base/config/environment.rb.erb +0 -0
- data/lib/grape/generator/templates/base/lib/tasks/routes.rake.erb +15 -0
- data/lib/grape/generator/templates/base/log/.keep +0 -0
- data/lib/grape/generator/templates/rails/Gemfile.erb +8 -0
- data/lib/grape/generator/templates/rails/app/apis/errors.rb.erb +27 -0
- data/lib/grape/generator/templates/rails/config.ru.erb +14 -0
- data/lib/grape/generator/templates/rails/lib/tasks/db.rake.erb +7 -0
- data/lib/grape/generator/version.rb +5 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e806516ef6ba2e1a8307e8391cb8e173c5af4dae
|
4
|
+
data.tar.gz: 3230a8143fb38e29567429cf7b6800f314130e61
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 697535646e692b9894da4328cf54e3cb601e40603e5f02343c470647489f450995a2ff9bd81aff40693e43c1bcaaba2942cd29f870223cc5c635b9919b486953
|
7
|
+
data.tar.gz: 21c2081106d3609a961e4ba69288c8c5a35775d0c151883252502c84340a947ca126567b1f6055ba6a184bea7c354fb87013e9512fb5d4e1d768c06fa4b5f8ee
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at arukoh10@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 arukoh
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
# Grape::Generator
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'grape-generator'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install grape-generator
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```
|
22
|
+
$ grape
|
23
|
+
Commands:
|
24
|
+
grape help [COMMAND] # Describe available commands or one specific command
|
25
|
+
grape new NAME # Generate project scaffolding.
|
26
|
+
|
27
|
+
Options:
|
28
|
+
-V, [--verbose], [--no-verbose]
|
29
|
+
```
|
30
|
+
|
31
|
+
### grape new
|
32
|
+
|
33
|
+
```
|
34
|
+
$ grape help new
|
35
|
+
Usage:
|
36
|
+
grape new NAME
|
37
|
+
|
38
|
+
Options:
|
39
|
+
-r, [--rails], [--no-rails] # Generate the ORM with Rails
|
40
|
+
[--rails-new-options=RAILS_NEW_OPTIONS] # Options for rails new
|
41
|
+
# Default: -fBJST
|
42
|
+
-V, [--verbose], [--no-verbose]
|
43
|
+
|
44
|
+
Generate project scaffolding.
|
45
|
+
```
|
46
|
+
|
47
|
+
Examples:
|
48
|
+
|
49
|
+
```
|
50
|
+
$ grape new app
|
51
|
+
inside ./app
|
52
|
+
create app/Gemfile
|
53
|
+
run bundle install --path vendor/bundle --without production from "./app"
|
54
|
+
create app/lib/tasks/routes.rake
|
55
|
+
create app/config/boot.rb
|
56
|
+
create app/config/environment.rb
|
57
|
+
create app/app/apis/errors.rb
|
58
|
+
create app/app/apis/app_api.rb
|
59
|
+
identical app/Gemfile
|
60
|
+
create app/config.ru
|
61
|
+
create app/Rakefile
|
62
|
+
create app/log/.keep
|
63
|
+
create app/app/apis/resources/.keep
|
64
|
+
$ cd app/
|
65
|
+
$ tree -I vendor
|
66
|
+
.
|
67
|
+
├── Gemfile
|
68
|
+
├── Gemfile.lock
|
69
|
+
├── Rakefile
|
70
|
+
├── app
|
71
|
+
│ └── apis
|
72
|
+
│ ├── app_api.rb
|
73
|
+
│ ├── errors.rb
|
74
|
+
│ └── resources
|
75
|
+
├── config
|
76
|
+
│ ├── boot.rb
|
77
|
+
│ └── environment.rb
|
78
|
+
├── config.ru
|
79
|
+
├── lib
|
80
|
+
│ └── tasks
|
81
|
+
│ └── routes.rake
|
82
|
+
└── log
|
83
|
+
|
84
|
+
7 directories, 9 files
|
85
|
+
$ rake routes
|
86
|
+
Verb URI Pattern
|
87
|
+
GET /(.:format)
|
88
|
+
```
|
89
|
+
|
90
|
+
## Development
|
91
|
+
|
92
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
93
|
+
|
94
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/grape-generator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
99
|
+
|
100
|
+
|
101
|
+
## License
|
102
|
+
|
103
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
104
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "grape/generator"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/exe/grape
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'grape/generator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "grape-generator"
|
8
|
+
spec.version = Grape::Generator::VERSION
|
9
|
+
spec.authors = ["arukoh"]
|
10
|
+
spec.email = ["arukoh10@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Grape project generator.}
|
13
|
+
spec.description = %q{Grape project generator.}
|
14
|
+
spec.homepage = "https://github.com/arukoh/grape-generator"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "thor"
|
31
|
+
spec.add_dependency "bundler"
|
32
|
+
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
require_relative 'version'
|
2
|
+
|
3
|
+
require 'thor'
|
4
|
+
require 'ostruct'
|
5
|
+
require 'pathname'
|
6
|
+
|
7
|
+
module Grape
|
8
|
+
module Generator
|
9
|
+
class CLI < Thor
|
10
|
+
class << self
|
11
|
+
def source_root
|
12
|
+
'templates'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
include Thor::Actions
|
17
|
+
|
18
|
+
class_option :verbose, type: :boolean, aliases: '-V', default: false
|
19
|
+
|
20
|
+
desc "new NAME", "Generate project scaffolding."
|
21
|
+
option :rails, type: :boolean, aliases: '-r', default: false,
|
22
|
+
desc: "Generate the ORM with Rails"
|
23
|
+
option :rails_new_options, type: :string, default: '-fBJST',
|
24
|
+
desc: "Options for rails new"
|
25
|
+
def new(name)
|
26
|
+
@config = {capture: !options[:verbose], verbose: true}
|
27
|
+
current_dir = Pathname.pwd
|
28
|
+
case name
|
29
|
+
when "."
|
30
|
+
app_name = current_dir.basename.to_path
|
31
|
+
work_dir = current_dir
|
32
|
+
else
|
33
|
+
raise "#{name} is already exists." if File.exists?(name)
|
34
|
+
app_name = name
|
35
|
+
work_dir = current_dir.join(name)
|
36
|
+
end
|
37
|
+
|
38
|
+
params = build_params(work_dir, app_name)
|
39
|
+
Dir[File.join(template_base, "**", "{*.*,.keep}")].each do |src|
|
40
|
+
dst = Pathname.new src.sub(template_base, work_dir.to_path)
|
41
|
+
if dst.basename.to_path == "app_api.rb.erb"
|
42
|
+
dst = dst.dirname.join("#{params[:app][:name_underscore]}_api.rb.erb")
|
43
|
+
end
|
44
|
+
template(src, dst.sub_ext("").to_path, params)
|
45
|
+
end
|
46
|
+
|
47
|
+
if options[:rails]
|
48
|
+
rails_dir = work_dir.join("rails")
|
49
|
+
setup_dir = work_dir.join(app_name)
|
50
|
+
rails_new(setup_dir, options[:rails_new_options], params)
|
51
|
+
|
52
|
+
Dir[File.join(template_rails, "**", "{*.*,.keep}")].each do |src|
|
53
|
+
dst = Pathname.new src.sub(template_rails, work_dir.to_path)
|
54
|
+
if dst.basename.to_path == "app_api.rb.erb"
|
55
|
+
dst = dst.dirname.join("#{params[:app][:name_underscore]}_api.rb.erb")
|
56
|
+
end
|
57
|
+
dst_path = dst.sub_ext("").to_path
|
58
|
+
template(src, dst_path, params.merge(force: :true))
|
59
|
+
end
|
60
|
+
|
61
|
+
inside(work_dir, @config) do
|
62
|
+
exec(["mv #{setup_dir.to_path} #{rails_dir.to_path}", "bundle install" ])
|
63
|
+
end
|
64
|
+
end
|
65
|
+
rescue
|
66
|
+
puts $!.backtrace
|
67
|
+
error_and_exit($!.message)
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
no_tasks do
|
72
|
+
def templates_path(*item)
|
73
|
+
File.expand_path(File.join(File.dirname(__FILE__), ["templates"] + item))
|
74
|
+
end
|
75
|
+
|
76
|
+
def template_base
|
77
|
+
@template_base ||= templates_path('base')
|
78
|
+
end
|
79
|
+
|
80
|
+
def template_rails
|
81
|
+
@template_rails ||= templates_path('rails')
|
82
|
+
end
|
83
|
+
|
84
|
+
def error_and_exit(status)
|
85
|
+
error(status)
|
86
|
+
exit(1)
|
87
|
+
end
|
88
|
+
|
89
|
+
def exec(commands)
|
90
|
+
commands.map do |cmd|
|
91
|
+
run cmd, @config
|
92
|
+
raise "Failed in exec `#{cmd}`" unless $?.exitstatus == 0
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def run_ruby(cmd)
|
97
|
+
run("bundle exec ruby -e \"#{cmd.join(";")}\"", capture: true, verbose: false).chomp
|
98
|
+
end
|
99
|
+
|
100
|
+
def build_params(work_dir, app_name)
|
101
|
+
params = { app: {name: app_name} }
|
102
|
+
inside(work_dir, @config) do |dst|
|
103
|
+
template(File.join(template_base, "Gemfile.erb"), work_dir.join("Gemfile").to_path)
|
104
|
+
exec([ "bundle install --path vendor/bundle --without production" ])
|
105
|
+
params[:app][:name_classify], params[:app][:name_underscore] = run_ruby(
|
106
|
+
[
|
107
|
+
"require 'active_support'",
|
108
|
+
"require 'active_support/core_ext'",
|
109
|
+
"puts '#{app_name}'.classify + ',' + '#{app_name}'.underscore"
|
110
|
+
]).split(',')
|
111
|
+
end
|
112
|
+
params
|
113
|
+
end
|
114
|
+
|
115
|
+
def database_gem_entry(database)
|
116
|
+
run_ruby([
|
117
|
+
"require 'rails'",
|
118
|
+
"require 'rails/generators'",
|
119
|
+
"require 'rails/generators/rails/app/app_generator'",
|
120
|
+
"options = {}",
|
121
|
+
database ? "options[:database] = database" : "",
|
122
|
+
"generator = Rails::Generators::AppGenerator.new ['rails'], options",
|
123
|
+
"gem_name, gem_version = generator.send('gem_for_database')",
|
124
|
+
"puts File.readlines('Gemfile').find{|line| line =~ /^gem '\#{gem_name}/ }.chomp",
|
125
|
+
])
|
126
|
+
end
|
127
|
+
|
128
|
+
def rails_new(dir, options, params)
|
129
|
+
inside(dir, @config) do |dst|
|
130
|
+
exec([
|
131
|
+
"bundle init",
|
132
|
+
"echo \"gem 'rails'\" >> Gemfile",
|
133
|
+
"bundle install --path ../vendor/bundle --without production",
|
134
|
+
"bundle exec rails new . #{options}",
|
135
|
+
"bundle install",
|
136
|
+
"mv db ../",
|
137
|
+
"ln -s ../db db",
|
138
|
+
])
|
139
|
+
options =~ /-d (.+)/i
|
140
|
+
params[:app][:database_gem_entry] = database_gem_entry($1)
|
141
|
+
end
|
142
|
+
|
143
|
+
inside(dir.join("app"), @config) do
|
144
|
+
exec([
|
145
|
+
"mv models ../../app",
|
146
|
+
"ln -s ../../app/models models",
|
147
|
+
])
|
148
|
+
end
|
149
|
+
|
150
|
+
inside(dir.join("config"), @config) do
|
151
|
+
exec([
|
152
|
+
"mv database.yml ../../config",
|
153
|
+
"ln -s ../../config/database.yml database.yml",
|
154
|
+
])
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'rubygems'
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must \`gem install bundler\` and \`bundle install\` to run rake tasks'
|
7
|
+
end
|
8
|
+
require 'rake'
|
9
|
+
require_relative 'config/boot'
|
10
|
+
|
11
|
+
Rake::Task.define_task(:environment)
|
12
|
+
Dir[File.expand_path('./lib/tasks/*.rake')].each{|f| load f }
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'errors'
|
2
|
+
|
3
|
+
module <%= config[:app][:name_classify] %>
|
4
|
+
class API < Grape::API
|
5
|
+
content_type :json, 'application/json'
|
6
|
+
format :json
|
7
|
+
default_format :json
|
8
|
+
|
9
|
+
rescue_from :all do |e|
|
10
|
+
code = Errors::ERROR_MAPPING[e.class] || 500
|
11
|
+
if code == 500 && ENV["RACK_ENV"] != "production"
|
12
|
+
raise e
|
13
|
+
else
|
14
|
+
error!({reason: Errors.error_reason(code)}, code)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
helpers do
|
19
|
+
def logger
|
20
|
+
env['app.logger'] #API.logger
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
get '/' do
|
25
|
+
{ status: 'alive' }
|
26
|
+
end
|
27
|
+
|
28
|
+
# require_relative 'resources/resources_api'
|
29
|
+
# mount ResourcesAPI
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module <%= config[:app][:name_classify] %>
|
2
|
+
class Errors
|
3
|
+
class << self
|
4
|
+
def error_reason(code)
|
5
|
+
Rack::Utils::HTTP_STATUS_CODES[code]
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Error < StandardError; end
|
10
|
+
class AuthenticationFailed < Error; end
|
11
|
+
class PermissionDenied < Error; end
|
12
|
+
class TooMenyRequests < Error; end
|
13
|
+
class ResourceNotFound < Error; end
|
14
|
+
|
15
|
+
ERROR_MAPPING = {
|
16
|
+
Grape::Exceptions::ValidationErrors => 400,
|
17
|
+
Grape::Exceptions::InvalidMessageBody => 400,
|
18
|
+
AuthenticationFailed => 401,
|
19
|
+
PermissionDenied => 403,
|
20
|
+
ResourceNotFound => 404,
|
21
|
+
TooMenyRequests => 429
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
File without changes
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<% name = config[:app][:name_classify] %>
|
2
|
+
#!/usr/bin/env rackup
|
3
|
+
require File.expand_path("../config/boot", __FILE__)
|
4
|
+
|
5
|
+
require "active_support"
|
6
|
+
logger = ActiveSupport::Logger.new("log/#{ENV["RACK_ENV"]}.log")
|
7
|
+
logger.level = ENV["RACK_ENV"] == "production" ? Logger::INFO : Logger::DEBUG
|
8
|
+
use <%= name %>::AppLogger, logger
|
9
|
+
|
10
|
+
run <%= name %>::API
|
@@ -0,0 +1,25 @@
|
|
1
|
+
ENV['RACK_ENV'] ||= 'development'
|
2
|
+
|
3
|
+
require 'rubygems' unless defined?(Gem)
|
4
|
+
require 'bundler/setup'
|
5
|
+
Bundler.require(:default, ENV["RACK_ENV"].to_sym)
|
6
|
+
|
7
|
+
Dir[File.expand_path('../../app/models/*.rb', __FILE__)].each{|f| require f }
|
8
|
+
|
9
|
+
module <%= config[:app][:name_classify] %>
|
10
|
+
class AppLogger < Rack::CommonLogger
|
11
|
+
def initialize(app, logger)
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
env["app.logger"] = @logger
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
|
22
|
+
$LOAD_PATH.unshift(File.expand_path("../../app/apis", __FILE__))
|
23
|
+
require '<%= config[:app][:name_underscore] %>_api'
|
24
|
+
|
25
|
+
require_relative 'environment'
|
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
desc "API Routes"
|
4
|
+
task routes: :environment do
|
5
|
+
Route = Struct.new(:verb, :path)
|
6
|
+
routes = [Route.new("Verb", "URI Pattern")]
|
7
|
+
<%= config[:app][:name_classify] %>::API.routes.each do |route|
|
8
|
+
routes << Route.new(route.route_method, route.route_path)
|
9
|
+
end
|
10
|
+
max_verb_size = routes.max_by{|r| r.verb.size}.verb.size
|
11
|
+
max_path_size = routes.max_by{|r| r.path.size}.path.size
|
12
|
+
routes.each do |route|
|
13
|
+
puts " #{route.verb.ljust(max_verb_size)} #{route.path.ljust(max_path_size)}"
|
14
|
+
end
|
15
|
+
end
|
File without changes
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module <%= config[:app][:name_classify] %>
|
2
|
+
class Errors
|
3
|
+
class << self
|
4
|
+
def error_reason(code)
|
5
|
+
Rack::Utils::HTTP_STATUS_CODES[code]
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Error < StandardError; end
|
10
|
+
class AuthenticationFailed < Error; end
|
11
|
+
class PermissionDenied < Error; end
|
12
|
+
class TooMenyRequests < Error; end
|
13
|
+
class ResourceNotFound < Error; end
|
14
|
+
|
15
|
+
ERROR_MAPPING = {
|
16
|
+
Grape::Exceptions::ValidationErrors => 400,
|
17
|
+
Grape::Exceptions::InvalidMessageBody => 400,
|
18
|
+
ActiveRecord::RecordInvalid => 400,
|
19
|
+
AuthenticationFailed => 401,
|
20
|
+
PermissionDenied => 403,
|
21
|
+
ResourceNotFound => 404,
|
22
|
+
ActiveRecord::RecordNotFound => 404,
|
23
|
+
TooMenyRequests => 429
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<% name = config[:app][:name_classify] %>
|
2
|
+
#!/usr/bin/env rackup
|
3
|
+
require File.expand_path("../config/boot", __FILE__)
|
4
|
+
|
5
|
+
require "active_support"
|
6
|
+
logger = ActiveSupport::Logger.new("log/#{ENV["RACK_ENV"]}.log")
|
7
|
+
logger.level = ENV["RACK_ENV"] == "production" ? Logger::INFO : Logger::DEBUG
|
8
|
+
use <%= name %>::AppLogger, logger
|
9
|
+
|
10
|
+
ActiveRecord::Base.logger = logger
|
11
|
+
ActiveRecord::Base.schema_format = :sql
|
12
|
+
use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
13
|
+
|
14
|
+
run <%= name %>::API
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grape-generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- arukoh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: Grape project generator.
|
56
|
+
email:
|
57
|
+
- arukoh10@gmail.com
|
58
|
+
executables:
|
59
|
+
- grape
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- exe/grape
|
72
|
+
- grape-generator.gemspec
|
73
|
+
- lib/grape/generator.rb
|
74
|
+
- lib/grape/generator/cli.rb
|
75
|
+
- lib/grape/generator/templates/.gitignore.erb
|
76
|
+
- lib/grape/generator/templates/base/Gemfile.erb
|
77
|
+
- lib/grape/generator/templates/base/Rakefile.erb
|
78
|
+
- lib/grape/generator/templates/base/app/apis/app_api.rb.erb
|
79
|
+
- lib/grape/generator/templates/base/app/apis/errors.rb.erb
|
80
|
+
- lib/grape/generator/templates/base/app/apis/resources/.keep
|
81
|
+
- lib/grape/generator/templates/base/config.ru.erb
|
82
|
+
- lib/grape/generator/templates/base/config/boot.rb.erb
|
83
|
+
- lib/grape/generator/templates/base/config/environment.rb.erb
|
84
|
+
- lib/grape/generator/templates/base/lib/tasks/routes.rake.erb
|
85
|
+
- lib/grape/generator/templates/base/log/.keep
|
86
|
+
- lib/grape/generator/templates/rails/Gemfile.erb
|
87
|
+
- lib/grape/generator/templates/rails/app/apis/errors.rb.erb
|
88
|
+
- lib/grape/generator/templates/rails/config.ru.erb
|
89
|
+
- lib/grape/generator/templates/rails/lib/tasks/db.rake.erb
|
90
|
+
- lib/grape/generator/version.rb
|
91
|
+
homepage: https://github.com/arukoh/grape-generator
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata:
|
95
|
+
allowed_push_host: https://rubygems.org
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.4.5.1
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Grape project generator.
|
116
|
+
test_files: []
|