Grab specific data from soap API Response using Ruby and Savon

我们两清 提交于 2019-12-24 21:37:03

问题


I am making a soap API call using the Savon Gem in a Rails App - See below:

base_provider.rb:

class Five9Providers::BaseProvider

  def initialize()
    @realm = Base64.strict_encode64("test@test.com:passwordtest")
  end

  def soap_client
    soap_client = Savon.client(
        wsdl: "https://api.five9.com/wsadmin/AdminWebService?wsdl&user=test@test.com",
        namespace: "http://service.admin.ws.five9.com/",
        headers: {"Authorization" =>  "Basic #{@realm}"},
        env_namespace: :soapenv,
        namespace_identifier: :ser,
        logger: Rails.logger,
        ssl_verify_mode: :none
    )

  end

end

record_provider.rb

class Five9Providers::RecordProvider < Five9Providers::BaseProvider

  def add_record_to_list(record)

    if record.primary_language == 'Spanish'
      list_name = "DIS #{record.short_name} #{record.call_type} Spanish"
    else
      list_name = "DIS #{record.short_name} #{record.call_type}"
    end

    soap_client.call :add_record_to_list,
    message: {
      :listName => list_name,
      :listUpdateSettings => {
        :allowDataCleanup => 'true',
        :fieldsMapping => [
          {:columnNumber => '1', :fieldName => 'number1', :key => 'true'},
          {:columnNumber => '2', :fieldName => 'number2', :key => 'false'},
          {:columnNumber => '3', :fieldName => 'number3', :key => 'false'},
          {:columnNumber => '4', :fieldName => 'first_name', :key => 'true'},
          {:columnNumber => '5', :fieldName => 'last_name', :key => 'true'},
          {:columnNumber => '6', :fieldName => 'company', :key => 'false'},
          {:columnNumber => '7', :fieldName => 'street', :key => 'false'},
          {:columnNumber => '8', :fieldName => 'city', :key => 'false'},
          {:columnNumber => '9', :fieldName => 'state', :key => 'false'},
          {:columnNumber => '10', :fieldName => 'zip', :key => 'false'},
          {:columnNumber => '11', :fieldName => 'email', :key => 'true'},
          {:columnNumber => '12', :fieldName => 'branch', :key => 'false'},
          {:columnNumber => '13', :fieldName => 'member ID', :key => 'false'},
          {:columnNumber => '14', :fieldName => 'Unit ID', :key => 'true'},
          {:columnNumber => '15', :fieldName => 'Child Name', :key => 'false'},
          {:columnNumber => '16', :fieldName => 'Child Date of Birth', :key => 'false'},
          {:columnNumber => '17', :fieldName => 'Membership Type', :key => 'false'},
          {:columnNumber => '18', :fieldName => 'Termination Reason', :key => 'false'},
          {:columnNumber => '19', :fieldName => 'ClientID', :key => 'true'},
          {:columnNumber => '20', :fieldName => 'BranchID', :key => 'true'},
          {:columnNumber => '21', :fieldName => 'DateOfBirth', :key => 'false'},
          {:columnNumber => '22', :fieldName => 'ChangeDate', :key => 'false'},
          {:columnNumber => '23', :fieldName => 'CallType', :key => 'true'},
          {:columnNumber => '24', :fieldName => 'Tour', :key => 'false'},
          {:columnNumber => '25', :fieldName => 'Tour Date', :key => 'false'},
          {:columnNumber => '26', :fieldName => 'Lead Source', :key => 'false'},
          {:columnNumber => '27', :fieldName => 'Interviewer', :key => 'false'},
          {:columnNumber => '28', :fieldName => 'Interests', :key => 'false'},
          {:columnNumber => '29', :fieldName => 'Last List Name', :key => 'false'},
          {:columnNumber => '30', :fieldName => 'MemID', :key => 'false'},
          {:columnNumber => '31', :fieldName => 'TerminationDate', :key => 'false'},
          {:columnNumber => '32', :fieldName => 'Visits', :key => 'false'}
        ],

        :cleanListBeforeUpdate => 'false',
        :crmAddMode => 'ADD_NEW',
        :crmUpdateMode => 'UPDATE_FIRST',
        :listAddMode => 'ADD_FIRST'
      },
      :record =>    {
        :fields => [
          record.number1,
          record.number2,
          record.number3,
          record.first_name,
          record.last_name,
          record.company,
          record.street,
          record.city,
          record.state,
          record.zip,
          record.email,
          record.branch,
          record.member_id,
          record.unit_id,
          record.child_name,
          record.child_date_of_birth,
          record.membership_type,
          record.termination_reason,
          record.client_id,
          record.branch_id,
          record.date_of_birth,
          record.change_date,
          record.call_type,
          record.tour,
          record.tour_date,
          record.lead_source,
          record.interviewer,
          record.interests,
          record.last_list_name,
          record.mem_id,
          record.termination_date,
          record.visits
        ]
      }
    }
  end
end

Response I get back, printed to my console:

