Module: JsonSchemaSpec

Defined in:
lib/json_schema_spec.rb,
lib/json_schema_spec/util.rb,
lib/json_schema_spec/tasks.rb

Defined Under Namespace

Modules: Util Classes: Tasks

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.download(path, url) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/json_schema_spec.rb', line 14

def download(path, url)
  FileUtils.mkdir_p(File.dirname(path))
  
  File.open(path, 'w') do |f|
    json = open(url).read
    yaml = JSON.parse(json).to_yaml
    f.write(yaml)
  end
end

Instance Method Details

#json_schema(resource, method) ⇒ Object



25
26
27
28
29
# File 'lib/json_schema_spec.rb', line 25

def json_schema(resource, method)
  schema = pick_json_schema
  schema = Util.symbolize_keys(schema)
  schema[:"#{resource}.json"][method]
end

#json_schema_params(resource, method, merge = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/json_schema_spec.rb', line 31

def json_schema_params(resource, method, merge={})
  required = merge[:required]
  schema   = json_schema(resource, method)
  params   = json_schema_to_params(schema, :required => required)
  params   = Util.deep_merge(params, merge)

  unless merge.empty?
    validate_json_schema(resource, method, params)
  end

  params = Util.deep_dup(params)
  [ params[:request], params[:response] ]
end

#json_schema_to_params(schema, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/json_schema_spec.rb', line 45

def json_schema_to_params(schema, options={})
  return schema  unless schema.is_a?(Hash)
  
  schema.inject({}) do |memo, (key, value)|
    memo[key] = json_schema_value(key, value, options)
    memo.delete(key)  unless memo[key]
    memo
  end
end

#json_schema_value(key, value, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/json_schema_spec.rb', line 55

def json_schema_value(key, value, options={})
  prefix   = options[:prefix] || []
  prefix   = prefix.dup  if prefix

  required = options[:required] || []
  required = [ required ].flatten  if required

  if return_nil?(key, value, required)
    nil
  elsif value[:enum]
    value[:enum].shuffle.first
  elsif value[:type] == 'array'
    [ json_schema_value(key, value[:items], options) ]
  elsif value[:type] == 'boolean'
    true
  elsif value[:type] == 'integer'
    123
  elsif value[:type] == 'object'
    prefix << key
    json_schema_to_params(
      value[:properties], :prefix => prefix, :required => required
    )
  elsif value[:type] == 'string'
    json_schema_value_prefix(prefix) + key.to_s
  else
    json_schema_to_params(value)
  end
end

#json_schema_value_prefix(prefix) ⇒ Object



84
85
86
87
88
# File 'lib/json_schema_spec.rb', line 84

def json_schema_value_prefix(prefix)
  prefix = prefix.join(':')
  prefix = "#{prefix}:"  unless prefix.empty? 
  prefix.gsub(/^[^:]*:*/, '')
end

#pick_json_schemaObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/json_schema_spec.rb', line 90

def pick_json_schema
  if defined?(Rails)
    path       = "/schema.json"
    uri        = URI.parse(path)
    env        = Rack::MockRequest.env_for(uri, 'HTTP_ACCEPT' => 'application/json')
    params     = Rails.application.routes.recognize_path(path)
    controller = "#{params[:controller]}_controller".camelize.constantize
    endpoint   = controller.action(params[:action])

    JSON.parse(endpoint.call(env)[2].body)
  else
    path = "#{File.expand_path(".")}/spec/fixtures/schema.yml"
    YAML.load(File.read(path))
  end
end

#return_nil?(key, value, required) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
# File 'lib/json_schema_spec.rb', line 106

def return_nil?(key, value, required)
  !required.include?(key) && (
    !value.is_a?(Hash) ||
    value[:optional]   ||
    value[:type] == 'null'
  )
end

#validate_json_schema(resource, method, params) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/json_schema_spec.rb', line 114

def validate_json_schema(resource, method, params)
  return  if RUBY_VERSION =~ /^1\.8\./

  schema = json_schema(resource, method)

  [ :request, :response ].each do |direction|
    validates = JSON::Validator.fully_validate(
      Util.stringify_keys(schema[direction]),
      Util.stringify_keys(params[direction]),
      :validate_schema => true
    )
    expect(validates).to eq([])
  end
end