SOAP with Ruby

SOAP (Simple Object Access Protocol) is a protocol specification for exchanging structured information in the implementation of web services in computer networks. Its purpose is to induce extensibility, neutrality and independence. It uses XML Information Set for its message format, and relies on application layer protocols, most often Hypertext Transfer Protocol (HTTP) or Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission. – Wikipedia

How to make SOAP calls in ruby

One of the best ways to make SOAP calls will be using the library Savon. Its very easy to get started.

If your service provider has WSDL (Web Services Description Language) document. You can also host the WSDL file in your local machine or deploy along with the source code. Here is sample WSDL file if you wanna have a look. Basically this document consists of all the operations supported, format of the response, the endpoint URL of the services and other details. 

Savon can parse the details and have ready in your app as ruby methods. Savon can tell you about the available operations.

client.operations  # => [:authenticate, :find_user]

Note: if your WSDL has operations like ‘CarSpecificationsResult’ then Savon will by default convert it to ‘carSpecificationsResult’ i.e. camelCase. So, if you provide the the key like

, 'RegistrationNumber':  'ZRF-882'

It will convert it to ‘registrationNumber’ and since such key/attribute is not supported by your service provider, you are not going to get the expected response.

Solution: Put off the conversion

globals.convert_request_keys_to :none

Example

This is an example of Vehicle Registration API provided by http://www.regcheck.org.uk/ or http://rekisterinumeroapi.com/. FYI, for SOAP calls to these services, you don’t need to send passwords, just username will work.

@client = Savon::client do |globals|
  globals.wsdl 'http://www.regcheck.org.uk/api/reg.asmx?wsdl'
  globals.convert_request_keys_to :none # or one of [:lower_camelcase, :upcase, :none]
end

response = @client.call(:check_finland, message: { username: ENV['rekisterinumeroapi_username'], 'RegistrationNumber':  'ZRF-882' })

Following is the segment of the WSDL document, where its mentioned that which messages are valid for the operation “check_finland“. Keep in mind that Savon converts check_finland back to CheckFinland during request.


To read data from response use the following methods,

response.header  
# => { token: "secret" }
response.body  
# => {:check_finland_response=>
       {:check_finland_result=>
         {:vehicle_json=> "{\"ABICode\":\"\",\" ............
response.hash  
# => { envelope: { header: { ... }, body: { ... } } }
response.to_xml  
# => "trueluke"

See this for more on this http://savonrb.com/version2/response.html

Regarding Vehicle Reg API – Finland

Regarding Finland, you only get these information

  • Make and Model
  • Year of Registration
  • VIN number
  • Insurer
  • Tow Bar

Sample Registration Number:
ZRF-882

Sample Json:
{
    "ABICode":                     "",
    "Description":                 "Citroen BERLINGO",
    "RegistrationYear":            "2012",
    "CarMake":                     { "CurrentTextValue": "Citroen" },
    "CarModel":                    { "CurrentTextValue": "BERLINGO" },
    "EngineSize":                  { "CurrentTextValue": "" },
    "FuelType":                    { "CurrentTextValue": "" },
    "MakeDescription":             { "CurrentTextValue": "Citroen" },
    "ModelDescription":            { "CurrentTextValue": "BERLINGO" },
    "Immobiliser":                 { "CurrentTextValue": "" },
    "NumberOfSeats":               { "CurrentTextValue": "" },
    "IndicativeValue":             { "CurrentTextValue": "" },
    "DriverSide":                  { "CurrentTextValue": "" },
    "BodyStyle":                   { "CurrentTextValue": "" },
    "InsuranceCompany":            "OP Vakuutus",
    "VechileIdentificationNumber": "VF77J9HP8CJ606306",
    "TowBar":                      true
}

Leave a comment