grape-generator 0.1.0 → 0.1.2
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.
- checksums.yaml +4 -4
- data/lib/grape/generator/cli.rb +6 -20
- data/lib/grape/generator/templates/base/app/apis/base.rb.erb +24 -0
- data/lib/grape/generator/templates/base/app/apis/concerns/error_handler.rb.erb +31 -0
- data/lib/grape/generator/templates/base/config.ru.erb +2 -3
- data/lib/grape/generator/templates/base/config/boot.rb.erb +2 -2
- data/lib/grape/generator/templates/rails/app/apis/concerns/error_handler.rb.erb +33 -0
- data/lib/grape/generator/templates/rails/config.ru.erb +2 -3
- data/lib/grape/generator/version.rb +1 -1
- metadata +5 -5
- data/lib/grape/generator/templates/base/app/apis/app_api.rb.erb +0 -31
- data/lib/grape/generator/templates/base/app/apis/errors.rb.erb +0 -25
- data/lib/grape/generator/templates/rails/app/apis/errors.rb.erb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea89c4515b12c647838b5228bebf19eefa3c76cb
|
4
|
+
data.tar.gz: 2705c1baab280db0a496078146328a18b04766f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4bf7ec4153a996c0c625cd7782c6f42d20ab2e77ff1c865b5b5ff2dfaab77fd7c1ea1ed1c0b0af318deb8ac821be04799f66196b1b0b4c375582ee2dcd201916
|
7
|
+
data.tar.gz: 642fcbbfcdb5d979391dd1463fabf399ef4f30bae48bdbe88c7af11c5b665caaa3a36446beda8acc4c0b7d7035b9139568d028d98c1142479caffdef5207e118
|
data/lib/grape/generator/cli.rb
CHANGED
@@ -35,12 +35,9 @@ module Grape
|
|
35
35
|
work_dir = current_dir.join(name)
|
36
36
|
end
|
37
37
|
|
38
|
-
params =
|
38
|
+
params = { app: {name: app_name} }
|
39
39
|
Dir[File.join(template_base, "**", "{*.*,.keep}")].each do |src|
|
40
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
41
|
template(src, dst.sub_ext("").to_path, params)
|
45
42
|
end
|
46
43
|
|
@@ -51,17 +48,18 @@ module Grape
|
|
51
48
|
|
52
49
|
Dir[File.join(template_rails, "**", "{*.*,.keep}")].each do |src|
|
53
50
|
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
51
|
dst_path = dst.sub_ext("").to_path
|
58
52
|
template(src, dst_path, params.merge(force: :true))
|
59
53
|
end
|
60
54
|
|
61
55
|
inside(work_dir, @config) do
|
62
|
-
exec(["mv #{setup_dir.to_path} #{rails_dir.to_path}"
|
56
|
+
exec(["mv #{setup_dir.to_path} #{rails_dir.to_path}"])
|
63
57
|
end
|
64
58
|
end
|
59
|
+
|
60
|
+
inside(work_dir, @config) do |dst|
|
61
|
+
exec([ "bundle install --path vendor/bundle --without production" ])
|
62
|
+
end
|
65
63
|
rescue
|
66
64
|
puts $!.backtrace
|
67
65
|
error_and_exit($!.message)
|
@@ -98,18 +96,6 @@ module Grape
|
|
98
96
|
end
|
99
97
|
|
100
98
|
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
99
|
end
|
114
100
|
|
115
101
|
def database_gem_entry(database)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative 'concerns/error_handler'
|
2
|
+
|
3
|
+
module API
|
4
|
+
class Base < Grape::API
|
5
|
+
include ErrorHandler
|
6
|
+
|
7
|
+
content_type :json, 'application/json'
|
8
|
+
format :json
|
9
|
+
default_format :json
|
10
|
+
|
11
|
+
helpers do
|
12
|
+
def logger
|
13
|
+
env['app.logger'] #API.logger
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
get '/' do
|
18
|
+
{ status: 'alive' }
|
19
|
+
end
|
20
|
+
|
21
|
+
# require_relative 'resources/resources_api'
|
22
|
+
# mount ResourcesAPI
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module API
|
2
|
+
class Error < StandardError; end
|
3
|
+
class AuthenticationFailed < Error; end
|
4
|
+
class PermissionDenied < Error; end
|
5
|
+
class TooMenyRequests < Error; end
|
6
|
+
class ResourceNotFound < Error; end
|
7
|
+
|
8
|
+
ERROR_MAPPING = {
|
9
|
+
Grape::Exceptions::ValidationErrors => 400,
|
10
|
+
Grape::Exceptions::InvalidMessageBody => 400,
|
11
|
+
AuthenticationFailed => 401,
|
12
|
+
PermissionDenied => 403,
|
13
|
+
ResourceNotFound => 404,
|
14
|
+
TooMenyRequests => 429
|
15
|
+
}
|
16
|
+
|
17
|
+
module ErrorHandler
|
18
|
+
extend ActiveSupport::Concern
|
19
|
+
|
20
|
+
included do
|
21
|
+
rescue_from :all do |e|
|
22
|
+
code = ERROR_MAPPING[e.class] || 500
|
23
|
+
if code == 500 && ENV["RACK_ENV"] != "production"
|
24
|
+
raise e
|
25
|
+
else
|
26
|
+
error!({reason: Rack::Utils::HTTP_STATUS_CODES[code]}, code)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -1,10 +1,9 @@
|
|
1
|
-
<% name = config[:app][:name_classify] %>
|
2
1
|
#!/usr/bin/env rackup
|
3
2
|
require File.expand_path("../config/boot", __FILE__)
|
4
3
|
|
5
4
|
require "active_support"
|
6
5
|
logger = ActiveSupport::Logger.new("log/#{ENV["RACK_ENV"]}.log")
|
7
6
|
logger.level = ENV["RACK_ENV"] == "production" ? Logger::INFO : Logger::DEBUG
|
8
|
-
use
|
7
|
+
use API::AppLogger, logger
|
9
8
|
|
10
|
-
run
|
9
|
+
run API::Base
|
@@ -6,7 +6,7 @@ Bundler.require(:default, ENV["RACK_ENV"].to_sym)
|
|
6
6
|
|
7
7
|
Dir[File.expand_path('../../app/models/*.rb', __FILE__)].each{|f| require f }
|
8
8
|
|
9
|
-
module
|
9
|
+
module API
|
10
10
|
class AppLogger < Rack::CommonLogger
|
11
11
|
def initialize(app, logger)
|
12
12
|
super
|
@@ -20,6 +20,6 @@ module <%= config[:app][:name_classify] %>
|
|
20
20
|
end
|
21
21
|
$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
|
22
22
|
$LOAD_PATH.unshift(File.expand_path("../../app/apis", __FILE__))
|
23
|
-
require '
|
23
|
+
require 'base'
|
24
24
|
|
25
25
|
require_relative 'environment'
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module API
|
2
|
+
class Error < StandardError; end
|
3
|
+
class AuthenticationFailed < Error; end
|
4
|
+
class PermissionDenied < Error; end
|
5
|
+
class TooMenyRequests < Error; end
|
6
|
+
class ResourceNotFound < Error; end
|
7
|
+
|
8
|
+
ERROR_MAPPING = {
|
9
|
+
Grape::Exceptions::ValidationErrors => 400,
|
10
|
+
Grape::Exceptions::InvalidMessageBody => 400,
|
11
|
+
ActiveRecord::RecordInvalid => 400,
|
12
|
+
AuthenticationFailed => 401,
|
13
|
+
PermissionDenied => 403,
|
14
|
+
ResourceNotFound => 404,
|
15
|
+
ActiveRecord::RecordNotFound => 404,
|
16
|
+
TooMenyRequests => 429
|
17
|
+
}
|
18
|
+
|
19
|
+
module ErrorHandler
|
20
|
+
extend ActiveSupport::Concern
|
21
|
+
|
22
|
+
included do
|
23
|
+
rescue_from :all do |e|
|
24
|
+
code = ERROR_MAPPING[e.class] || 500
|
25
|
+
if code == 500 && ENV["RACK_ENV"] != "production"
|
26
|
+
raise e
|
27
|
+
else
|
28
|
+
error!({reason: Rack::Utils::HTTP_STATUS_CODES[code]}, code)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,14 +1,13 @@
|
|
1
|
-
<% name = config[:app][:name_classify] %>
|
2
1
|
#!/usr/bin/env rackup
|
3
2
|
require File.expand_path("../config/boot", __FILE__)
|
4
3
|
|
5
4
|
require "active_support"
|
6
5
|
logger = ActiveSupport::Logger.new("log/#{ENV["RACK_ENV"]}.log")
|
7
6
|
logger.level = ENV["RACK_ENV"] == "production" ? Logger::INFO : Logger::DEBUG
|
8
|
-
use
|
7
|
+
use API::AppLogger, logger
|
9
8
|
|
10
9
|
ActiveRecord::Base.logger = logger
|
11
10
|
ActiveRecord::Base.schema_format = :sql
|
12
11
|
use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
13
12
|
|
14
|
-
run
|
13
|
+
run API::Base
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- arukoh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -75,8 +75,8 @@ files:
|
|
75
75
|
- lib/grape/generator/templates/.gitignore.erb
|
76
76
|
- lib/grape/generator/templates/base/Gemfile.erb
|
77
77
|
- lib/grape/generator/templates/base/Rakefile.erb
|
78
|
-
- lib/grape/generator/templates/base/app/apis/
|
79
|
-
- lib/grape/generator/templates/base/app/apis/
|
78
|
+
- lib/grape/generator/templates/base/app/apis/base.rb.erb
|
79
|
+
- lib/grape/generator/templates/base/app/apis/concerns/error_handler.rb.erb
|
80
80
|
- lib/grape/generator/templates/base/app/apis/resources/.keep
|
81
81
|
- lib/grape/generator/templates/base/config.ru.erb
|
82
82
|
- lib/grape/generator/templates/base/config/boot.rb.erb
|
@@ -84,7 +84,7 @@ files:
|
|
84
84
|
- lib/grape/generator/templates/base/lib/tasks/routes.rake.erb
|
85
85
|
- lib/grape/generator/templates/base/log/.keep
|
86
86
|
- lib/grape/generator/templates/rails/Gemfile.erb
|
87
|
-
- lib/grape/generator/templates/rails/app/apis/
|
87
|
+
- lib/grape/generator/templates/rails/app/apis/concerns/error_handler.rb.erb
|
88
88
|
- lib/grape/generator/templates/rails/config.ru.erb
|
89
89
|
- lib/grape/generator/templates/rails/lib/tasks/db.rake.erb
|
90
90
|
- lib/grape/generator/version.rb
|
@@ -1,31 +0,0 @@
|
|
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
|
@@ -1,25 +0,0 @@
|
|
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
|
-
|
@@ -1,27 +0,0 @@
|
|
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
|
-
|