Method: MLS#delete

Defined in:
lib/mls.rb

#delete(url, body = {}, *valid_response_codes, &block) ⇒ Object

Deletes to url on the MLS Server. Automatically includes any headers returned by the MLS#headers function.

Paramaters
  • url - The url on the server to Post to. This url will automatically be prefixed with "/api". To delete to "/api/accounts" pass "/accounts" as url

  • body - A Ruby object which is converted into JSON and added in the request Body.

  • valid_response_codes - An Array of HTTP response codes that should be considered accepable and not raise exceptions. For example If you don’t want a MLS::Exception::NotFound to be raised when a POST request returns a 404 pass in 404, and the response body will be returned if the status code is a 404 as it does if the status code is in the 200..299 rage. Status codes in the 200..299 range are always considred acceptable

Return Value

Returns the return value of the &block if given, otherwise the response object

Examples:

#!ruby
MLS.delete('/example') # => #<Net::HTTP::Response>

MLS.delete('/example', {:body => 'stuff'}) # => #<Net::HTTP::Response>

MLS.delete('/404') # => raises MLS::Exception::NotFound

MLS.delete('/404', nil, 404, 450..499) # => #<Net::HTTP::Response>

MLS.delete('/404', nil, [404, 450..499]) # => #<Net::HTTP::Response>

MLS.delete('/404', nil, 404) # => #<Net::HTTP::Response>

# this will still raise an exception if the response_code is not valid
# and the block will not be called
MLS.delete('/act') do |response, response_code|
  # ...
end


319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/mls.rb', line 319

def delete(url, body={}, *valid_response_codes, &block)
  body ||= {}
  
  req = Net::HTTP::Delete.new("/api#{url}")
  req.body = Yajl::Encoder.encode(body)
  add_headers(req)
  
  response = connection.request(req)
  handle_response(response, valid_response_codes)
  if block_given?
    yield(response, response.code.to_i)
  else
    response
  end
end