ruby 调用savon接口示例
程序员文章站
2022-03-09 23:13:21
...
例子一
例子二
module Api module AobotQuotation def self.query(body = {}) begin quotation_client = Savon.client(wsdl: "#{Settings.robot_quotation_url}/QuotePriceService.svc?singleWsdl", log: false, pretty_print_xml: false) # 0:ok,有结果,-1:未找到合成路径,1:未接收到报价商品信息,2:JCHEM服务异常,3:WCF服务异常 response = quotation_client.call(:get_one_step_price, message: {info: body.to_json}) result = JSON.parse(response.body[:get_one_step_price_response][:get_one_step_price_result]) return result rescue return {'Result' => {'ErrCode' => 3}} end end def self.artificial_quotes(body = {}) begin client = Savon.client(wsdl: "#{Settings.robot_quotation_url}/QuotePriceService.svc?singleWsdl", log: false, pretty_print_xml: false) # 0:ok,有结果,-1:未找到合成路径,1:未接收到报价商品信息,2:JCHEM服务异常,3:WCF服务异常 response = client.call(:artificial_quotes, message: body) return response.body[:artificial_quotes_response][:artificial_quotes_result] rescue return '接口异常。' end end end end -------- module Api module Aide def self.query(cas_number: "", product_num: "", ak_num: "") client = Savon.client(wsdl: "#{Settings.bide_quotation_url}?wsdl", log: false, pretty_print_xml: false) body = {"CASNumber" => cas_number, "ProductNum" => product_num, "AKNum" => ak_num, "client_id" => Settings.bide_client_id, "client_secret" => Settings.bide_client_secret} response = client.call(:get_stock_price_purity_data_cn, message: body.as_json) result = JSON.parse(response.body[:get_stock_price_purity_data_cn_response][:get_stock_price_purity_data_cn_result]) end def self.get_prices(cas) result = query(cas_number: cas) prices = [] if result['ret'].to_i == 0 result['rows'].each do |row| next if row['Price'].to_f <= 0 package = row['PackSize'].to_f unit = row['PackSize'].to_s.gsub(/^\d+/, '').downcase next if package <= 0 || !['g', 'mg', 'kg', 't', 'ml', 'l'].include?(unit) next if OrderDetail.new.convert_unit(package, unit, 'g').to_f > row['Weight'].to_f prices << {package: package, unit: QuotationDetail.package_units[unit], price: row['Price'].to_f, art_no: row['ProductNum'], purity: row['Purity']} end end prices end end end
例子二
# Soap gem 'soap4r' gem 'savon'
def download_msds order_item = OrderItem.find(params[:order_item_id]) msds = Msds.new(order_item) result, flag = msds.generate send_data msds.render, filename: 'iChemical' << (flag ? '' : '-GT') << '-MSDS-' << order_item.order_item_no << '.pdf',type: "application/pdf", disposition: "attachment" end
class Msds def initialize(order_item) super() @order_item = order_item @chemical = order_item.chemical @result = nil end def client client = Savon.client do wsdl MSETTING["wsdl_url"] ssl_verify_mode :none log true end end def generate if @order_item.msds_record.present? @result, flag = complete_modify_msds(@order_item.msds_record) else if call_msds[0].nil? @result, flag = call_fixed_msds else @result, flag = call_msds end end end def call_msds response = client.call(:generate_msds, message: { cas: @chemical.cas, catalogueNo: @chemical.catalog_no }) [response.body[:generate_msds_response][:generate_msds_result], true] end def call_fixed_msds response = client.call(:generate_fixed_msds, message: { cas: @chemical.cas, catalogueNo: @chemical.catalog_no }) [response.body[:generate_fixed_msds_response][:generate_fixed_msds_result], false] end def request_modify_msds response = client.call(:request_modify_msds, message: { cas: @chemical.cas }) response.body[:request_modify_msds_response][:request_modify_msds_result][:string] end def complete_modify_msds(msds) response = client.call(:complete_modify_msds, message: { cas: @chemical.cas, catalogueNo: @chemical.catalog_no, name: msds.name, hazardsIdentification: msds.hazardsIdentification,other: msds.other, purity: msds.purity}) [response.body[:complete_modify_msds_response][:complete_modify_msds_result], true] end def render Base64.strict_decode64(@result).force_encoding('UTF-8') end end