Class: HTTP

Inherits:
Object
  • Object
show all
Defined in:
opal/opal-jquery/http.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (HTTP) initialize(url, method, options, handler = nil)

Returns a new instance of HTTP



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'opal/opal-jquery/http.rb', line 60

def initialize(url, method, options, handler=nil)
  @url     = url
  @method  = method
  @ok      = true
  @xhr     = nil
  http     = self
  payload  = options.delete :payload
  settings = options.to_n

  if handler
    @callback = @errback = handler
  end

  %x{
    if (typeof(payload) === 'string') {
      settings.data = payload;
    }
    else if (payload != nil) {
      settings.data = payload.$to_json();
      settings.contentType = 'application/json';
    }

    settings.url  = url;
    settings.type = method;

    settings.success = function(data, status, xhr) {
      http.body = data;
      http.xhr = xhr;
      http.status_code = xhr.status;

      if (typeof(data) === 'object') {
        http.json = #{ JSON.from_object `data` };
      }

      return #{ http.succeed };
    };

    settings.error = function(xhr, status, error) {
      http.body = xhr.responseText;
      http.xhr = xhr;
      http.status_code = xhr.status;

      return #{ http.fail };
    };
  }

  @settings = settings
end

Instance Attribute Details

- (Object) body (readonly)

Returns the value of attribute body



18
19
20
# File 'opal/opal-jquery/http.rb', line 18

def body
  @body
end

- (Object) error_message (readonly)

Returns the value of attribute error_message



18
19
20
# File 'opal/opal-jquery/http.rb', line 18

def error_message
  @error_message
end

- (Object) method (readonly)

Returns the value of attribute method



18
19
20
# File 'opal/opal-jquery/http.rb', line 18

def method
  @method
end

- (Object) status_code (readonly)

Returns the value of attribute status_code



18
19
20
# File 'opal/opal-jquery/http.rb', line 18

def status_code
  @status_code
end

- (Object) url (readonly)

Returns the value of attribute url



18
19
20
# File 'opal/opal-jquery/http.rb', line 18

def url
  @url
end

- (Object) xhr (readonly)

Returns the value of attribute xhr



18
19
20
# File 'opal/opal-jquery/http.rb', line 18

def xhr
  @xhr
end

Class Method Details

+ (Object) build_request(url, method, options, block)



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'opal/opal-jquery/http.rb', line 44

def self.build_request(url, method, options, block)
  unless block
    promise = ::Promise.new
    block = proc do |response|
      if response.ok?
        promise.resolve response
      else
        promise.reject response
      end
    end
  end

  http = new(url, method, options, block).send!
  promise || http
end

+ (Object) delete(url, opts = {}, &block)



32
33
34
# File 'opal/opal-jquery/http.rb', line 32

def self.delete(url, opts={}, &block)
  build_request url, :DELETE, opts, block
end

+ (Object) get(url, opts = {}, &block)



20
21
22
# File 'opal/opal-jquery/http.rb', line 20

def self.get(url, opts={}, &block)
  build_request url, :GET, opts, block
end

+ (Object) head(url, opts = {}, &block)



40
41
42
# File 'opal/opal-jquery/http.rb', line 40

def self.head(url, opts={}, &block)
  build_request url, :HEAD, opts, block
end

+ (Object) patch(url, opts = {}, &block)



36
37
38
# File 'opal/opal-jquery/http.rb', line 36

def self.patch(url, opts={}, &block)
  build_request url, :PATCH, opts, block
end

+ (Object) post(url, opts = {}, &block)



24
25
26
# File 'opal/opal-jquery/http.rb', line 24

def self.post(url, opts={}, &block)
  build_request url, :POST, opts, block
end

+ (Object) put(url, opts = {}, &block)



28
29
30
# File 'opal/opal-jquery/http.rb', line 28

def self.put(url, opts={}, &block)
  build_request url, :PUT, opts, block
end

+ (Object) setup



9
10
11
# File 'opal/opal-jquery/http.rb', line 9

def self.setup
  Hash.new(`$.ajaxSetup()`)
end

+ (Object) setup=(settings)



13
14
15
# File 'opal/opal-jquery/http.rb', line 13

def self.setup= settings
  `$.ajaxSetup(#{settings.to_n})`
end

Instance Method Details

- (Object) fail



109
110
111
112
# File 'opal/opal-jquery/http.rb', line 109

def fail
  @ok = false
  @errback.call self if @errback
end

- (String) get_header(key)

Returns the value of the specified response header.

Parameters:

  • name (String)

    of the header to get

Returns:

  • (String)

    value of the header



161
162
163
# File 'opal/opal-jquery/http.rb', line 161

def get_header(key)
  `#{xhr}.getResponseHeader(#{key});`
end

- (Object) json

Parses the http response body through json. If the response is not valid JSON then an error will very likely be thrown.

Examples:

# Getting JSON content
HTTP.get("api.json") do |response|
  puts response.json
end

# => {"key" => 1, "bar" => 2, ... }

Returns:

  • (Object)

    returns the parsed json



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

def json
  @json || JSON.parse(@body)
end

- (Boolean) ok?

Returns true if the request succeeded, false otherwise.

Examples:

HTTP.get("/some/url") do |response|
  if response.ok?
    alert "Yay!"
  else
    alert "Aww :("
  end

Returns:

  • (Boolean)

    true if request was successful



141
142
143
# File 'opal/opal-jquery/http.rb', line 141

def ok?
  @ok
end

- (HTTP) send!

Actually send this request

Returns:

  • (HTTP)

    returns self



148
149
150
151
# File 'opal/opal-jquery/http.rb', line 148

def send!
  `$.ajax(#{ @settings })`
  self
end

- (Object) succeed



153
154
155
# File 'opal/opal-jquery/http.rb', line 153

def succeed
  @callback.call self if @callback
end