Class: Respect::Rails::ResponseSchema
- Inherits:
-
Object
- Object
- Respect::Rails::ResponseSchema
- Includes:
- DocHelper, HeadersSimplifier
- Defined in:
- lib/respect/rails/response_schema.rb
Instance Attribute Summary (collapse)
-
- (Object) body
Returns the value of attribute body.
-
- (Object) documentation
Returns the value of attribute documentation.
-
- (Object) headers
Returns the value of attribute headers.
-
- (Object) last_error
readonly
Returns the value of attribute last_error.
-
- (Object) status
readonly
Returns the value of attribute status.
Class Method Summary (collapse)
- + (Object) define(*args, &block)
-
+ (Object) from_file(status, filename)
Read the response schema definition from the given
filename
and evaluate it as a block passed to ResponseSchema.define. - + (Object) http_status(status)
-
+ (Object) symbolize_http_status(http_status)
FIXME(Nicolas Despres): Move me to another module/class.
Instance Method Summary (collapse)
- - (Object) ==(other)
- - (Object) http_status
-
- (ResponseSchema) initialize(status = :ok)
constructor
A new instance of ResponseSchema.
- - (Object) validate(response)
- - (Boolean) validate?(response)
Methods included from HeadersSimplifier
Constructor Details
- (ResponseSchema) initialize(status = :ok)
A new instance of ResponseSchema
32 33 34 35 |
# File 'lib/respect/rails/response_schema.rb', line 32 def initialize(status = :ok) @status = status @headers = HashSchema.new end |
Instance Attribute Details
- (Object) body
Returns the value of attribute body
43 44 45 |
# File 'lib/respect/rails/response_schema.rb', line 43 def body @body end |
- (Object) documentation
Returns the value of attribute documentation
81 82 83 |
# File 'lib/respect/rails/response_schema.rb', line 81 def documentation @documentation end |
- (Object) headers
Returns the value of attribute headers
80 81 82 |
# File 'lib/respect/rails/response_schema.rb', line 80 def headers @headers end |
- (Object) last_error (readonly)
Returns the value of attribute last_error
78 79 80 |
# File 'lib/respect/rails/response_schema.rb', line 78 def last_error @last_error end |
- (Object) status (readonly)
Returns the value of attribute status
37 38 39 |
# File 'lib/respect/rails/response_schema.rb', line 37 def status @status end |
Class Method Details
+ (Object) define(*args, &block)
19 20 21 |
# File 'lib/respect/rails/response_schema.rb', line 19 def define(*args, &block) ResponseDef.eval(*args, &block) end |
+ (Object) from_file(status, filename)
Read the response schema definition from the given filename
and evaluate it as a block passed to define
25 26 27 28 29 |
# File 'lib/respect/rails/response_schema.rb', line 25 def from_file(status, filename) define(status) do |r| r.instance_eval(File.read(filename), filename) end end |
+ (Object) http_status(status)
9 10 11 |
# File 'lib/respect/rails/response_schema.rb', line 9 def http_status(status) Rack::Utils.status_code(status) end |
+ (Object) symbolize_http_status(http_status)
FIXME(Nicolas Despres): Move me to another module/class.
14 15 16 17 |
# File 'lib/respect/rails/response_schema.rb', line 14 def symbolize_http_status(http_status) h = Rack::Utils::HTTP_STATUS_CODES (h[http_status] || h[500]).downcase.gsub(/\s|-/, '_').to_sym end |
Instance Method Details
- (Object) ==(other)
45 46 47 |
# File 'lib/respect/rails/response_schema.rb', line 45 def ==(other) @status == other.status && @body == other.body end |
- (Object) http_status
39 40 41 |
# File 'lib/respect/rails/response_schema.rb', line 39 def http_status self.class.http_status(@status) end |
- (Object) validate(response)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/respect/rails/response_schema.rb', line 49 def validate(response) if headers begin headers.validate(response.headers) rescue Respect::ValidationError => e raise Respect::Rails::ResponseValidationError.new(e, :headers, simplify_headers(response.headers)) end end if body decoded_body = ActiveSupport::JSON.decode(response.body) begin body.validate(decoded_body) rescue Respect::ValidationError => e raise Respect::Rails::ResponseValidationError.new(e, :body, decoded_body) end end true end |
- (Boolean) validate?(response)
68 69 70 71 72 73 74 75 76 |
# File 'lib/respect/rails/response_schema.rb', line 68 def validate?(response) begin validate(response) true rescue Respect::Rails::ResponseValidationError => e @last_error = e false end end |