Module: Browser::HTTP

Defined in:
opal/browser/http.rb,
opal/browser/http/binary.rb,
opal/browser/http/headers.rb,
opal/browser/http/request.rb,
opal/browser/http/response.rb

Defined Under Namespace

Classes: Binary, Header, Headers, Request, Response

Class Method Summary collapse

Class Method Details

.delete(url, data = nil) {|request| ... } ⇒ Promise

Send an asynchronous DELETE request.

Parameters:

  • url (String)

    the URL to request

  • data (String, Hash) (defaults to: nil)

    the data to send

Yield Parameters:

  • request (Request)

    the request to configure

Returns:

  • (Promise)

    a promise that will be resolved with the response



91
92
93
# File 'opal/browser/http.rb', line 91

def self.delete(url, data = nil, &block)
  send(:delete, url, data, &block)
end

.delete!(url, data = nil) {|request| ... } ⇒ Response

Send a synchronous DELETE request.

Parameters:

  • url (String)

    the URL to request

  • data (String, Hash) (defaults to: nil)

    the data to send

Yield Parameters:

  • request (Request)

    the request to configure

Returns:



162
163
164
# File 'opal/browser/http.rb', line 162

def self.delete!(url, data = nil, &block)
  send!(:delete, url, data, &block)
end

.get(url) {|request| ... } ⇒ Promise

Send an asynchronous GET request.

Parameters:

  • url (String)

    the URL to request

Yield Parameters:

  • request (Request)

    the request to configure

Returns:

  • (Promise)

    a promise that will be resolved with the response



44
45
46
# File 'opal/browser/http.rb', line 44

def self.get(url, &block)
  send(:get, url, &block)
end

.get!(url) {|request| ... } ⇒ Response

Send a synchronous GET request.

Parameters:

  • url (String)

    the URL to request

Yield Parameters:

  • request (Request)

    the request to configure

Returns:



115
116
117
# File 'opal/browser/http.rb', line 115

def self.get!(url, &block)
  send!(:get, url, &block)
end

.head(url) {|request| ... } ⇒ Promise

Send an asynchronous HEAD request.

Parameters:

  • url (String)

    the URL to request

Yield Parameters:

  • request (Request)

    the request to configure

Returns:

  • (Promise)

    a promise that will be resolved with the response



55
56
57
# File 'opal/browser/http.rb', line 55

def self.head(url, &block)
  send(:head, url, &block)
end

.head!(url) {|request| ... } ⇒ Response

Send a synchronous HEAD request.

Parameters:

  • url (String)

    the URL to request

Yield Parameters:

  • request (Request)

    the request to configure

Returns:



126
127
128
# File 'opal/browser/http.rb', line 126

def self.head!(url, &block)
  send!(:head, url, &block)
end

.post(url, data = nil) {|request| ... } ⇒ Promise

Send an asynchrnous POST request.

Parameters:

  • url (String)

    the URL to request

  • data (String, Hash) (defaults to: nil)

    the data to send

Yield Parameters:

  • request (Request)

    the request to configure

Returns:

  • (Promise)

    a promise that will be resolved with the response



67
68
69
# File 'opal/browser/http.rb', line 67

def self.post(url, data = nil, &block)
  send(:post, url, data, &block)
end

.post!(url, data = nil) {|request| ... } ⇒ Response

Send a synchronous POST request.

Parameters:

  • url (String)

    the URL to request

  • data (String, Hash) (defaults to: nil)

    the data to send

Yield Parameters:

  • request (Request)

    the request to configure

Returns:



138
139
140
# File 'opal/browser/http.rb', line 138

def self.post!(url, data = nil, &block)
  send!(:post, url, data, &block)
end

.put(url, data = nil) {|request| ... } ⇒ Promise

Send an asynchronous PUT request.

Parameters:

  • url (String)

    the URL to request

  • data (String, Hash) (defaults to: nil)

    the data to send

Yield Parameters:

  • request (Request)

    the request to configure

Returns:

  • (Promise)

    a promise that will be resolved with the response



79
80
81
# File 'opal/browser/http.rb', line 79

def self.put(url, data = nil, &block)
  send(:put, url, data, &block)
end

.put!(url, data = nil) {|request| ... } ⇒ Response

Send a synchronous PUT request.

Parameters:

  • url (String)

    the URL to request

  • data (String, Hash) (defaults to: nil)

    the data to send

Yield Parameters:

  • request (Request)

    the request to configure

Returns:



150
151
152
# File 'opal/browser/http.rb', line 150

def self.put!(url, data = nil, &block)
  send!(:put, url, data, &block)
end

.send(method, url, data = nil) {|request| ... } ⇒ Promise

Send an asynchronous request.

Parameters:

  • method (Symbol)

    the HTTP method to use

  • url (String)

    the URL to request

  • data (String, Hash) (defaults to: nil)

    the data to send

Yield Parameters:

  • request (Request)

    the request to configure

Returns:

  • (Promise)

    a promise that will be resolved with the response



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'opal/browser/http.rb', line 23

def self.send(method, url, data = nil, &block)
  Promise.new.tap {|promise|
    Request.new(&block).tap {|req|
      req.on :success do |res|
        promise.resolve(res)
      end

      req.on :failure do |res|
        promise.reject(res)
      end
    }.open(method, url).send(data)
  }
end

.send!(method, url, data = nil) {|request| ... } ⇒ Response

Send a synchronous request.

Parameters:

  • method (Symbol)

    the HTTP method to use

  • url (String)

    the URL to request

  • data (String, Hash) (defaults to: nil)

    the data to send

Yield Parameters:

  • request (Request)

    the request to configure

Returns:



104
105
106
# File 'opal/browser/http.rb', line 104

def self.send!(method, url, data = nil, &block)
  Request.new(&block).open(method, url, false).send(data)
end

.supported?Boolean

Check if HTTP requests are supported.

Returns:

  • (Boolean)


10
11
12
# File 'opal/browser/http.rb', line 10

def self.supported?
  Browser.supports?('XHR') || Browser.supports?('ActiveXObject')
end