Class: Browser::HTTP::Response

Inherits:
Object
  • Object
show all
Includes:
Native::Wrapper
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


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'opal/browser/http/response.rb', line 98

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


70
71
72
73
74
75
76
77
78
79
80
# File 'opal/browser/http/response.rb', line 70

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


56
57
58
59
60
61
62
63
64
65
66
# File 'opal/browser/http/response.rb', line 56

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

    if (!result) {
      return nil;
    }

    return result;
  }
end

#urlString (readonly)

Returns the response URL (after redirects).

Returns:

  • (String)

    the response URL (after redirects)


52
# File 'opal/browser/http/response.rb', line 52

alias_native :url, :responseURL

#xmlDOM::Document (readonly)

Returns the response body as DOM document.

Returns:


84
85
86
87
88
89
90
91
92
93
94
# File 'opal/browser/http/response.rb', line 84

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