Class: Blix::Rest::JsonFormatParser

Inherits:
FormatParser show all
Defined in:
lib/blix/rest/format_parser.rb

Overview


the default json format parser

Instance Attribute Summary

Attributes inherited from FormatParser

#__custom_headers, #_format, #_options

Instance Method Summary collapse

Methods inherited from FormatParser

_types, #_types, accept_types

Instance Method Details

#format_error(message) ⇒ Object



68
69
70
# File 'lib/blix/rest/format_parser.rb', line 68

def format_error(message)
  MultiJson.dump({"error"=>message.to_s}) rescue "{\"error\":\"Internal Formatting Error\"}"
end

#format_response(value, response) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/blix/rest/format_parser.rb', line 72

def format_response(value, response)
  if value.is_a?(RawJsonString)
    response.content = if _options[:nodata]
                         [value.to_s]
                       else
                         ["{\"data\":#{value}}"]
                       end
  else
    begin
      response.content = if _options[:nodata]
                           [MultiJson.dump(value)]
                         else
                           [MultiJson.dump('data' => value)]
                       end
    rescue Exception => e
      ::Blix::Rest.logger << e.to_s
      response.set(500, format_error('Internal Formatting Error'))
    end
  end
end

#set_default_headers(headers) ⇒ Object



62
63
64
65
66
# File 'lib/blix/rest/format_parser.rb', line 62

def set_default_headers(headers)
  headers[CACHE_CONTROL] = CACHE_NO_STORE
  headers[PRAGMA]       = NO_CACHE
  headers[CONTENT_TYPE] = CONTENT_TYPE_JSON
end