Class: Browser::HTTP::Request

Inherits:
Object
  • Object
show all
Includes:
Event::Target, Native
Defined in:
lib/diamonds/opal/browser/http/request.rb

Constant Summary collapse

HEADERS =

Default headers.

{
  'X-Requested-With' => 'XMLHttpRequest',
  'X-Opal-Version'   => RUBY_ENGINE_VERSION,
  'Accept'           => 'text/javascript, text/html, application/xml, text/xml, */*'
}
STATES =
%w[uninitialized loading loaded interactive complete]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Event::Target

#attach, #attach!, convert, converters, #detach, #dispatch, included, #off, #on!, register, #trigger, #trigger!

Constructor Details

#initialize {|request| ... } ⇒ Request

Create a request with the optionally given configuration block.

Yields:

  • (request)

    if the block has a parameter the request is passed otherwise it’s instance_exec’d



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/diamonds/opal/browser/http/request.rb', line 36

def initialize(&block)
  super(transport)

  @parameters   = {}
  @query        = {}
  @headers      = Headers[HEADERS]
  @method       = :get
  @asynchronous = true
  @binary       = false
  @cacheable    = true
  @opened       = false
  @sent         = false
  @completed    = false
  @callbacks    = Hash.new { |h, k| h[k] = [] }

  if block.arity == 0
    instance_exec(&block)
  else
    block.call(self)
  end if block
end

Instance Attribute Details

#headersHeaders (readonly)

Returns the request headers.

Returns:

  • (Headers)

    the request headers



18
19
20
# File 'lib/diamonds/opal/browser/http/request.rb', line 18

def headers
  @headers
end

#methodSymbol (readonly)

Returns the HTTP method for this request.

Returns:

  • (Symbol)

    the HTTP method for this request



26
27
28
# File 'lib/diamonds/opal/browser/http/request.rb', line 26

def method
  @method
end

#responseResponse (readonly)

Returns the response associated with this request.

Returns:

  • (Response)

    the response associated with this request



22
23
24
# File 'lib/diamonds/opal/browser/http/request.rb', line 22

def response
  @response
end

#urlString, #to_s (readonly)

Returns the URL for this request.

