Sha256: 76f2a751b8e3b07bd2c796187a17457bc143a797439d25991c913695f4904d73

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'hpricot'
require 'base64'
require 'tempfile'
require 'time'

module EFax
  class InboundPostStatus
    SUCCESS = 1
  end
  
  class InboundPostRequest
    attr_reader :encoded_file_contents,
                :file_type,
                :ani,
                :account_id,
                :fax_name,
                :csid,
                :status,
                :mcfid,
                :page_count,
                :request_type,
                :date_received,
                :request_date
    
    alias_method :sender_fax_number, :ani
    
    def initialize(xml)
      doc            = Hpricot(xml)
      @encoded_file_contents = doc.at(:filecontents).inner_text
      @file_type     = doc.at(:filetype).inner_text.to_sym
      @ani           = doc.at(:ani).inner_text
      @account_id    = doc.at(:accountid).inner_text
      @fax_name      = doc.at(:faxname).inner_text
      @csid          = doc.at(:csid).inner_text
      @status        = doc.at(:status).inner_text.to_i
      @mcfid         = doc.at(:mcfid).inner_text.to_i
      @page_count    = doc.at(:pagecount).inner_text.to_i
      @request_type  = doc.at(:requesttype).inner_text
      @date_received = Time.parse("#{doc.at(:datereceived).inner_text} -08:00")
      @request_date  = Time.parse("#{doc.at(:requestdate).inner_text} -08:00")
    end
    
    def file_contents
      @file_contents ||= Base64.decode64(encoded_file_contents)
    end
    
    def file
      @file ||= begin
        file = Tempfile.new(fax_name)
        file << file_contents
        file.rewind
        file
      end
    end
    def post_successful_message
      "Post Successful"
    end
    
    def self.receive_by_params(params)
      receive_by_xml(params[:xml] || params["xml"])
    end
  
    def self.receive_by_xml(xml)
      new(xml)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
efax-1.2.0 lib/efax/inbound.rb