Class: Sinatra::Schema::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/schema/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
11
12
# File 'lib/sinatra/schema/resource.rb', line 6

def initialize(options)
  @id         = options[:id]
  @path       = (options[:path] || "").chomp("/")
  @links      = []
  @defs       = {}
  @properties = {}
end

Instance Attribute Details

#defsObject

Returns the value of attribute defs.



4
5
6
# File 'lib/sinatra/schema/resource.rb', line 4

def defs
  @defs
end

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/sinatra/schema/resource.rb', line 4

def description
  @description
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/sinatra/schema/resource.rb', line 4

def id
  @id
end

Returns the value of attribute links.



4
5
6
# File 'lib/sinatra/schema/resource.rb', line 4

def links
  @links
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/sinatra/schema/resource.rb', line 4

def path
  @path
end

#propertiesObject

Returns the value of attribute properties.



4
5
6
# File 'lib/sinatra/schema/resource.rb', line 4

def properties
  @properties
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/sinatra/schema/resource.rb', line 4

def title
  @title
end

Instance Method Details

#validate_properties!(received) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sinatra/schema/resource.rb', line 43

def validate_properties!(received)
  required_properties = properties.map do |k, prop|
    # ignore nested properties for now, we'll cover these next
    k unless prop.is_a?(Hash) || prop.optional
  end.compact

  missing = required_properties.map(&:to_s).sort - received.keys.map(&:to_s).sort
  unless missing.empty?
    raise BadResponse.new("Missing properties: #{missing}")
  end

  extra = received.keys.map(&:to_s).sort - properties.keys.map(&:to_s).sort
  unless extra.empty?
    raise BadResponse.new("Unexpected properties: #{extra}")
  end

  properties.each do |id, definition|
    unless definition.valid?(received[id.to_s])
      raise BadResponse.new("Bad response property: #{id}")
    end
  end
end

#validate_response!(rel, raw) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sinatra/schema/resource.rb', line 22

def validate_response!(rel, raw)
  # only validate responses in tests
  return unless ENV["RACK_ENV"] == "test"

  res = MultiJson.decode(raw)

  if rel == :instances
    unless res.is_a?(Array)
      raise BadResponse.new("Response should return an array")
    end
    if sample = res.first
      validate_properties!(sample)
    end
  else
    unless res.is_a?(Hash)
      raise BadResponse.new("Response should return a hash")
    end
    validate_properties!(res)
  end
end