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).string
    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
# File 'lib/json_schema_spec.rb', line 31

def json_schema_params(resource, method, merge={})
  schema = json_schema(resource, method)
  params = json_schema_to_params(schema)
  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, prefix = []) ⇒ Object


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

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

#json_schema_value(key, value, prefix) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/json_schema_spec.rb', line 54

def json_schema_value(key, value, prefix)
  if !value.is_a?(Hash) || value[:optional] || value[:type] == 'null'
    nil
  elsif value[:enum]
    value[:enum].shuffle.first
  elsif value[:type] == 'array'
    [ json_schema_value(key, value[:items], prefix) ]
  elsif value[:type] == 'boolean'
    true
  elsif value[:type] == 'integer'
    rand(1_000_000)
  elsif value[:type] == 'object'
    json_schema_to_params(value[:properties], prefix << key)
  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


74
75
76
77
78
# File 'lib/json_schema_spec.rb', line 74

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

#pick_json_schemaObject


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/json_schema_spec.rb', line 80

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

#validate_json_schema(resource, method, params) ⇒ Object


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/json_schema_spec.rb', line 96

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