Class: Hotwire::Response::Json

Inherits:
Base
  • Object
show all
Defined in:
lib/hotwire/response/json.rb

Overview

Specialization responsible for producing responses in JSON format.

Defined Under Namespace

Classes: Date, DateTime

Instance Attribute Summary

Attributes inherited from Base

#columns, #data

Attributes inherited from Base

#errors

Instance Method Summary collapse

Methods inherited from Base

#add_column, #add_columns, #set_data

Methods inherited from Base

#add_error, #valid?, #validate

Constructor Details

#initialize(request) ⇒ Json

Returns a new instance of Json.



6
7
8
9
# File 'lib/hotwire/response/json.rb', line 6

def initialize(request)
  super(request)
  @responseHandler = "google.visualization.Query.setResponse"
end

Instance Method Details

#bodyObject

Returns the datasource in JSON format. It supports the responseHandler parameter. All the errors are returned with the invalid_request key. Warnings are unsupported (yet).



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hotwire/response/json.rb', line 14

def body
  rsp = {}
  rsp[:version] = @version
  rsp[:reqId] = @request[:reqId] if @request[:reqId]
  if valid?
    rsp[:status] = "ok"
    rsp[:table] = datatable unless data.nil?      
  else
    rsp[:status] = "error"
    rsp[:errors] = @errors
  end
  "#{@request[:responseHandler] || @responseHandler}(#{rsp.to_json})"   
end