Method: MLS#put

Defined in:
lib/mls.rb

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

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

Paramaters
  • url - The url on the server to Put to. This url will automatically be prefixed with "/api". To put 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 PUT 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.put('/example') # => #<Net::HTTP::Response>

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

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

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

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

MLS.put('/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.put('/act') do |response, response_code|
  # ...
end


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/mls.rb', line 201

def put(url, body={}, *valid_response_codes, &block)
  body ||= {}
  
  req = Net::HTTP::Put.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