Updating an Entity Using REST (Ruby)

[This document supports a preliminary release of a software product that may be changed substantially prior to final commercial release. This document is provided for informational purposes only.]

As described in Updating an Entity Using REST, this topic provides a Ruby sample to update an entity. You will need to update the code and provide the following:

  • Your own credentials
  • Your existing authority id, container id and entity id (to update)
  • Provide proxy information

The sample does the following:

  • Retrieves the entity and loads it into an XML Document object.
  • Updates the XML by adding a new property (for illustration, it adds a new <year/> element).
  • Sends the update back to the server.
require 'base64' 
require 'bigdecimal'
require 'net/https'
require 'openssl'
require 'rexml/document'
require 'uri'

# Provide your own credentials, your existing authority id, container id and Entity id(to delete).
USER_NAME = '<UserName>'
PASSWORD = '<Password>'
AUTHORITY_ID = '<YourExistingAuthorityId>'
CONTAINER_ID = '<YourExistingContainerId>'
ENTITY_ID = '<YourExistingEntityId>'

# provide your proxy information
PROXY_ADDRESS = nil 
PROXY_PORT = nil
PROXY_USER = nil 
PROXY_PASSWORD = nil 

SVC_ENDPOINT = 'data.database.windows.net'
SVC_VERSION = 'v1'
  
# setup credentials
Credentials = Struct.new("Credentials", :un, :pw)
myCred = Credentials.new(USER_NAME, PASSWORD)

#setup proxy information -- if you don't have to use a proxy you can set everything to nil
Proxy_info = Struct.new("Proxy_info", :addr, :port, :user, :pw)
myProxy = Proxy_info.new(PROXY_ADDRESS, PROXY_PORT, PROXY_USER, PROXY_PASSWORD)


authorityUri  = AUTHORITY_ID +  '.' + SVC_ENDPOINT

SSDS = Net::HTTP::Proxy(myProxy.addr, myProxy.port, myProxy.user, myProxy.pw).new(authorityUri, 443) 
SSDS.use_ssl=true
SSDS.verify_mode=OpenSSL::SSL::VERIFY_NONE
SSDS.start()


reqPath= '/' + SVC_VERSION + '/' + CONTAINER_ID + '/' +  ENTITY_ID
myReq = Net::HTTP::Get.new(reqPath)       
myReq['Content-Type'] = 'application/x-ssds+xml'
myReq.basic_auth(myCred.un,  myCred.pw)
response = SSDS.request(myReq)
method = 'GET'
case response
  when Net::HTTPSuccess     then 
    error = false
  when Net::HTTPNotFound then 
    puts "Entity does not exist"
    error = true
  when Net::HTTPForbidden then
    puts "SSDS Access denied"
    error = true
  when Net::HTTPBadRequest then
    puts "Request is not valid"
    error = true
  else
    puts "Unexpected Error"
    error = true
  end
  if (error)
    xml = REXML::Document.new(response.body)
    puts "SSDS #{method} Error: #{xml.root().elements[1].name} =>  #{xml.root().elements[1].text}"
    puts "SSDS #{method} Error: #{xml.root().elements[2].name} =>  #{xml.root().elements[2].text}"
    raise ArgumentError,ENTITY_ID + ' Entity does not exist'

  end
  
reqXml = REXML::Document.new(response.body)

reqXml.root().add_element('year')
reqXml.root().elements['year'].add_attribute('xsi:type', 'x:string')
reqXml.root().elements['year'].text = '2005'

myReq = Net::HTTP::Put.new(reqPath)       
myReq['Content-Type'] = 'application/x-ssds+xml'
myReq['Content-Length'] = reqXml.to_s.size.to_s
myReq.basic_auth(myCred.un,  myCred.pw)
response = SSDS.request(myReq, reqXml.to_s)

method = 'PUT'
case response
  when Net::HTTPSuccess     then 
    puts ENTITY_ID + ' updated'
    error = false
  when Net::HTTPForbidden then
    puts "SSDS Access denied"
    error = true
  when Net::HTTPBadRequest then
    puts "Request is not valid"
    error = true
  when Net::HTTPConflict   then
    puts "Entity already exists"
    error = true
  else
    puts "Unexpected Error"
    error = true
  end
  if (error)
  xml = REXML::Document.new(response.body)
  puts "SSDS #{method} Error: #{xml.root().elements[1].name} =>  #{xml.root().elements[1].text}"
  puts "SSDS #{method} Error: #{xml.root().elements[2].name} =>  #{xml.root().elements[2].text}"
end

See Also

Concepts

Updating an Entity Using REST
Examples of Using SOAP and REST Interfaces with the SQL Data Services