Method: Browser::HTTP::Request#open

Defined in:
opal/browser/http/request.rb

#open(method = nil, url = nil, asynchronous = nil, user = nil, password = nil) ⇒ self

Open the request.

Parameters:

  • method (Symbol) (defaults to: nil)

    the HTTP method to use

  • url (String, #to_s) (defaults to: nil)

    the URL to send the request to

  • asynchronous (Boolean) (defaults to: nil)

    whether the request is asynchronous or not

  • user (String) (defaults to: nil)

    the user to use for authentication

  • password (String) (defaults to: nil)

    the password to use for authentication

Returns:

  • (self)
[View source]

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'opal/browser/http/request.rb', line 215

def open(method = nil, url = nil, asynchronous = nil, user = nil, password = nil)
  raise 'the request has already been opened' if opened?

  @method       = method       unless method.nil?
  @url          = url          unless url.nil?
  @asynchronous = asynchronous unless asynchronous.nil?
  @user         = user         unless user.nil?
  @password     = password     unless password.nil?

  url = @url

  # add a dummy random parameter to the query to try circumvent caching
  unless cacheable?
    @query[:_] = rand
  end

  # add the encoded query to the @url, prepending the right character if
  # there was already a query in the defined @url or not
  unless @query.empty?
    if url.include? ??
      url += ?&
    else
      url += ??
    end

    url += FormData.build_query(@query)
  end

  `#@native.open(#{@method.to_s.upcase}, #{url.to_s}, #{@asynchronous}, #{@user.to_n}, #{@password.to_n})`

  # if there are no registered callbacks no point in setting the event
  # handler
  unless @callbacks.empty?
    `#@native.onreadystatechange = #{callback}`
  end

  @opened = true

  self
end