Class: Sinatra::Schema::Resource
- Inherits:
-
Object
- Object
- Sinatra::Schema::Resource
- Defined in:
- lib/sinatra/schema/resource.rb
Instance Attribute Summary collapse
-
#defs ⇒ Object
Returns the value of attribute defs.
-
#description ⇒ Object
Returns the value of attribute description.
-
#id ⇒ Object
Returns the value of attribute id.
-
#links ⇒ Object
Returns the value of attribute links.
-
#path ⇒ Object
Returns the value of attribute path.
-
#properties ⇒ Object
Returns the value of attribute properties.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
-
#initialize(options) ⇒ Resource
constructor
A new instance of Resource.
- #validate_properties!(received) ⇒ Object
- #validate_response!(rel, raw) ⇒ Object
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() @id = [:id] @path = ([:path] || "").chomp("/") @links = [] @defs = {} @properties = {} end |
Instance Attribute Details
#defs ⇒ Object
Returns the value of attribute defs.
4 5 6 |
# File 'lib/sinatra/schema/resource.rb', line 4 def defs @defs end |
#description ⇒ Object
Returns the value of attribute description.
4 5 6 |
# File 'lib/sinatra/schema/resource.rb', line 4 def description @description end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/sinatra/schema/resource.rb', line 4 def id @id end |
#links ⇒ Object
Returns the value of attribute links.
4 5 6 |
# File 'lib/sinatra/schema/resource.rb', line 4 def links @links end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/sinatra/schema/resource.rb', line 4 def path @path end |
#properties ⇒ Object
Returns the value of attribute properties.
4 5 6 |
# File 'lib/sinatra/schema/resource.rb', line 4 def properties @properties end |
#title ⇒ Object
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 |