Method: Request#execute

Defined in:
lib/source/redshift/request.rb

#execute(options = {}) ⇒ Object

call-seq:

req.execute(options = {}) -> req

Returns req immediately if the request is already running. Otherwise, opens a connection and sends the data provided with the specified options.

Raises:



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/source/redshift/request.rb', line 82

def execute(options = {})
  # return self unless self.check(options)
  @options.update(options)
  raise(TypeError, 'can\'t convert %s to a String' % @options[:url].inspect) unless [String].include?(@options[:url].class)
  raise(TypeError, 'can\'t convert %s to a String' % @options[:method].inspect) unless [String, Symbol].include?(@options[:method].class)
  raise(TypeError, 'can\'t convert %s to a Hash' % @options[:data].inspect) unless [Hash].include?(@options[:data].class)
  raise(HttpMethodError, 'invalid HTTP method "%s" for %s' % [@options[:method],self]) unless METHODS.include?(method = @options[:method].to_s.upcase)
  
  @running = true
  data = @options[:data].to_query_string
  url = @options[:url]
  
  if @options[:format]
    format = "format=%s" % @options[:format]
    data   = data.empty? ? format : [format, data].join('&')
  end
  
  if @options[:emulation] && %w(PUT DELETE).include?(method)
    _method = "_method=%s" % method
    data    = data.empty? ? _method : [_method, data].join('&')
    method  = 'POST'
  end
  
  if @options[:url_encoded] && method == 'POST'
    encoding = @options[:encoding] ? "; charset=%s" % @options[:encoding] : ""
    self.headers['Content-type'] = "application/x-www-form-urlencoded" + encoding
  end
  
  if data && method == 'GET'
    separator = url.include?('?') ? "&" : "?"
    url       = [url, data].join(separator)
    data      = nil
  end
  
  `this.__xhr__.open(method.__value__, url.__value__, #{@options[:async]})`
  `this.__xhr__.onreadystatechange = #{self.on_state_change}.__block__`
  
  @options[:headers].each do |k,v|
    `this.__xhr__.setRequestHeader(k.__value__,v.__value__)`
  # raise(HeaderError, "#{k} => #{v}")
  end
  
  self.fire(:request)
  `this.__xhr__.send($T(data)?data.__value__:'')`
  self.on_state_change.call unless @options[:async]
  return self
end