2014-06-16 44 views
0

要求:響應來自API的「transaction_id」和上傳參數時,我們還需要傳遞pdf文件有了這個API,但是沒有任何參數需要用API發送文件。因此我們在模型中以這種方式做了multipart/form-data到http請求。如何將pdf文件和SOAP API一起上載爲rails中的多部分/表單數據文件4+ savon

http://xyz.xyz.com/xyz/upload.ashx?u=username&p=passwordtid=ticketGuid

用戶名 「XXXXXXX」 密碼 「YYYYYY」 TID 「f155a1e5-d1cd-4edb-8d25-89f3852a06f4」 需要

響應是TRANSID(十進制)

我們型號代碼如下,

要求「net/http」

類工作<的ActiveRecord :: Base的 after_update:upload_ticket_file

has_attached_file :ad_pdf 
validates_attachment_content_type :ad_pdf, :content_type => "application/pdf" 
validates_attachment :ad_pdf, :presence => true 

validates :section_id, :section, :colour_id, :colour, :size_id, :sizes, :height, :width, :height_bleed, :width_bleed, :booking_number, :presence => true 

def generate_xml_ticket 
    "<?xml version='1.0' encoding='UTF-8'?><env:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ads='http://adstream.com/' xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'><env:Body><ads:AddTicketToPool><ads:XMLTicket><![CDATA[<?xml version='1.0' encoding='utf-8'?><jobbag><jobentry><colour_name /><page_setup><bleed_depth>#{(self.height_bleed.to_f*2.83).round(2)}</bleed_depth><bleed_width>#{(self.width_bleed.to_f*2.83).round(2)}</bleed_width><trim_depth>#{(self.height.to_f*2.83).round(2)}</trim_depth><trim_width>#{(self.width.to_f*2.83).round(2)}</trim_width></page_setup><named_size>#{self.sizes.split('/').last}</named_size><number_of_columns>#{self.size_number_of_columns}</number_of_columns><publication_code>#{self.publication_code}</publication_code><publisher_code>#{PUBLISHER[:code]}</publisher_code><section_code>#{self.section.split('/')[1]}</section_code><style>3</style><attention/><instructions /><booking_number pscode='BkNo' psformat='pZ1F' pstitle='Booking #' pstype='1'>#{self.booking_number}</booking_number><publication_date pscode='RnDt' psformat='yZ00' pstitle='Publicaiton Date' pstype='1'>#{self.issue_date.strftime("%d-%b-%Y")}</publication_date><caption /><insertion_number/><production_key /><customer_name pscode='ACNm' psformat='sZ00' pstitle='Ad Client Name' pstype='1'>#{PUBLISHER[:company_name].to_s}</customer_name><any pscode='APro' psformat='NZ00' pstitle='Brand' pstype='1'>#{self.brand}</any><any pscode='Camp' psformat='nZ00' pstitle='Campaign' pstype='1'>#{self.campaign}</any><any pscode='Rpla' psformat='bP00' pstitle='Replacement Material' pstype='1'>false</any><any pscode='Hard' psformat='BP00' pstitle='Hard Proof Sent' pstype='1'>false</any></jobentry><job_common><operator>#{self.attention_to}</operator><file_name>#{self.ad_pdf_file_name}</file_name></job_common></jobbag>]]></ads:XMLTicket></ads:AddTicketToPool></env:Body></env:Envelope>" 
end 

def upload_ticket_file 
    # response = system("curl -v -F [email protected]#{self.ad_pdf.path} -F message='#{self.ad_pdf_file_name}' '#{ADSTREAM_QPWEB_UPLOAD_URL}?u=#{ADSTREAM_QPWEB_USERNAME}&p=#{ADSTREAM_QPWEB_PASSWORD}&tid=#{self.ticket_guid}'" 
    # uri = URI.parse(ADSTREAM_QPWEB_UPLOAD_URL+"?u=#{ADSTREAM_QPWEB_USERNAME_ENCODED}&p=#{ADSTREAM_QPWEB_PASSWORD}&tid=#{self.ticket_guid}") 
    boundary = "ABASDASDSAKJDFKDFNDKFDKFJDKNDFDN" 
    uri = URI.parse(ADSTREAM_QPWEB_UPLOAD_URL+"?u=#{ADSTREAM_QPWEB_USERNAME_ENCODED}&p=#{ADSTREAM_QPWEB_PASSWORD}&tid=#{self.ticket_guid}") 
    puts ">>>>>>>>>>>>>>>>>>>>>>>>>" 
    puts ADSTREAM_QPWEB_UPLOAD_URL+"?u=#{ADSTREAM_QPWEB_USERNAME_ENCODED}&p=#{ADSTREAM_QPWEB_PASSWORD}&tid=#{self.ticket_guid}" 
    file = self.ad_pdf.path 
    post_body = [] 
    post_body << "Content-Disposition: form-data; name='datafile'; filename='#{self.ad_pdf_file_name}'" 
    post_body << "Content-Type: application/pdf" 
    post_body << File.read(file)+boundary 
    http = Net::HTTP.new(uri.host, uri.port) 
    request = Net::HTTP::Post.new(uri.request_uri) 
    request.body = post_body.join 
    request["Content-Type"] = "multipart/form-data" 
    request.add_field('session', boundary) 
    response = http.request(request) 
    puts request.inspect 
    puts response.inspect 
    if response and (response.body != "ticketnotaddedtostore" and !response.body.to_s.blank? and response.body != "incorrectquerystrings") 
     if self.update_attribute(:transaction_id, response.body)   
      client = Savon.client(namespace_identifier: :ads) do 
       wsdl ADSTREAM_QPWEB_URL 
       log true 
      end 
      message = {TransID: self.transaction_id} 
      response = client.call(:check_status) do 
       message (message) 
      end 
      self.update_attribute(:check_status_result, response.body.to_hash[:check_status_response][:check_status_result]) 
     end 
    end 
end 

類JobXMLTicket

def to_s 
    builder = Builder::XmlMarkup.new 
    builder.instruct!(:xml, encoding: "UTF-8") 

    builder.jobbag { |jo| 
     jo.jobentry { |je| 
      je.colour_name 
      je.page_setup {|ps| 
       ps.bleed_depth "0.0" 
       ps.bleed_width "0.0" 
       ps.trim_depth "725.66" 
       ps.trim_width "1150.86" 
      } 
     } 
    } 
    builder 
end 

回答

0

使用 'RESTClient實現寶石',我們可以做要緊

require "net/http" 

require 'savon' 

require 'rest_client' 

$config = { 
    qpweb_upload_url: 'http://example.com/WebAPI/upload.ashx' 
} 


class Job < ActiveRecord::Base 
    def uploadPdf 
    q = URI.encode_www_form({ 
     :u => USERNAME, 
     :p => PASSWORD, 
     :tid => ticket_guid 
     }) 
    response = RestClient.post($config[:qpweb_upload_url] + '?' + q, :file =>File.open(self.ad_pdf.path, 'rb')) 
    id = /transid=(.*)/.match(response.to_str) 
    raise 'error uploading file' if !id 
    puts 'TransactionId: ', id[1] 
    return id[1] 
    end 
end 
相關問題