meta-api 0.0.2 → 0.0.3
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/.gitignore +4 -4
- data/CHANGELOG.md +6 -1
- data/Gemfile.lock +1 -1
- data/README.md +9 -7
- data/docs/Rails.md +61 -0
- data/docs//345/246/202/344/275/225/350/264/241/347/214/256.md +10 -0
- data/docs//346/225/231/347/250/213.md +21 -21
- data/examples/rails_app/.gitattributes +5 -0
- data/examples/rails_app/.gitignore +23 -0
- data/examples/rails_app/.rspec +1 -0
- data/examples/rails_app/.ruby-version +1 -0
- data/examples/rails_app/Gemfile +29 -0
- data/examples/rails_app/Gemfile.lock +190 -0
- data/examples/rails_app/README.md +11 -0
- data/examples/rails_app/Rakefile +6 -0
- data/examples/rails_app/app/controllers/application_controller.rb +7 -0
- data/examples/rails_app/app/controllers/concerns/.keep +0 -0
- data/examples/rails_app/app/controllers/data_controller.rb +63 -0
- data/examples/rails_app/app/controllers/swagger_controller.rb +13 -0
- data/examples/rails_app/app/models/concerns/.keep +0 -0
- data/examples/rails_app/bin/rails +4 -0
- data/examples/rails_app/bin/rake +4 -0
- data/examples/rails_app/bin/setup +25 -0
- data/examples/rails_app/config/application.rb +39 -0
- data/examples/rails_app/config/boot.rb +3 -0
- data/examples/rails_app/config/credentials.yml.enc +1 -0
- data/examples/rails_app/config/environment.rb +5 -0
- data/examples/rails_app/config/environments/development.rb +51 -0
- data/examples/rails_app/config/environments/production.rb +65 -0
- data/examples/rails_app/config/environments/test.rb +50 -0
- data/examples/rails_app/config/initializers/cors.rb +16 -0
- data/examples/rails_app/config/initializers/filter_parameter_logging.rb +8 -0
- data/examples/rails_app/config/initializers/inflections.rb +16 -0
- data/examples/rails_app/config/initializers/meta_rails_plugin.rb +3 -0
- data/examples/rails_app/config/locales/en.yml +33 -0
- data/examples/rails_app/config/puma.rb +43 -0
- data/examples/rails_app/config/routes.rb +13 -0
- data/examples/rails_app/config.ru +6 -0
- data/examples/rails_app/lib/tasks/.keep +0 -0
- data/examples/rails_app/log/.keep +0 -0
- data/examples/rails_app/public/robots.txt +1 -0
- data/examples/rails_app/spec/data_controller_spec.rb +60 -0
- data/examples/rails_app/spec/rails_helper.rb +55 -0
- data/examples/rails_app/spec/spec_helper.rb +94 -0
- data/examples/rails_app/spec/swagger_controller_spec.rb +13 -0
- data/examples/rails_app/tmp/.keep +0 -0
- data/examples/rails_app/tmp/pids/.keep +0 -0
- data/lib/meta/application/execution.rb +3 -11
- data/lib/meta/application/{meta.rb → metadata.rb} +5 -15
- data/lib/meta/application/parameters.rb +47 -0
- data/lib/meta/application/route.rb +2 -2
- data/lib/meta/json_schema/builders/array_schema_builder.rb +11 -10
- data/lib/meta/json_schema/builders/dynamic_schema_builder.rb +23 -0
- data/lib/meta/json_schema/builders/object_schema_builder.rb +4 -18
- data/lib/meta/json_schema/builders/ref_schema_builder.rb +19 -0
- data/lib/meta/json_schema/builders/schema_builder_tool.rb +26 -1
- data/lib/meta/json_schema/schemas/array_schema.rb +3 -3
- data/lib/meta/json_schema/schemas/base_schema.rb +29 -69
- data/lib/meta/json_schema/schemas/dynamic_schema.rb +29 -0
- data/lib/meta/json_schema/schemas/object_schema.rb +27 -113
- data/lib/meta/json_schema/schemas/properties.rb +157 -0
- data/lib/meta/json_schema/schemas/ref_schema.rb +35 -0
- data/lib/meta/json_schema/schemas.rb +0 -2
- data/lib/meta/json_schema/support/schema_options.rb +21 -12
- data/lib/meta/json_schema.rb +2 -0
- data/lib/meta/rails.rb +98 -0
- data/lib/meta/route_dsl/parameters_builder.rb +2 -1
- data/lib/meta/swagger_doc.rb +5 -2
- data/lib/meta/utils/kwargs/builder.rb +115 -0
- data/lib/meta/utils/{kwargs.rb → kwargs/check.rb} +22 -22
- data/meta-api.gemspec +1 -1
- metadata +55 -4
@@ -0,0 +1,63 @@
|
|
1
|
+
class DataController < ApplicationController
|
2
|
+
route '/parse_params', :post do
|
3
|
+
params do
|
4
|
+
param :q, in: 'query'
|
5
|
+
param :user, required: true do
|
6
|
+
param :name, type: 'string', default: 'Jim'
|
7
|
+
param :age, type: 'integer', default: 18
|
8
|
+
param :foo, param: false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
def parse_params
|
13
|
+
render json: { params_on_schema: params_on_schema }
|
14
|
+
end
|
15
|
+
|
16
|
+
route '/render_hash', :post do
|
17
|
+
status 200 do
|
18
|
+
expose :user, required: true do
|
19
|
+
expose :name, type: 'string', default: 'Jim'
|
20
|
+
expose :age, type: 'integer', default: 18
|
21
|
+
end
|
22
|
+
end
|
23
|
+
status 201 do
|
24
|
+
expose :user, required: true do
|
25
|
+
expose :name, type: 'string', default: 'Jack'
|
26
|
+
expose :age, type: 'integer', default: 20
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
def render_hash
|
31
|
+
render json_on_schema: { 'user' => params[:user].to_unsafe_h }, status: params[:status] || 200, scope: 'foo'
|
32
|
+
end
|
33
|
+
|
34
|
+
route '/render_object', :post do
|
35
|
+
status 200 do
|
36
|
+
expose :user, required: true do
|
37
|
+
expose :name, type: 'string', default: 'Jim'
|
38
|
+
expose :age, type: 'integer', default: 18
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
def render_object
|
43
|
+
user = Object.new
|
44
|
+
def user.name; 'Jim'; end
|
45
|
+
def user.age; 18; end
|
46
|
+
render json_on_schema: { 'user' => user }
|
47
|
+
end
|
48
|
+
|
49
|
+
route '/render_with_options', :post do
|
50
|
+
status 200 do
|
51
|
+
expose :user, required: true do
|
52
|
+
expose :a, value: -> { @a }
|
53
|
+
expose :b, scope: 'foo'
|
54
|
+
expose :c, scope: 'bar'
|
55
|
+
expose :d, default: 'd', render: false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
def render_with_options
|
60
|
+
@a = 'aa'
|
61
|
+
render json_on_schema: { 'user' => params[:user].to_unsafe_hash }, scope: 'foo'
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class SwaggerController < ApplicationController
|
2
|
+
def get_spec
|
3
|
+
# 需要提前加载控制器常量,如以下两种方式:
|
4
|
+
# - Rails.application.eager_load!
|
5
|
+
# - Dir.glob(Rails.root.join('app', 'controllers', '**', '*.rb')).each { |f| require f }
|
6
|
+
#
|
7
|
+
# 这里仅仅仅仅使用了 require_relative
|
8
|
+
require_relative 'data_controller'
|
9
|
+
|
10
|
+
doc = Meta::Rails::Plugin.generate_swagger_doc(ApplicationController)
|
11
|
+
render json: doc
|
12
|
+
end
|
13
|
+
end
|
File without changes
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = File.expand_path("..", __dir__)
|
6
|
+
|
7
|
+
def system!(*args)
|
8
|
+
system(*args) || abort("\n== Command #{args} failed ==")
|
9
|
+
end
|
10
|
+
|
11
|
+
FileUtils.chdir APP_ROOT do
|
12
|
+
# This script is a way to set up or update your development environment automatically.
|
13
|
+
# This script is idempotent, so that you can run it at any time and get an expectable outcome.
|
14
|
+
# Add necessary setup steps to this file.
|
15
|
+
|
16
|
+
puts "== Installing dependencies =="
|
17
|
+
system! "gem install bundler --conservative"
|
18
|
+
system("bundle check") || system!("bundle install")
|
19
|
+
|
20
|
+
puts "\n== Removing old logs and tempfiles =="
|
21
|
+
system! "bin/rails log:clear tmp:clear"
|
22
|
+
|
23
|
+
puts "\n== Restarting application server =="
|
24
|
+
system! "bin/rails restart"
|
25
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative "boot"
|
2
|
+
|
3
|
+
require "rails"
|
4
|
+
# Pick the frameworks you want:
|
5
|
+
require "active_model/railtie"
|
6
|
+
# require "active_job/railtie"
|
7
|
+
# require "active_record/railtie"
|
8
|
+
# require "active_storage/engine"
|
9
|
+
require "action_controller/railtie"
|
10
|
+
# require "action_mailer/railtie"
|
11
|
+
# require "action_mailbox/engine"
|
12
|
+
# require "action_text/engine"
|
13
|
+
require "action_view/railtie"
|
14
|
+
# require "action_cable/engine"
|
15
|
+
require "rails/test_unit/railtie"
|
16
|
+
|
17
|
+
# Require the gems listed in Gemfile, including any gems
|
18
|
+
# you've limited to :test, :development, or :production.
|
19
|
+
Bundler.require(*Rails.groups)
|
20
|
+
|
21
|
+
module RailsApp
|
22
|
+
class Application < Rails::Application
|
23
|
+
# Initialize configuration defaults for originally generated Rails version.
|
24
|
+
config.load_defaults 7.0
|
25
|
+
|
26
|
+
# Configuration for the application, engines, and railties goes here.
|
27
|
+
#
|
28
|
+
# These settings can be overridden in specific environments using the files
|
29
|
+
# in config/environments, which are processed later.
|
30
|
+
#
|
31
|
+
# config.time_zone = "Central Time (US & Canada)"
|
32
|
+
# config.eager_load_paths << Rails.root.join("extras")
|
33
|
+
|
34
|
+
# Only loads a smaller set of middleware suitable for API only apps.
|
35
|
+
# Middleware like session, flash, cookies can be added back manually.
|
36
|
+
# Skip views, helpers and assets when generating a new resource.
|
37
|
+
config.api_only = true
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
lvsdM1BYTQWVA7SRjgf5/h8SfdnTnG/YYhQTZ8CztshiP6FzOzb8vAnguQXg8J3it/KQHdEGgK47y2HEirkCr0QUByzbjxR9tqmA383W0AAnLPxn5Gr8Ma8XXkigmXOX4uTZD0nti2Qjq9W0v8X8yFj6xLtUoXpvZ+owPpp5kZgE7ZUeZ05t3X+EoOtCF1m7TNBBiIsyefLLqmwmJneR8R5TWUm6gNiXZXDysPzZBTlovqs93uVFL7WfViZNNWkGUPARrgreugworPRRUx1jqxJQxoOotiSaabUv/6mILkR//okBlxBw3WHS6KONtdgJ35P3BTRSIGniO2qeC5QaUNLfcE7zfLM+21zFVkynsCkBXhsP6IFRzSO1Bs9xIIuj7ror7wxqga8I4Oi0Oj67ErVuQwo3+zNzoek+--27xzMgBPWtwjh/KK--/Yao0rAoDQQqRS4/Y24E+g==
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "active_support/core_ext/integer/time"
|
2
|
+
|
3
|
+
Rails.application.configure do
|
4
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
5
|
+
|
6
|
+
# In the development environment your application's code is reloaded any time
|
7
|
+
# it changes. This slows down response time but is perfect for development
|
8
|
+
# since you don't have to restart the web server when you make code changes.
|
9
|
+
config.cache_classes = false
|
10
|
+
|
11
|
+
# Do not eager load code on boot.
|
12
|
+
config.eager_load = false
|
13
|
+
|
14
|
+
# Show full error reports.
|
15
|
+
config.consider_all_requests_local = true
|
16
|
+
|
17
|
+
# Enable server timing
|
18
|
+
config.server_timing = true
|
19
|
+
|
20
|
+
# Enable/disable caching. By default caching is disabled.
|
21
|
+
# Run rails dev:cache to toggle caching.
|
22
|
+
if Rails.root.join("tmp/caching-dev.txt").exist?
|
23
|
+
config.cache_store = :memory_store
|
24
|
+
config.public_file_server.headers = {
|
25
|
+
"Cache-Control" => "public, max-age=#{2.days.to_i}"
|
26
|
+
}
|
27
|
+
else
|
28
|
+
config.action_controller.perform_caching = false
|
29
|
+
|
30
|
+
config.cache_store = :null_store
|
31
|
+
end
|
32
|
+
|
33
|
+
# Print deprecation notices to the Rails logger.
|
34
|
+
config.active_support.deprecation = :log
|
35
|
+
|
36
|
+
# Raise exceptions for disallowed deprecations.
|
37
|
+
config.active_support.disallowed_deprecation = :raise
|
38
|
+
|
39
|
+
# Tell Active Support which deprecation messages to disallow.
|
40
|
+
config.active_support.disallowed_deprecation_warnings = []
|
41
|
+
|
42
|
+
|
43
|
+
# Raises error for missing translations.
|
44
|
+
# config.i18n.raise_on_missing_translations = true
|
45
|
+
|
46
|
+
# Annotate rendered view with file names.
|
47
|
+
# config.action_view.annotate_rendered_view_with_filenames = true
|
48
|
+
|
49
|
+
# Uncomment if you wish to allow Action Cable access from any origin.
|
50
|
+
# config.action_cable.disable_request_forgery_protection = true
|
51
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "active_support/core_ext/integer/time"
|
2
|
+
|
3
|
+
Rails.application.configure do
|
4
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
5
|
+
|
6
|
+
# Code is not reloaded between requests.
|
7
|
+
config.cache_classes = true
|
8
|
+
|
9
|
+
# Eager load code on boot. This eager loads most of Rails and
|
10
|
+
# your application in memory, allowing both threaded web servers
|
11
|
+
# and those relying on copy on write to perform better.
|
12
|
+
# Rake tasks automatically ignore this option for performance.
|
13
|
+
config.eager_load = true
|
14
|
+
|
15
|
+
# Full error reports are disabled and caching is turned on.
|
16
|
+
config.consider_all_requests_local = false
|
17
|
+
|
18
|
+
# Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
|
19
|
+
# or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
|
20
|
+
# config.require_master_key = true
|
21
|
+
|
22
|
+
# Disable serving static files from the `/public` folder by default since
|
23
|
+
# Apache or NGINX already handles this.
|
24
|
+
config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?
|
25
|
+
|
26
|
+
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
|
27
|
+
# config.asset_host = "http://assets.example.com"
|
28
|
+
|
29
|
+
# Specifies the header that your server uses for sending files.
|
30
|
+
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache
|
31
|
+
# config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX
|
32
|
+
|
33
|
+
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
|
34
|
+
# config.force_ssl = true
|
35
|
+
|
36
|
+
# Include generic and useful information about system operation, but avoid logging too much
|
37
|
+
# information to avoid inadvertent exposure of personally identifiable information (PII).
|
38
|
+
config.log_level = :info
|
39
|
+
|
40
|
+
# Prepend all log lines with the following tags.
|
41
|
+
config.log_tags = [ :request_id ]
|
42
|
+
|
43
|
+
# Use a different cache store in production.
|
44
|
+
# config.cache_store = :mem_cache_store
|
45
|
+
|
46
|
+
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
|
47
|
+
# the I18n.default_locale when a translation cannot be found).
|
48
|
+
config.i18n.fallbacks = true
|
49
|
+
|
50
|
+
# Don't log any deprecations.
|
51
|
+
config.active_support.report_deprecations = false
|
52
|
+
|
53
|
+
# Use default logging formatter so that PID and timestamp are not suppressed.
|
54
|
+
config.log_formatter = ::Logger::Formatter.new
|
55
|
+
|
56
|
+
# Use a different logger for distributed setups.
|
57
|
+
# require "syslog/logger"
|
58
|
+
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name")
|
59
|
+
|
60
|
+
if ENV["RAILS_LOG_TO_STDOUT"].present?
|
61
|
+
logger = ActiveSupport::Logger.new(STDOUT)
|
62
|
+
logger.formatter = config.log_formatter
|
63
|
+
config.logger = ActiveSupport::TaggedLogging.new(logger)
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "active_support/core_ext/integer/time"
|
2
|
+
|
3
|
+
# The test environment is used exclusively to run your application's
|
4
|
+
# test suite. You never need to work with it otherwise. Remember that
|
5
|
+
# your test database is "scratch space" for the test suite and is wiped
|
6
|
+
# and recreated between test runs. Don't rely on the data there!
|
7
|
+
|
8
|
+
Rails.application.configure do
|
9
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
10
|
+
|
11
|
+
# Turn false under Spring and add config.action_view.cache_template_loading = true.
|
12
|
+
config.cache_classes = true
|
13
|
+
|
14
|
+
# Eager loading loads your whole application. When running a single test locally,
|
15
|
+
# this probably isn't necessary. It's a good idea to do in a continuous integration
|
16
|
+
# system, or in some way before deploying your code.
|
17
|
+
config.eager_load = ENV["CI"].present?
|
18
|
+
|
19
|
+
# Configure public file server for tests with Cache-Control for performance.
|
20
|
+
config.public_file_server.enabled = true
|
21
|
+
config.public_file_server.headers = {
|
22
|
+
"Cache-Control" => "public, max-age=#{1.hour.to_i}"
|
23
|
+
}
|
24
|
+
|
25
|
+
# Show full error reports and disable caching.
|
26
|
+
config.consider_all_requests_local = true
|
27
|
+
config.action_controller.perform_caching = false
|
28
|
+
config.cache_store = :null_store
|
29
|
+
|
30
|
+
# Raise exceptions instead of rendering exception templates.
|
31
|
+
config.action_dispatch.show_exceptions = false
|
32
|
+
|
33
|
+
# Disable request forgery protection in test environment.
|
34
|
+
config.action_controller.allow_forgery_protection = false
|
35
|
+
|
36
|
+
# Print deprecation notices to the stderr.
|
37
|
+
config.active_support.deprecation = :stderr
|
38
|
+
|
39
|
+
# Raise exceptions for disallowed deprecations.
|
40
|
+
config.active_support.disallowed_deprecation = :raise
|
41
|
+
|
42
|
+
# Tell Active Support which deprecation messages to disallow.
|
43
|
+
config.active_support.disallowed_deprecation_warnings = []
|
44
|
+
|
45
|
+
# Raises error for missing translations.
|
46
|
+
# config.i18n.raise_on_missing_translations = true
|
47
|
+
|
48
|
+
# Annotate rendered view with file names.
|
49
|
+
# config.action_view.annotate_rendered_view_with_filenames = true
|
50
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# Avoid CORS issues when API is called from the frontend app.
|
4
|
+
# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests.
|
5
|
+
|
6
|
+
# Read more: https://github.com/cyu/rack-cors
|
7
|
+
|
8
|
+
# Rails.application.config.middleware.insert_before 0, Rack::Cors do
|
9
|
+
# allow do
|
10
|
+
# origins "example.com"
|
11
|
+
#
|
12
|
+
# resource "*",
|
13
|
+
# headers: :any,
|
14
|
+
# methods: [:get, :post, :put, :patch, :delete, :options, :head]
|
15
|
+
# end
|
16
|
+
# end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# Configure parameters to be filtered from the log file. Use this to limit dissemination of
|
4
|
+
# sensitive information. See the ActiveSupport::ParameterFilter documentation for supported
|
5
|
+
# notations and behaviors.
|
6
|
+
Rails.application.config.filter_parameters += [
|
7
|
+
:passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
|
8
|
+
]
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# Add new inflection rules using the following format. Inflections
|
4
|
+
# are locale specific, and you may define rules for as many different
|
5
|
+
# locales as you wish. All of these examples are active by default:
|
6
|
+
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
7
|
+
# inflect.plural /^(ox)$/i, "\\1en"
|
8
|
+
# inflect.singular /^(ox)en/i, "\\1"
|
9
|
+
# inflect.irregular "person", "people"
|
10
|
+
# inflect.uncountable %w( fish sheep )
|
11
|
+
# end
|
12
|
+
|
13
|
+
# These inflection rules are supported but not enabled by default:
|
14
|
+
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
15
|
+
# inflect.acronym "RESTful"
|
16
|
+
# end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Files in the config/locales directory are used for internationalization
|
2
|
+
# and are automatically loaded by Rails. If you want to use locales other
|
3
|
+
# than English, add the necessary files in this directory.
|
4
|
+
#
|
5
|
+
# To use the locales, use `I18n.t`:
|
6
|
+
#
|
7
|
+
# I18n.t "hello"
|
8
|
+
#
|
9
|
+
# In views, this is aliased to just `t`:
|
10
|
+
#
|
11
|
+
# <%= t("hello") %>
|
12
|
+
#
|
13
|
+
# To use a different locale, set it with `I18n.locale`:
|
14
|
+
#
|
15
|
+
# I18n.locale = :es
|
16
|
+
#
|
17
|
+
# This would use the information in config/locales/es.yml.
|
18
|
+
#
|
19
|
+
# The following keys must be escaped otherwise they will not be retrieved by
|
20
|
+
# the default I18n backend:
|
21
|
+
#
|
22
|
+
# true, false, on, off, yes, no
|
23
|
+
#
|
24
|
+
# Instead, surround them with single quotes.
|
25
|
+
#
|
26
|
+
# en:
|
27
|
+
# "true": "foo"
|
28
|
+
#
|
29
|
+
# To learn more, please read the Rails Internationalization guide
|
30
|
+
# available at https://guides.rubyonrails.org/i18n.html.
|
31
|
+
|
32
|
+
en:
|
33
|
+
hello: "Hello world"
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# Puma can serve each request in a thread from an internal thread pool.
|
2
|
+
# The `threads` method setting takes two numbers: a minimum and maximum.
|
3
|
+
# Any libraries that use thread pools should be configured to match
|
4
|
+
# the maximum value specified for Puma. Default is set to 5 threads for minimum
|
5
|
+
# and maximum; this matches the default thread size of Active Record.
|
6
|
+
#
|
7
|
+
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
|
8
|
+
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
|
9
|
+
threads min_threads_count, max_threads_count
|
10
|
+
|
11
|
+
# Specifies the `worker_timeout` threshold that Puma will use to wait before
|
12
|
+
# terminating a worker in development environments.
|
13
|
+
#
|
14
|
+
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"
|
15
|
+
|
16
|
+
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
|
17
|
+
#
|
18
|
+
port ENV.fetch("PORT") { 3000 }
|
19
|
+
|
20
|
+
# Specifies the `environment` that Puma will run in.
|
21
|
+
#
|
22
|
+
environment ENV.fetch("RAILS_ENV") { "development" }
|
23
|
+
|
24
|
+
# Specifies the `pidfile` that Puma will use.
|
25
|
+
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
|
26
|
+
|
27
|
+
# Specifies the number of `workers` to boot in clustered mode.
|
28
|
+
# Workers are forked web server processes. If using threads and workers together
|
29
|
+
# the concurrency of the application would be max `threads` * `workers`.
|
30
|
+
# Workers do not work on JRuby or Windows (both of which do not support
|
31
|
+
# processes).
|
32
|
+
#
|
33
|
+
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }
|
34
|
+
|
35
|
+
# Use the `preload_app!` method when specifying a `workers` number.
|
36
|
+
# This directive tells Puma to first boot the application and load code
|
37
|
+
# before forking the application. This takes advantage of Copy On Write
|
38
|
+
# process behavior so workers use less memory.
|
39
|
+
#
|
40
|
+
# preload_app!
|
41
|
+
|
42
|
+
# Allow puma to be restarted by `bin/rails restart` command.
|
43
|
+
plugin :tmp_restart
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Rails.application.routes.draw do
|
2
|
+
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
3
|
+
|
4
|
+
# Defines the root path route ("/")
|
5
|
+
# root "articles#index"
|
6
|
+
|
7
|
+
post '/parse_params', to: 'data#parse_params'
|
8
|
+
post '/render_hash', to: 'data#render_hash'
|
9
|
+
post '/render_object', to: 'data#render_object'
|
10
|
+
post '/render_with_options', to: 'data#render_with_options'
|
11
|
+
|
12
|
+
get '/swagger_doc', to: 'swagger#get_spec'
|
13
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
RSpec.describe "DataController", type: :request do
|
4
|
+
describe "参数" do
|
5
|
+
it "过滤参数到 params,原始参数存储到 raw_params" do
|
6
|
+
post '/parse_params', params: { q: 'q', user: {} }, as: :json
|
7
|
+
expect(response.status).to eq(200)
|
8
|
+
|
9
|
+
response_body = JSON.parse(response.body)
|
10
|
+
# params 被修改,同时保留 controller 和 action 参数
|
11
|
+
expect(response_body['params_on_schema']).to eq(
|
12
|
+
'q' => 'q',
|
13
|
+
'user' => { 'name' => 'Jim', 'age' => 18 }
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "参数异常自动捕获" do
|
18
|
+
post '/parse_params', params: {}, as: :json
|
19
|
+
expect(response.status).to eq(400)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '渲染' do
|
24
|
+
it "默认值设定起作用" do
|
25
|
+
post '/render_hash', params: { user: {} }, as: :json
|
26
|
+
expect(response.status).to eq(200)
|
27
|
+
|
28
|
+
response_body = JSON.parse(response.body)
|
29
|
+
expect(response_body['user']).to eq('name' => 'Jim', 'age' => 18)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "使用不同的块定义" do
|
33
|
+
post '/render_hash', params: { user: {}, status: 201 }, as: :json
|
34
|
+
expect(response.status).to eq(201)
|
35
|
+
|
36
|
+
response_body = JSON.parse(response.body)
|
37
|
+
expect(response_body['user']).to eq('name' => 'Jack', 'age' => 20)
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'render object' do
|
41
|
+
it "render 一个对象" do
|
42
|
+
post '/render_object'
|
43
|
+
expect(response.status).to eq(200)
|
44
|
+
|
45
|
+
response_body = JSON.parse(response.body)
|
46
|
+
expect(response_body['user']).to eq('name' => 'Jim', 'age' => 18)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'with execution:, stage:, scope:' do
|
51
|
+
it "使用选项" do
|
52
|
+
post '/render_with_options', params: { user: { a: 'a', b: 'b', c: 'c', d: 'd' }, status: 202 }, as: :json
|
53
|
+
expect(response.status).to eq(200)
|
54
|
+
|
55
|
+
response_body = JSON.parse(response.body)
|
56
|
+
expect(response_body['user']).to eq('a' => 'aa', 'b' => 'b')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
2
|
+
require 'spec_helper'
|
3
|
+
ENV['RAILS_ENV'] ||= 'test'
|
4
|
+
require_relative '../config/environment'
|
5
|
+
# Prevent database truncation if the environment is production
|
6
|
+
abort("The Rails environment is running in production mode!") if Rails.env.production?
|
7
|
+
require 'rspec/rails'
|
8
|
+
# Add additional requires below this line. Rails is not loaded until this point!
|
9
|
+
|
10
|
+
# Requires supporting ruby files with custom matchers and macros, etc, in
|
11
|
+
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
|
12
|
+
# run as spec files by default. This means that files in spec/support that end
|
13
|
+
# in _spec.rb will both be required and run as specs, causing the specs to be
|
14
|
+
# run twice. It is recommended that you do not name files matching this glob to
|
15
|
+
# end with _spec.rb. You can configure this pattern with the --pattern
|
16
|
+
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
|
17
|
+
#
|
18
|
+
# The following line is provided for convenience purposes. It has the downside
|
19
|
+
# of increasing the boot-up time by auto-requiring all files in the support
|
20
|
+
# directory. Alternatively, in the individual `*_spec.rb` files, manually
|
21
|
+
# require only the support files necessary.
|
22
|
+
#
|
23
|
+
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
|
24
|
+
|
25
|
+
RSpec.configure do |config|
|
26
|
+
# Remove this line to enable support for ActiveRecord
|
27
|
+
config.use_active_record = false
|
28
|
+
|
29
|
+
# If you enable ActiveRecord support you should uncomment these lines,
|
30
|
+
# note if you'd prefer not to run each example within a transaction, you
|
31
|
+
# should set use_transactional_fixtures to false.
|
32
|
+
#
|
33
|
+
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
|
34
|
+
# config.use_transactional_fixtures = true
|
35
|
+
|
36
|
+
# RSpec Rails can automatically mix in different behaviours to your tests
|
37
|
+
# based on their file location, for example enabling you to call `get` and
|
38
|
+
# `post` in specs under `spec/controllers`.
|
39
|
+
#
|
40
|
+
# You can disable this behaviour by removing the line below, and instead
|
41
|
+
# explicitly tag your specs with their type, e.g.:
|
42
|
+
#
|
43
|
+
# RSpec.describe UsersController, type: :controller do
|
44
|
+
# # ...
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# The different available types are documented in the features, such as in
|
48
|
+
# https://relishapp.com/rspec/rspec-rails/docs
|
49
|
+
config.infer_spec_type_from_file_location!
|
50
|
+
|
51
|
+
# Filter lines from Rails gems in backtraces.
|
52
|
+
config.filter_rails_from_backtrace!
|
53
|
+
# arbitrary gems may also be filtered via:
|
54
|
+
# config.filter_gems_from_backtrace("gem name")
|
55
|
+
end
|