json_schema_builder 0.0.1 → 0.0.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.
- data/.travis.yml +4 -0
- data/Gemfile +2 -4
- data/{README → README.rdoc} +2 -0
- data/bin/{schema.rb → schema} +0 -0
- data/lib/json_schema_builder/version.rb +1 -1
- data/lib/json_schema_builder/writer.rb +25 -5
- data/spec/json_schema_builder/writer_spec.rb +40 -3
- data/spec/spec_helper.rb +8 -1
- metadata +7 -6
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/{README → README.rdoc}
RENAMED
@@ -1,5 +1,7 @@
|
|
1
1
|
= JSON Schema Builder
|
2
2
|
|
3
|
+
{<img src="https://secure.travis-ci.org/salesking/json_schema_builder.png?branch=master" alt="Build Status" />}[http://travis-ci.org/salesking/json_schema_builder]
|
4
|
+
|
3
5
|
Build a JSON schema for your ActiveRecord models. STOP your API pains!
|
4
6
|
|
5
7
|
The created schema.json files are meant as stubs and need to be enriched with
|
data/bin/{schema.rb → schema}
RENAMED
File without changes
|
@@ -11,7 +11,7 @@ module JsonSchemaBuilder
|
|
11
11
|
res = []
|
12
12
|
create_file_path
|
13
13
|
models_as_hash.each do |model|
|
14
|
-
file = File.join(
|
14
|
+
file = File.join( out_path, "#{model['title'].downcase}.json")
|
15
15
|
File.open( file, 'w+' ) {|f| f.write(JSON.pretty_generate(model)) }
|
16
16
|
res << "#{file} created"
|
17
17
|
end
|
@@ -53,16 +53,36 @@ module JsonSchemaBuilder
|
|
53
53
|
hsh
|
54
54
|
end
|
55
55
|
|
56
|
+
# @return [Array<ActiveRecord::Base>] AR::Base descendant models
|
56
57
|
def models
|
57
|
-
#
|
58
|
-
|
59
|
-
Dir.glob( + '/app/models/**/*.rb').each { |file| require file }
|
58
|
+
# require so they are in module scope
|
59
|
+
Dir.glob( model_path ).each { |file| require file }
|
60
60
|
model_names = Module.constants.select { |c| (eval "#{c}").is_a?(Class) && (eval "#{c}") < ::ActiveRecord::Base }
|
61
61
|
model_names.map{|i| "#{i}".constantize}
|
62
62
|
end
|
63
63
|
|
64
64
|
def create_file_path
|
65
|
-
FileUtils.
|
65
|
+
FileUtils.mkdir_p(out_path) unless File.exists?(out_path)
|
66
|
+
end
|
67
|
+
|
68
|
+
def model_path
|
69
|
+
@model_path ||= File.join( Dir.pwd, 'app/models', '**/*.rb')
|
70
|
+
end
|
71
|
+
|
72
|
+
# Set the model path
|
73
|
+
# @param [String] path or file or pattern like models/**/*.rb
|
74
|
+
def model_path=(path)
|
75
|
+
@model_path = path
|
76
|
+
end
|
77
|
+
|
78
|
+
# Path to write json files
|
79
|
+
def out_path
|
80
|
+
@out_path ||= File.join( Dir.pwd, 'json-schema')
|
81
|
+
end
|
82
|
+
|
83
|
+
# @param [String] path to json schema files
|
84
|
+
def out_path=(path)
|
85
|
+
@out_path = path
|
66
86
|
end
|
67
87
|
|
68
88
|
private
|
@@ -2,9 +2,46 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe JsonSchemaBuilder::Writer do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
context 'in general' do
|
6
|
+
before :each do
|
7
|
+
@writer = JsonSchemaBuilder::Writer.new
|
8
|
+
end
|
9
|
+
it 'should get model_path' do
|
10
|
+
@writer.model_path.should == "#{Dir.pwd}/app/models/**/*.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should set model_path' do
|
14
|
+
path = 'some/model-folder/*.*.rb'
|
15
|
+
@writer.model_path = path
|
16
|
+
@writer.model_path.should == path
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should get models' do
|
20
|
+
@writer.models.should include(User)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'file writing' do
|
25
|
+
before :each do
|
26
|
+
@writer = JsonSchemaBuilder::Writer.new
|
27
|
+
@writer.out_path = test_out_path
|
28
|
+
@writer.model_path = File.join( File.expand_path( __FILE__), '../fixtures/*.rb')
|
29
|
+
@writer.write
|
30
|
+
@file_path = File.join(test_out_path, 'user.json')
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should write file' do
|
34
|
+
File.exist?( @file_path ).should be
|
35
|
+
File.delete @file_path
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should write valid json' do
|
39
|
+
hsh = JSON.parse( File.read @file_path)
|
40
|
+
hsh['type'].should == 'object'
|
41
|
+
hsh['title'].should == 'User'
|
42
|
+
field_names = hsh['properties'].keys
|
43
|
+
field_names.should include 'id', 'number', 'birthday', 'is_admin', 'notes'
|
44
|
+
end
|
8
45
|
end
|
9
46
|
|
10
47
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -18,10 +18,17 @@ RSpec.configure do |config|
|
|
18
18
|
config.treat_symbols_as_metadata_keys_with_true_values = true
|
19
19
|
config.run_all_when_everything_filtered = true
|
20
20
|
config.filter_run :focus
|
21
|
+
config.after(:all) do
|
22
|
+
FileUtils.rm_rf(test_out_path)
|
23
|
+
end
|
21
24
|
end
|
22
25
|
|
23
26
|
ActiveRecord::Base.establish_connection({
|
24
27
|
'adapter' => 'sqlite3',
|
25
28
|
'database' => ':memory:'
|
26
29
|
})
|
27
|
-
load(File.join(File.dirname(__FILE__), 'fixtures/ar_schema.rb'))
|
30
|
+
load(File.join(File.dirname(__FILE__), 'fixtures/ar_schema.rb'))
|
31
|
+
|
32
|
+
def test_out_path
|
33
|
+
File.join( File.dirname( __FILE__), 'tmp')
|
34
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -143,16 +143,17 @@ description: Build JSON Schema fields for ActiveRecord models
|
|
143
143
|
email:
|
144
144
|
- gl@salesking.eu
|
145
145
|
executables:
|
146
|
-
- schema
|
146
|
+
- schema
|
147
147
|
extensions: []
|
148
148
|
extra_rdoc_files: []
|
149
149
|
files:
|
150
150
|
- .gitignore
|
151
151
|
- .rspec
|
152
|
+
- .travis.yml
|
152
153
|
- Gemfile
|
153
|
-
- README
|
154
|
+
- README.rdoc
|
154
155
|
- Rakefile
|
155
|
-
- bin/schema
|
156
|
+
- bin/schema
|
156
157
|
- json_schema_builder.gemspec
|
157
158
|
- lib/json_schema_builder.rb
|
158
159
|
- lib/json_schema_builder/version.rb
|
@@ -175,7 +176,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
175
176
|
version: '0'
|
176
177
|
segments:
|
177
178
|
- 0
|
178
|
-
hash:
|
179
|
+
hash: -4247510019183941488
|
179
180
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
181
|
none: false
|
181
182
|
requirements:
|
@@ -184,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
185
|
version: '0'
|
185
186
|
segments:
|
186
187
|
- 0
|
187
|
-
hash:
|
188
|
+
hash: -4247510019183941488
|
188
189
|
requirements: []
|
189
190
|
rubyforge_project:
|
190
191
|
rubygems_version: 1.8.24
|