Sha256: 105db00a92f06a681625ad127b6a16354f80e4ba3552295e9e53bc2e2e222291

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

require 'bowser/http/response'
require 'bowser/event_target'

module Bowser
  module HTTP
    class Request
      include EventTarget

      attr_reader :method, :url, :data, :headers, :promise
      attr_accessor :response

      UNSENT           = 0
      OPENED           = 1
      HEADERS_RECEIVED = 2
      LOADING          = 3
      DONE             = 4

      def initialize(method, url, native: `new XMLHttpRequest()`)
        @native = native
        @method = method
        @url = url
        @response = Response.new(@native)
      end

      def send(data: {}, headers: {})
        `#@native.open(#{method}, #{url})`
        @data = data
        self.headers = headers

        if method == :get || method == :delete
          `#@native.send()`
        elsif Hash === data
          `#@native.send(#{JSON.generate data})`
        elsif `!!data.native`
          `#@native.send(data.native)`
        else
          `#@native.send(data)`
        end

        self
      end

      def headers= headers
        @headers = headers
        headers.each do |attr, value|
          `#@native.setRequestHeader(attr, value)`
        end
      end

      def post?
        method == :post
      end

      def get?
        method == :get
      end

      def ready_state
        `#@native.readyState`
      end

      def sent?
        ready_state >= OPENED
      end

      def headers_received?
        ready_state >= HEADERS_RECEIVED
      end

      def loading?
        ready_state == LOADING
      end

      def done?
        ready_state >= DONE
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bowser-0.2.2 opal/bowser/http/request.rb
bowser-0.2.1 opal/bowser/http/request.rb
bowser-0.2.0 opal/bowser/http/request.rb