Class: Ballast::AjaxResponse
- Inherits:
-
Object
- Object
- Ballast::AjaxResponse
- Defined in:
- lib/ballast/ajax_response.rb
Overview
An AJAX response.
Instance Attribute Summary (collapse)
-
- (Object|Hash|NilClass) data
The data to send to the client.
-
- (Object|NilClass) error
A error message.
-
- (Symbol|Fixnum) status
The HTTP status of the response.
-
- (Object|NilClass) transport
The transport to use for sending.
Instance Method Summary (collapse)
-
- (Hash) as_json(options = {})
Returns a JSON representation of the response.
-
- (AjaxResponse) initialize(status: :ok, data: {}, error: nil, transport: nil)
constructor
Creates an AJAX response.
-
- (Fixnum) numeric_status
Returns the status as a number.
-
- (Object) reply(format: :json, pretty_json: false)
Sends the response using the transport.
Constructor Details
- (AjaxResponse) initialize(status: :ok, data: {}, error: nil, transport: nil)
Creates an AJAX response.
26 27 28 29 30 31 |
# File 'lib/ballast/ajax_response.rb', line 26 def initialize(status: :ok, data: {}, error: nil, transport: nil) @status = status @data = data @error = error @transport = transport end |
Instance Attribute Details
- (Object|Hash|NilClass) data
Returns The data to send to the client.
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ballast/ajax_response.rb', line 17 class AjaxResponse attr_accessor :status, :data, :error, :transport # Creates an AJAX response. # # @param status [Symbol|Fixnum] The HTTP status of the response. # @param data [Object|Hash|NilClass] Additional data to append to the response. # @param error [Object|NilClass] A error to append to the response. # @param transport [Object|NilClass] The transport to use for sending. Must respond to `render`, `params`, `request.format` and `performed?`. def initialize(status: :ok, data: {}, error: nil, transport: nil) @status = status @data = data @error = error @transport = transport end # Returns the status as a number. # # @return [Fixnum] The status as a number. def numeric_status status.is_a?(Fixnum) ? status : Rack::Utils.status_code(status.ensure_string.to_sym) end # Returns a JSON representation of the response. # # @param options [Hash] The options to use for serializing. Currently only `original_status` is supported. # @return [Hash] A JSON representation of the response. def as_json( = {}) { status: [:original_status] ? status : numeric_status, data: data, error: error } end # Sends the response using the transport. # # @param format [Symbol] The content type of the response. # @param pretty_json [Boolean] If JSON response must be pretty formatted. def reply(format: :json, pretty_json: false) return if transport.performed? format, callback, content_type = format_reply(format) data = (pretty_json ? Oj.dump(payload) : ActiveSupport::JSON.encode(payload)) if [:json, :jsonp, :text].include?(format) transport.render(format => data, status: numeric_status, callback: callback, content_type: content_type) end private # :nodoc: def format_reply(format) format = choose_format(format) callback = [:jsonp, :pretty_jsonp].include?(format) ? (transport.params[:callback] || "jsonp#{Time.now.to_i}") : nil content_type = (format == :text) ? "text/plain" : nil [format, callback, content_type] end # :nodoc: def choose_format(format) (format || transport.params[:format] || transport.request.format || "json").to_sym end # :nodoc: def payload {status: status, data: data, error: error} end end |
- (Object|NilClass) error
Returns A error message.
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ballast/ajax_response.rb', line 17 class AjaxResponse attr_accessor :status, :data, :error, :transport # Creates an AJAX response. # # @param status [Symbol|Fixnum] The HTTP status of the response. # @param data [Object|Hash|NilClass] Additional data to append to the response. # @param error [Object|NilClass] A error to append to the response. # @param transport [Object|NilClass] The transport to use for sending. Must respond to `render`, `params`, `request.format` and `performed?`. def initialize(status: :ok, data: {}, error: nil, transport: nil) @status = status @data = data @error = error @transport = transport end # Returns the status as a number. # # @return [Fixnum] The status as a number. def numeric_status status.is_a?(Fixnum) ? status : Rack::Utils.status_code(status.ensure_string.to_sym) end # Returns a JSON representation of the response. # # @param options [Hash] The options to use for serializing. Currently only `original_status` is supported. # @return [Hash] A JSON representation of the response. def as_json( = {}) { status: [:original_status] ? status : numeric_status, data: data, error: error } end # Sends the response using the transport. # # @param format [Symbol] The content type of the response. # @param pretty_json [Boolean] If JSON response must be pretty formatted. def reply(format: :json, pretty_json: false) return if transport.performed? format, callback, content_type = format_reply(format) data = (pretty_json ? Oj.dump(payload) : ActiveSupport::JSON.encode(payload)) if [:json, :jsonp, :text].include?(format) transport.render(format => data, status: numeric_status, callback: callback, content_type: content_type) end private # :nodoc: def format_reply(format) format = choose_format(format) callback = [:jsonp, :pretty_jsonp].include?(format) ? (transport.params[:callback] || "jsonp#{Time.now.to_i}") : nil content_type = (format == :text) ? "text/plain" : nil [format, callback, content_type] end # :nodoc: def choose_format(format) (format || transport.params[:format] || transport.request.format || "json").to_sym end # :nodoc: def payload {status: status, data: data, error: error} end end |
- (Symbol|Fixnum) status
Returns The HTTP status of the response.
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ballast/ajax_response.rb', line 17 class AjaxResponse attr_accessor :status, :data, :error, :transport # Creates an AJAX response. # # @param status [Symbol|Fixnum] The HTTP status of the response. # @param data [Object|Hash|NilClass] Additional data to append to the response. # @param error [Object|NilClass] A error to append to the response. # @param transport [Object|NilClass] The transport to use for sending. Must respond to `render`, `params`, `request.format` and `performed?`. def initialize(status: :ok, data: {}, error: nil, transport: nil) @status = status @data = data @error = error @transport = transport end # Returns the status as a number. # # @return [Fixnum] The status as a number. def numeric_status status.is_a?(Fixnum) ? status : Rack::Utils.status_code(status.ensure_string.to_sym) end # Returns a JSON representation of the response. # # @param options [Hash] The options to use for serializing. Currently only `original_status` is supported. # @return [Hash] A JSON representation of the response. def as_json( = {}) { status: [:original_status] ? status : numeric_status, data: data, error: error } end # Sends the response using the transport. # # @param format [Symbol] The content type of the response. # @param pretty_json [Boolean] If JSON response must be pretty formatted. def reply(format: :json, pretty_json: false) return if transport.performed? format, callback, content_type = format_reply(format) data = (pretty_json ? Oj.dump(payload) : ActiveSupport::JSON.encode(payload)) if [:json, :jsonp, :text].include?(format) transport.render(format => data, status: numeric_status, callback: callback, content_type: content_type) end private # :nodoc: def format_reply(format) format = choose_format(format) callback = [:jsonp, :pretty_jsonp].include?(format) ? (transport.params[:callback] || "jsonp#{Time.now.to_i}") : nil content_type = (format == :text) ? "text/plain" : nil [format, callback, content_type] end # :nodoc: def choose_format(format) (format || transport.params[:format] || transport.request.format || "json").to_sym end # :nodoc: def payload {status: status, data: data, error: error} end end |
- (Object|NilClass) transport
Returns The transport to use for sending. Must respond to render
, params
, request.format
and performed?
.
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ballast/ajax_response.rb', line 17 class AjaxResponse attr_accessor :status, :data, :error, :transport # Creates an AJAX response. # # @param status [Symbol|Fixnum] The HTTP status of the response. # @param data [Object|Hash|NilClass] Additional data to append to the response. # @param error [Object|NilClass] A error to append to the response. # @param transport [Object|NilClass] The transport to use for sending. Must respond to `render`, `params`, `request.format` and `performed?`. def initialize(status: :ok, data: {}, error: nil, transport: nil) @status = status @data = data @error = error @transport = transport end # Returns the status as a number. # # @return [Fixnum] The status as a number. def numeric_status status.is_a?(Fixnum) ? status : Rack::Utils.status_code(status.ensure_string.to_sym) end # Returns a JSON representation of the response. # # @param options [Hash] The options to use for serializing. Currently only `original_status` is supported. # @return [Hash] A JSON representation of the response. def as_json( = {}) { status: [:original_status] ? status : numeric_status, data: data, error: error } end # Sends the response using the transport. # # @param format [Symbol] The content type of the response. # @param pretty_json [Boolean] If JSON response must be pretty formatted. def reply(format: :json, pretty_json: false) return if transport.performed? format, callback, content_type = format_reply(format) data = (pretty_json ? Oj.dump(payload) : ActiveSupport::JSON.encode(payload)) if [:json, :jsonp, :text].include?(format) transport.render(format => data, status: numeric_status, callback: callback, content_type: content_type) end private # :nodoc: def format_reply(format) format = choose_format(format) callback = [:jsonp, :pretty_jsonp].include?(format) ? (transport.params[:callback] || "jsonp#{Time.now.to_i}") : nil content_type = (format == :text) ? "text/plain" : nil [format, callback, content_type] end # :nodoc: def choose_format(format) (format || transport.params[:format] || transport.request.format || "json").to_sym end # :nodoc: def payload {status: status, data: data, error: error} end end |
Instance Method Details
- (Hash) as_json(options = {})
Returns a JSON representation of the response.
44 45 46 47 48 49 50 |
# File 'lib/ballast/ajax_response.rb', line 44 def as_json( = {}) { status: [:original_status] ? status : numeric_status, data: data, error: error } end |
- (Fixnum) numeric_status
Returns the status as a number.
36 37 38 |
# File 'lib/ballast/ajax_response.rb', line 36 def numeric_status status.is_a?(Fixnum) ? status : Rack::Utils.status_code(status.ensure_string.to_sym) end |
- (Object) reply(format: :json, pretty_json: false)
Sends the response using the transport.
56 57 58 59 60 61 62 63 |
# File 'lib/ballast/ajax_response.rb', line 56 def reply(format: :json, pretty_json: false) return if transport.performed? format, callback, content_type = format_reply(format) data = (pretty_json ? Oj.dump(payload) : ActiveSupport::JSON.encode(payload)) if [:json, :jsonp, :text].include?(format) transport.render(format => data, status: numeric_status, callback: callback, content_type: content_type) end |