Class: Ballast::AjaxResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/ballast/ajax_response.rb

Overview

An AJAX response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status: :ok, data: {}, error: nil, transport: nil) ⇒ AjaxResponse

Creates an AJAX response.

Parameters:

  • status (Symbol|Fixnum) (defaults to: :ok)

    The HTTP status of the response.

  • data (Object|Hash|NilClass) (defaults to: {})

    Additional data to append to the response.

  • error (Object|NilClass) (defaults to: nil)

    A error to append to the response.

  • transport (Object|NilClass) (defaults to: nil)

    The transport to use for sending. Must respond to render, params, request.format and performed?.



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

#dataObject|Hash|NilClass

Returns The data to send to the client.

Returns:

  • (Object|Hash|NilClass)

    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
86
# 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(options = {})
    {
      status: options[: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)
    content = prepare_content
    content = (pretty_json ? Oj.dump(content) : ActiveSupport::JSON.encode(content)) if [:json, :jsonp, :text].include?(format)

    transport.render(format => content, 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 || :html
  end

  # :nodoc:
  def prepare_content
    {status: Rack::Utils.status_code(status), data: data, error: error}
  end
end

#errorObject|NilClass

Returns A error message.

Returns:

  • (Object|NilClass)

    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
86
# 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(options = {})
    {
      status: options[: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)
    content = prepare_content
    content = (pretty_json ? Oj.dump(content) : ActiveSupport::JSON.encode(content)) if [:json, :jsonp, :text].include?(format)

    transport.render(format => content, 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 || :html
  end

  # :nodoc:
  def prepare_content
    {status: Rack::Utils.status_code(status), data: data, error: error}
  end
end

#statusSymbol|Fixnum

Returns The HTTP status of the response.

Returns:

  • (Symbol|Fixnum)

    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
86
# 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(options = {})
    {
      status: options[: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)
    content = prepare_content
    content = (pretty_json ? Oj.dump(content) : ActiveSupport::JSON.encode(content)) if [:json, :jsonp, :text].include?(format)

    transport.render(format => content, 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 || :html
  end

  # :nodoc:
  def prepare_content
    {status: Rack::Utils.status_code(status), data: data, error: error}
  end
end

#transportObject|NilClass

Returns The transport to use for sending. Must respond to render, params, request.format and performed?.

Returns:

  • (Object|NilClass)

    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
86
# 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(options = {})
    {
      status: options[: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)
    content = prepare_content
    content = (pretty_json ? Oj.dump(content) : ActiveSupport::JSON.encode(content)) if [:json, :jsonp, :text].include?(format)

    transport.render(format => content, 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 || :html
  end

  # :nodoc:
  def prepare_content
    {status: Rack::Utils.status_code(status), data: data, error: error}
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Hash

Returns a JSON representation of the response.

Parameters:

  • options (Hash) (defaults to: {})

    The options to use for serializing. Currently only original_status is supported.

Returns:

  • (Hash)

    A JSON representation of the response.



44
45
46
47
48
49
50
# File 'lib/ballast/ajax_response.rb', line 44

def as_json(options = {})
  {
    status: options[:original_status] ? status : numeric_status,
    data: data,
    error: error
  }
end

#numeric_statusFixnum

Returns the status as a number.

Returns:

  • (Fixnum)

    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

#reply(format: :json, pretty_json: false) ⇒ Object

Sends the response using the transport.

Parameters:

  • format (Symbol) (defaults to: :json)

    The content type of the response.

  • pretty_json (Boolean) (defaults to: false)

    If JSON response must be pretty formatted.



56
57
58
59
60
61
62
63
64
# 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)
  content = prepare_content
  content = (pretty_json ? Oj.dump(content) : ActiveSupport::JSON.encode(content)) if [:json, :jsonp, :text].include?(format)

  transport.render(format => content, status: numeric_status, callback: callback, content_type: content_type)
end