D, [2013-12-30T16:20:46.107366 #28073] DEBUG -- : HTTPI GET request to api.five9.com (net_http)
SOAP request: https://api.five9.com/wsadmin/AdminWebService
Authorization: Basic test==, SOAPAction: "addRecordToList", Content-Type: text/xml;charset=UTF-8, Content-Length: 5015
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ser="http://service.admin.ws.five9.com/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="http://jaxb.dev.java.net/array" xmlns:ins1="http://service.admin.ws.five9.com/v1/"><soapenv:Body><ser:addRecordToList><listName>DIS Membership NPS 2013</listName><listUpdateSettings><allowDataCleanup>true</allowDataCleanup><fieldsMapping><columnNumber>1</columnNumber><fieldName>number1</fieldName><key>true</key></fieldsMapping><fieldsMapping><columnNumber>2</columnNumber><fieldName>number2</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>3</columnNumber><fieldName>number3</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>4</columnNumber><fieldName>first_name</fieldName><key>true</key></fieldsMapping><fieldsMapping><columnNumber>5</columnNumber><fieldName>last_name</fieldName><key>true</key></fieldsMapping><fieldsMapping><columnNumber>6</columnNumber><fieldName>company</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>7</columnNumber><fieldName>street</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>8</columnNumber><fieldName>city</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>9</columnNumber><fieldName>state</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>10</columnNumber><fieldName>zip</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>11</columnNumber><fieldName>email</fieldName><key>true</key></fieldsMapping><fieldsMapping><columnNumber>12</columnNumber><fieldName>branch</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>13</columnNumber><fieldName>member ID</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>14</columnNumber><fieldName>Unit ID</fieldName><key>true</key></fieldsMapping><fieldsMapping><columnNumber>15</columnNumber><fieldName>Child Name</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>16</columnNumber><fieldName>Child Date of Birth</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>17</columnNumber><fieldName>Membership Type</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>18</columnNumber><fieldName>Termination Reason</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>19</columnNumber><fieldName>ClientID</fieldName><key>true</key></fieldsMapping><fieldsMapping><columnNumber>20</columnNumber><fieldName>BranchID</fieldName><key>true</key></fieldsMapping><fieldsMapping><columnNumber>21</columnNumber><fieldName>DateOfBirth</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>22</columnNumber><fieldName>ChangeDate</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>23</columnNumber><fieldName>CallType</fieldName><key>true</key></fieldsMapping><fieldsMapping><columnNumber>24</columnNumber><fieldName>Tour</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>25</columnNumber><fieldName>Tour Date</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>26</columnNumber><fieldName>Lead Source</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>27</columnNumber><fieldName>Interviewer</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>28</columnNumber><fieldName>Interests</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>29</columnNumber><fieldName>Last List Name</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>30</columnNumber><fieldName>MemID</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>31</columnNumber><fieldName>TerminationDate</fieldName><key>false</key></fieldsMapping><fieldsMapping><columnNumber>32</columnNumber><fieldName>Visits</fieldName><key>false</key></fieldsMapping><cleanListBeforeUpdate>false</cleanListBeforeUpdate><crmAddMode>ADD_NEW</crmAddMode><crmUpdateMode>UPDATE_FIRST</crmUpdateMode><listAddMode>ADD_FIRST</listAddMode></listUpdateSettings><record><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields>2021</fields><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/><fields xsi:nil="true"/></record></ser:addRecordToList></soapenv:Body></soapenv:Envelope>
D, [2013-12-30T16:20:49.046215 #28073] DEBUG -- : HTTPI POST request to api.five9.com (net_http)
SOAP response (status 200)
<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'><env:Header></env:Header><env:Body><ns2:addRecordToListResponse xmlns:ns2="http://service.admin.ws.five9.com/" xmlns:ns3="http://service.admin.ws.five9.com/v1/"><return><failureMessage></failureMessage><uploadDuplicatesCount>0</uploadDuplicatesCount><uploadErrorsCount>0</uploadErrorsCount><warningsCount/><callNowQueued>0</callNowQueued><crmRecordsInserted>1</crmRecordsInserted><crmRecordsUpdated>0</crmRecordsUpdated><listName>DIS Membership NPS 2013</listName><listRecordsDeleted>0</listRecordsDeleted><listRecordsInserted>1</listRecordsInserted></return></ns2:addRecordToListResponse></env:Body></env:Envelope>

My question here is two fold:

  1. Why is this printing to STDOUT when in my base_provider I have the logger set to to Rails.logger?

  2. How can I capture the response, specifically the envelope portion following the response code of 200 and save the value 1 in <crmRecordsInserted>1</crmRecordsInserted>. I think this is really puzzling me because the response isn't in a variable that I can access.


回答1:


I have found an answer for the second question, and a work-around for the first:

By setting the entire soap_client.call block equal to a variable, you can access whatever part of the response you want as detailed in the Savon Docs:

http://savonrb.com/version2/client.html

As a work-around for the first question, since you then have a variable that you can do what you want to with it is easy to log it using Rails.logger.



来源:https://stackoverflow.com/questions/20858938/grab-specific-data-from-soap-api-response-using-ruby-and-savon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!