Class: Browser::HTTP::Response

Inherits:
Object
  • Object
show all
Includes:
Native
Defined in:
opal/browser/http/response.rb

Overview

Represents an HTTP response.

Defined Under Namespace

Classes: Status

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Response

Create a response from a request.

Parameters:

  • request (Request)

    the request



18
19
20
21
22
# File 'opal/browser/http/response.rb', line 18

def initialize(request)
  super(request.to_n)

  @request = request
end

Instance Attribute Details

#binaryBinary (readonly)

Returns the response body as binary.

Returns:

  • (Binary)

    the response body as binary



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'opal/browser/http/response.rb', line 94

def binary
  return unless request.binary?

  if Buffer.supported?
    %x{
      var result = #@native.response;

      if (!result) {
        return nil;
      }
    }

    Binary.new(Buffer.new(`result`))
  else
    return unless text

    Binary.new(text)
  end
end

#headersHeaders (readonly)

Returns the response headers.

Returns:

  • (Headers)

    the response headers



26
27
28
# File 'opal/browser/http/response.rb', line 26

def headers
  @headers ||= Headers.parse(`#@native.getAllResponseHeaders()`)
end

#jsonHash, Array (readonly)

Returns the response body as JSON.

Returns:

  • (Hash, Array)

    the response body as JSON



66
67
68
69
70
71
72
73
74
75
76
# File 'opal/browser/http/response.rb', line 66

def json
  %x{
    var result = #@native.responseText;

    if (!result) {
      return nil;
    }

    return #{JSON.parse(`result`)};
  }
end

#requestRequest (readonly)

Returns the request to this response.

Returns:

  • (Request)

    the request to this response



13
14
15
# File 'opal/browser/http/response.rb', line 13

def request
  @request
end

#statusStatus (readonly)

Returns the response status.

Returns:

  • (Status)

    the response status



32
33
34
# File 'opal/browser/http/response.rb', line 32

def status
  Status.new(`#@native.status || nil`, `#@native.statusText || nil`)
end

#textString (readonly)

Returns the response body as text.

Returns:

  • (String)

    the response body as text



52
53
54
55
56
57
58
59
60
61
62
# File 'opal/browser/http/response.rb', line 52

def text
  %x{
    var result = #@native.responseText;

    if (!result) {
      return nil;
    }

    return result;
  }
end

#xmlDOM::Document (readonly)

Returns the response body as DOM document.

Returns:



80
81
82
83
84
85
86
87
88
89
90
# File 'opal/browser/http/response.rb', line 80

def xml
  %x{
    var result = #@native.responseXML;

    if (!result) {
      return nil;
    }
  }

  DOM(`result`)
end

Instance Method Details

#failure?Boolean

Check if the response failed

Returns:

  • (Boolean)


46
47
48
# File 'opal/browser/http/response.rb', line 46

def failure?
  !success?
end

#success?Boolean

Checks if the response was successful

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'opal/browser/http/response.rb', line 37

def success?
  if code = status.code
    code >= 200 && code < 300 || code == 304
  else
    false
  end
end