json_schema_rails 0.0.3 → 0.1.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.
- checksums.yaml +4 -4
- data/.travis.yml +5 -4
- data/README.md +22 -0
- data/app/controllers/json_schema_rails/application_controller.rb +4 -0
- data/app/controllers/json_schema_rails/schemas_controller.rb +13 -0
- data/config/routes.rb +3 -0
- data/lib/json_schema_rails.rb +3 -1
- data/lib/json_schema_rails/{railtie.rb → engine.rb} +3 -1
- data/lib/json_schema_rails/json_schema/parser.rb +15 -0
- data/lib/json_schema_rails/json_schema/schema.rb +13 -0
- data/lib/json_schema_rails/version.rb +1 -1
- data/spec/dummy_apps/rails3/config/routes.rb +1 -0
- data/spec/dummy_apps/rails4/config/routes.rb +1 -0
- data/spec/{railtie/controllers → integration}/posts_controller_spec.rb +0 -0
- data/spec/integration/schemas_controller_spec.rb +36 -0
- data/spec/lib/json_schema_rails/json_schema/parser_spec.rb +13 -0
- metadata +14 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6815f00fc76b0d36cdff144949a31a27bfc1713b
|
4
|
+
data.tar.gz: 3f8ef54b094edc0b14bacb96857ea4ef367da63b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f04c0e5d4c0d1f41af6a49b64f922faf592dbafbe6ee11ade859a23e558ba888e5ae6e7c22ca261ad361b5870a2b0b54a8d02f99e981d62aa9d5580d7ddb41f9
|
7
|
+
data.tar.gz: fb6c4568245c45e24ef30d981e5ec39a5af725995cd38841303d1ef206f888f198de54b740e5afb5d259b0b2a8994f5e25b1d66187dfa846cae0df22405d3e4c
|
data/.travis.yml
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
language: ruby
|
2
|
+
cache: bundler
|
2
3
|
rvm:
|
4
|
+
- 1.9.3-p547
|
3
5
|
- 2.1.2
|
6
|
+
env:
|
7
|
+
- RAILS_VERSION=3.2.18
|
8
|
+
- RAILS_VERSION=4.1.1
|
4
9
|
|
5
10
|
install:
|
6
11
|
- "travis_retry bundle install"
|
7
12
|
|
8
13
|
script: "bundle exec rspec"
|
9
|
-
|
10
|
-
env:
|
11
|
-
- RAILS_VERSION=3.2.18
|
12
|
-
- RAILS_VERSION=4.1.1
|
data/README.md
CHANGED
@@ -97,6 +97,28 @@ class ApplicationController < ActionController::Base
|
|
97
97
|
end
|
98
98
|
```
|
99
99
|
|
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
|
+
|
100
122
|
### Validate schema files
|
101
123
|
|
102
124
|
json_schema_rails provides a rake task `schema:check` to validate your schema files.
|
@@ -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
|
data/config/routes.rb
ADDED
data/lib/json_schema_rails.rb
CHANGED
@@ -2,7 +2,9 @@ require "json_schema_rails/version"
|
|
2
2
|
require "json_schema_rails/errors"
|
3
3
|
require "json_schema_rails/loaders"
|
4
4
|
require "json_schema_rails/schema_validator"
|
5
|
-
require "json_schema_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"
|
6
8
|
|
7
9
|
module JsonSchemaRails
|
8
10
|
def self.schema_loader
|
@@ -1,7 +1,9 @@
|
|
1
1
|
require "json_schema_rails/helpers"
|
2
2
|
|
3
3
|
module JsonSchemaRails
|
4
|
-
class
|
4
|
+
class Engine < ::Rails::Engine
|
5
|
+
isolate_namespace JsonSchemaRails
|
6
|
+
|
5
7
|
initializer "json_schema_rails.set_schema_loader" do |app|
|
6
8
|
if schema_file = Dir[app.root.join('app', 'schema.*')].first
|
7
9
|
loader = JsonSchemaRails::Loaders::HyperSchema.new(schema_file)
|
@@ -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
|
File without changes
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe JsonSchemaRails::SchemasController, type: :request do
|
4
|
+
describe "GET #get" do
|
5
|
+
context "with existing schema" do
|
6
|
+
before { get "/schemas/posts/create.#{format}" }
|
7
|
+
let(:expected_schema) { YAML.load_file(Rails.root.join('app', 'schemas', 'posts', 'create.yml').to_s) }
|
8
|
+
|
9
|
+
context "as json" do
|
10
|
+
let(:format) { "json" }
|
11
|
+
it "returns the schema as json" do
|
12
|
+
expect(response).to be_success
|
13
|
+
expect(response.content_type).to eq :json
|
14
|
+
expect(response.body).to eq expected_schema.to_json
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "as yaml" do
|
19
|
+
let(:format) { "yaml" }
|
20
|
+
it "returns the schema as yaml" do
|
21
|
+
expect(response).to be_success
|
22
|
+
expect(response.content_type).to eq :yaml
|
23
|
+
expect(response.body).to eq expected_schema.to_yaml
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "with nonexisting schema" do
|
29
|
+
subject { -> { get "/schemas/posts/nonexist.json" } }
|
30
|
+
|
31
|
+
it "raises ActionController::RoutingError" do
|
32
|
+
expect(subject).to raise_error ActionController::RoutingError
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
RSpec.describe JsonSchema::Parser do
|
5
|
+
describe "#parse!" do
|
6
|
+
let(:schema_content) { YAML.load_file(File.join(TEST_SCHEMAS_DIR, 'posts/create.yml')) }
|
7
|
+
subject(:schema) { JsonSchema.parse!(schema_content) }
|
8
|
+
|
9
|
+
it "sets original schema data to schema#data" do
|
10
|
+
expect(schema.data).to eq schema_content
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kota Saito
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -176,6 +176,9 @@ files:
|
|
176
176
|
- MIT-LICENSE
|
177
177
|
- README.md
|
178
178
|
- Rakefile
|
179
|
+
- app/controllers/json_schema_rails/application_controller.rb
|
180
|
+
- app/controllers/json_schema_rails/schemas_controller.rb
|
181
|
+
- config/routes.rb
|
179
182
|
- json_schema_rails.gemspec
|
180
183
|
- lib/generators/schema/action/USAGE
|
181
184
|
- lib/generators/schema/action/action_generator.rb
|
@@ -184,13 +187,15 @@ files:
|
|
184
187
|
- lib/generators/schema/controller/controller_generator.rb
|
185
188
|
- lib/generators/schema/controller/templates/schema.yml.erb
|
186
189
|
- lib/json_schema_rails.rb
|
190
|
+
- lib/json_schema_rails/engine.rb
|
187
191
|
- lib/json_schema_rails/errors.rb
|
188
192
|
- lib/json_schema_rails/helpers.rb
|
193
|
+
- lib/json_schema_rails/json_schema/parser.rb
|
194
|
+
- lib/json_schema_rails/json_schema/schema.rb
|
189
195
|
- lib/json_schema_rails/loaders.rb
|
190
196
|
- lib/json_schema_rails/loaders/base.rb
|
191
197
|
- lib/json_schema_rails/loaders/directory.rb
|
192
198
|
- lib/json_schema_rails/loaders/hyper_schema.rb
|
193
|
-
- lib/json_schema_rails/railtie.rb
|
194
199
|
- lib/json_schema_rails/schema_validator.rb
|
195
200
|
- lib/json_schema_rails/version.rb
|
196
201
|
- lib/tasks/schema_tasks.rake
|
@@ -283,11 +288,13 @@ files:
|
|
283
288
|
- spec/fixtures/schemas/posts/search.json
|
284
289
|
- spec/fixtures/schemas/posts/update.yaml
|
285
290
|
- spec/fixtures/schemas/syntax_error.json
|
291
|
+
- spec/integration/posts_controller_spec.rb
|
292
|
+
- spec/integration/schemas_controller_spec.rb
|
293
|
+
- spec/lib/json_schema_rails/json_schema/parser_spec.rb
|
286
294
|
- spec/lib/json_schema_rails/loaders/directory_spec.rb
|
287
295
|
- spec/lib/json_schema_rails/loaders/hyper_schema_spec.rb
|
288
296
|
- spec/lib/json_schema_rails/schema_validator_spec.rb
|
289
297
|
- spec/lib/json_schema_rails_spec.rb
|
290
|
-
- spec/railtie/controllers/posts_controller_spec.rb
|
291
298
|
- spec/spec_helper.rb
|
292
299
|
homepage: https://github.com/kotas/json_schema_rails
|
293
300
|
licenses:
|
@@ -399,9 +406,11 @@ test_files:
|
|
399
406
|
- spec/fixtures/schemas/posts/search.json
|
400
407
|
- spec/fixtures/schemas/posts/update.yaml
|
401
408
|
- spec/fixtures/schemas/syntax_error.json
|
409
|
+
- spec/integration/posts_controller_spec.rb
|
410
|
+
- spec/integration/schemas_controller_spec.rb
|
411
|
+
- spec/lib/json_schema_rails/json_schema/parser_spec.rb
|
402
412
|
- spec/lib/json_schema_rails/loaders/directory_spec.rb
|
403
413
|
- spec/lib/json_schema_rails/loaders/hyper_schema_spec.rb
|
404
414
|
- spec/lib/json_schema_rails/schema_validator_spec.rb
|
405
415
|
- spec/lib/json_schema_rails_spec.rb
|
406
|
-
- spec/railtie/controllers/posts_controller_spec.rb
|
407
416
|
- spec/spec_helper.rb
|