Skip to content

Commit 2cb0101

Browse files
committed
Merge pull request #1 from kotas/mountable
Mountable engine for serving schemas
2 parents d7dab39 + 195b8ef commit 2cb0101

File tree

13 files changed

+127
-2
lines changed

13 files changed

+127
-2
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,28 @@ class ApplicationController < ActionController::Base
9797
end
9898
```
9999

100+
### Serve schemas in your application
101+
102+
If you want to use your schemas in your client JavaScript, the mountable engine `JsonSchemaRails::Engine` provides a route for serving schemas in you application.
103+
104+
To use it, write the following in your `config/routes.rb`:
105+
106+
```ruby
107+
Your::Application.routes.draw do
108+
mount JsonSchemaRails::Engine => '/schemas'
109+
end
110+
```
111+
112+
And you can get your schemas through the path `/schemas` like following:
113+
114+
```
115+
# Get `app/schemas/posts/create.*` as application/json
116+
GET /schemas/posts/create.json
117+
118+
# Get `app/schemas/posts/update.*` as text/yaml
119+
GET /schemas/posts/update.yaml
120+
```
121+
100122
### Validate schema files
101123

102124
json_schema_rails provides a rake task `schema:check` to validate your schema files.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module JsonSchemaRails
2+
class ApplicationController < ActionController::Base
3+
end
4+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module JsonSchemaRails
2+
class SchemasController < ApplicationController
3+
def get
4+
schema = JsonSchemaRails.lookup_schema(params[:schema])
5+
raise ActionController::RoutingError.new('No Such Schema') unless schema
6+
7+
respond_to do |format|
8+
format.yaml { render text: schema.to_hash.to_yaml, content_type: 'text/yaml' }
9+
format.json { render json: schema }
10+
end
11+
end
12+
end
13+
end

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
JsonSchemaRails::Engine.routes.draw do
2+
get '/:schema(.:format)', to: 'schemas#get', constraints: { schema: /[\/a-zA-Z0-9_-]+/ }
3+
end

lib/json_schema_rails.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
require "json_schema_rails/errors"
33
require "json_schema_rails/loaders"
44
require "json_schema_rails/schema_validator"
5-
require "json_schema_rails/railtie" if defined?(Rails)
5+
require "json_schema_rails/engine" if defined?(Rails)
6+
require "json_schema_rails/json_schema/schema"
7+
require "json_schema_rails/json_schema/parser"
68

79
module JsonSchemaRails
810
def self.schema_loader

lib/json_schema_rails/railtie.rb renamed to lib/json_schema_rails/engine.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
require "json_schema_rails/helpers"
22

33
module JsonSchemaRails
4-
class Railtie < ::Rails::Railtie
4+
class Engine < ::Rails::Engine
5+
isolate_namespace JsonSchemaRails
6+
57
initializer "json_schema_rails.set_schema_loader" do |app|
68
if schema_file = Dir[app.root.join('app', 'schema.*')].first
79
loader = JsonSchemaRails::Loaders::HyperSchema.new(schema_file)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Patch for JsonSchema::Parser to memorize schema data
2+
3+
require "json_schema/parser"
4+
5+
module JsonSchema
6+
class Parser
7+
alias_method :_super_parse_data, :parse_data
8+
9+
def parse_data(data, parent, fragment)
10+
schema = _super_parse_data(data, parent, fragment)
11+
schema.data = data
12+
schema
13+
end
14+
end
15+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Patch for JsonSchema::Schema to memorize schema data
2+
3+
require "json_schema/schema"
4+
5+
module JsonSchema
6+
class Schema
7+
attr_accessor :data
8+
9+
def to_hash
10+
data
11+
end
12+
end
13+
end

spec/dummy_apps/rails3/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Dummy::Application.routes.draw do
2+
mount JsonSchemaRails::Engine => '/schemas'
23
resources :posts, only: [:create, :update]
34

45
# The priority is based upon order of creation:

spec/dummy_apps/rails4/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Rails.application.routes.draw do
2+
mount JsonSchemaRails::Engine => '/schemas'
23
resources :posts, only: [:create, :update]
34

45
# The priority is based upon order of creation: first created -> highest priority.

0 commit comments

Comments
 (0)