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



93
94
95
# File 'opal/browser/http.rb', line 93

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:



164
165
166
# File 'opal/browser/http.rb', line 164

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



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

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:



117
118
119
# File 'opal/browser/http.rb', line 117

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



57
58
59
# File 'opal/browser/http.rb', line 57

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:



128
129
130
# File 'opal/browser/http.rb', line 128

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



69
70
71
# File 'opal/browser/http.rb', line 69

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:



140
141
142
# File 'opal/browser/http.rb', line 140

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



81
82
83
# File 'opal/browser/http.rb', line 81

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:



152
153
154
# File 'opal/browser/http.rb', line 152

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



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

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:



106
107
108
# File 'opal/browser/http.rb', line 106

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)


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

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