Returns:

  • (String, #to_s)

    the URL for this request



30
31
32
# File 'lib/diamonds/opal/browser/http/request.rb', line 30

def url
  @url
end

Instance Method Details

#abortObject

Abort the request.



317
318
319
# File 'lib/diamonds/opal/browser/http/request.rb', line 317

def abort
  `#@native.abort()`
end

#asynchronous!Object

Make the request asynchronous.



100
101
102
# File 'lib/diamonds/opal/browser/http/request.rb', line 100

def asynchronous!
  @asynchronous = true
end

#asynchronous?Boolean

Check the request is asynchronous.

Returns:

  • (Boolean)


90
91
92
# File 'lib/diamonds/opal/browser/http/request.rb', line 90

def asynchronous?
  @asynchronous
end

#binary!Object

Make the request binary.



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

def binary!
  @binary = true
end

#binary?Boolean

Check the request is binary.

Returns:

  • (Boolean)


110
111
112
# File 'lib/diamonds/opal/browser/http/request.rb', line 110

def binary?
  @binary
end

#cacheable?Boolean

Check if the request is cacheable.

Returns:

  • (Boolean)


120
121
122
# File 'lib/diamonds/opal/browser/http/request.rb', line 120

def cacheable?
  @cacheable
end

#completed?Boolean

Check if the request has completed.

Returns:

  • (Boolean)


85
86
87
# File 'lib/diamonds/opal/browser/http/request.rb', line 85

def completed?
  @completed
end

#content_type(value = nil) ⇒ String

Get or set the Content-Type of the request.

Parameters:

  • value (String) (defaults to: nil)

    when passed it sets, when omitted it gets

Returns:



161
162
163
# File 'lib/diamonds/opal/browser/http/request.rb', line 161

def content_type(value = nil)
  value ? @content_type = value : @content_type
end

#encoding(value = nil) ⇒ String

Get or set the encoding of the request.

Parameters:

  • value (String) (defaults to: nil)

    when passed it sets, when omitted it gets

Returns:



170
171
172
# File 'lib/diamonds/opal/browser/http/request.rb', line 170

def encoding(value = nil)
  value ? @encoding = value : @encoding
end

#mime_type(value = nil) ⇒ String

Get or set the MIME type of the request.

Parameters:

  • value (String) (defaults to: nil)

    when passed it sets, when omitted it gets

Returns:



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

def mime_type(value = nil)
  value ? @mime_type = value : @mime_type
end

#no_cache!Object

Disable caching for this request.



125
126
127
# File 'lib/diamonds/opal/browser/http/request.rb', line 125

def no_cache!
  @cacheable = false
end

#on(what) {|response| ... } ⇒ Object

Register an event on the request.

Parameters:

  • what (Symbol, String)

    the event name

Yield Parameters:

  • response (Response)

    the response for the event



197
198
199
200
201
202
203
# File 'lib/diamonds/opal/browser/http/request.rb', line 197

def on(what, *, &block)
  if STATES.include?(what) || %w[success failure].include?(what) || Integer === what
    @callbacks[what] << block
  else
    super
  end
end

#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)


214
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
# File 'lib/diamonds/opal/browser/http/request.rb', line 214

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 += @query.encode_uri
  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

#opened?Boolean

Check if the request has been opened.

Returns:

  • (Boolean)


75
76
77
# File 'lib/diamonds/opal/browser/http/request.rb', line 75

def opened?
  @opened
end

#parameters(hash = nil) ⇒ Hash

Set the request parameters.

Parameters:

  • hash (Hash) (defaults to: nil)

    the parameters

Returns:



179
180
181
# File 'lib/diamonds/opal/browser/http/request.rb', line 179

def parameters(hash = nil)
  hash ? @parameters = hash : @parameters
end

#password(value = nil) ⇒ String

Get or set the password used for authentication.

Parameters:

  • value (String) (defaults to: nil)

    when passed it sets, when omitted it gets

Returns:



143
144
145
# File 'lib/diamonds/opal/browser/http/request.rb', line 143

def password(value = nil)
  value ? @password = value : @password
end

#query(hash = nil) ⇒ Hash

Set the URI query.

Parameters:

  • hash (Hash) (defaults to: nil)

    the query

Returns:



188
189
190
# File 'lib/diamonds/opal/browser/http/request.rb', line 188

def query(hash = nil)
  hash ? @query = hash : @query
end

#send(parameters = @parameters) ⇒ Response

Send the request with optional parameters.

Parameters:

  • parameters (String, Hash) (defaults to: @parameters)

    the data to send

Returns:



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/diamonds/opal/browser/http/request.rb', line 260

def send(parameters = @parameters)
  raise 'the request has not been opened' unless opened?

  raise 'the request has already been sent' if sent?

  # try to circumvent caching setting an If-Modified-Since header with a very
  # old date
  unless cacheable?
    `#@native.setRequestHeader("If-Modified-Since", "Tue, 11 Sep 2001 12:46:00 GMT")`
  end

  @headers.each {|name, value|
    `#@native.setRequestHeader(#{name.to_s}, #{value.to_s})`
  }

  if @content_type
    header  = @content_type
    header += "; charset=#{@encoding}" if @encoding

    `#@native.setRequestHeader('Content-Type', header)`
  end

  if binary?
    if Buffer.supported?
      `#@native.responseType = 'arraybuffer'`
    else
      `#@native.overrideMimeType('text/plain; charset=x-user-defined')`
    end
  end

  if mime_type && !binary?
    `#@native.overrideMimeType(#@mime_type)`
  end

  @sent     = true
  @response = Response.new(self)

  if String === parameters
    data = parameters
  elsif Hash === parameters && !parameters.empty?
    data = parameters.map {|vals|
      vals.map(&:encode_uri_component).join(?=)
    }.join(?&)

    unless @content_type
      `#@native.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')`
    end
  else
    data = `null`
  end

  `#@native.send(#{data})`

  @response
end

#sent?Boolean

Check if the request has been sent.

Returns:

  • (Boolean)


80
81
82
# File 'lib/diamonds/opal/browser/http/request.rb', line 80

def sent?
  @sent
end

#synchronous!Object

Make the request synchronous.



105
106
107
# File 'lib/diamonds/opal/browser/http/request.rb', line 105

def synchronous!
  @asynchronous = false
end

#synchronous?Boolean

Check the request is synchronous.

Returns:

  • (Boolean)


95
96
97
# File 'lib/diamonds/opal/browser/http/request.rb', line 95

def synchronous?
  !@asynchronous
end

#transportObject

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/diamonds/opal/browser/http/request.rb', line 61

def transport
  `new XMLHttpRequest()`
end

#user(value = nil) ⇒ String

Get or set the user used for authentication.

Parameters:

  • value (String) (defaults to: nil)

    when passed it sets, when omitted it gets

Returns:



134
135
136
# File 'lib/diamonds/opal/browser/http/request.rb', line 134

def user(value = nil)
  value ? @user = value : @user
end