Class: Jsonatra::Response
- Inherits:
-
Sinatra::Response
- Object
- Sinatra::Response
- Jsonatra::Response
- Defined in:
- lib/jsonatra/response.rb
Instance Attribute Summary collapse
- #error ⇒ Object
-
#jsonp_callback ⇒ Object
writeonly
set this and the ‘content_type` in the `before` filter.
-
#override_processing ⇒ Object
Returns the value of attribute override_processing.
Instance Method Summary collapse
- #add_header_error(header, type, message) {|error| ... } ⇒ Object
- #add_parameter_error(parameter, type, message) {|error| ... } ⇒ Object
- #camelcase_error_types ⇒ Object
- #error? ⇒ Boolean
- #finish ⇒ Object
-
#sinatra_finish ⇒ Object
new #finish method to handle error reporting and json(p)-ification.
Instance Attribute Details
#error ⇒ Object
49 50 51 52 |
# File 'lib/jsonatra/response.rb', line 49 def error @error ||= {} @error end |
#jsonp_callback=(value) ⇒ Object (writeonly)
set this and the ‘content_type` in the `before` filter
11 12 13 |
# File 'lib/jsonatra/response.rb', line 11 def jsonp_callback=(value) @jsonp_callback = value end |
#override_processing ⇒ Object
Returns the value of attribute override_processing.
7 8 9 |
# File 'lib/jsonatra/response.rb', line 7 def override_processing @override_processing end |
Instance Method Details
#add_header_error(header, type, message) {|error| ... } ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/jsonatra/response.rb', line 65 def add_header_error header, type, error[:type] ||= 'invalid_header' error[:message] ||= 'invalid header or header value' error[:headers] ||= {} error[:headers][header.to_sym] ||= [] yield error if block_given? error[:headers][header.to_sym] << {type: type, message: } end |
#add_parameter_error(parameter, type, message) {|error| ... } ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/jsonatra/response.rb', line 56 def add_parameter_error parameter, type, error[:type] ||= 'invalid_input' error[:message] ||= 'invalid parameter or parameter value' error[:parameters] ||= {} error[:parameters][parameter.to_sym] ||= [] yield error if block_given? error[:parameters][parameter.to_sym] << {type: type, message: } end |
#camelcase_error_types ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/jsonatra/response.rb', line 74 def camelcase_error_types error[:type] = error[:type].camelcase if error[:headers] error[:headers].each {|k,v| v.each {|he| he[:type] = he[:type].camelcase}} end if error[:parameters] error[:parameters].each {|k,v| v.each {|pe| pe[:type] = pe[:type].camelcase}} end end |
#error? ⇒ Boolean
54 |
# File 'lib/jsonatra/response.rb', line 54 def error?; !error.empty?; end |
#finish ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/jsonatra/response.rb', line 16 def finish unless @override_processing if self.error? self.body = {error: @error.delete_if {|k,v| v.nil?}}.to_json else # TODO what if there are more elements in the array? # if Array === self.body self.body = self.body[0] # JSON is not valid unless it's "{}" or "[]" # self.body ||= {} end if Hash === self.body json_body = self.body.to_json if @jsonp_callback self.body = "#{@jsonp_callback}(#{json_body});" else self.body = json_body end end end end sinatra_finish end |
#sinatra_finish ⇒ Object
new #finish method to handle error reporting and json(p)-ification
15 |
# File 'lib/jsonatra/response.rb', line 15 alias sinatra_finish finish |