Sha256: 94e1adbd248742bf3240ca3c57502e87c43aadaab693d2f57392528b9314b97f

Contents?: true

Size: 1.45 KB

Versions: 12

Compression:

Stored size: 1.45 KB

Contents

module Fedex
  class Document
    attr_reader :tracking_number, :filenames, :response_details

    # Initialize Fedex::Document Object
    # @param [Hash] options
    def initialize(shipment_details = {})
      @response_details = shipment_details[:process_shipment_reply]
      @filenames = shipment_details[:filenames]

      # extract label and tracking number
      package_details = @response_details[:completed_shipment_detail][:completed_package_details]
      label = package_details[:label]
      @tracking_number = package_details[:tracking_ids][:tracking_number]

      # extract shipment documents
      shipment_documents = @response_details[:completed_shipment_detail][:shipment_documents] || []

      # unify iteration interface
      unless shipment_documents.kind_of?(Array)
        shipment_documents = [shipment_documents]
      end

      # keeps the filenames which actually saved
      save(@filenames[:label], label)

      # save shipment documents
      shipment_documents.each do |doc|
        doc_type = doc[:type].downcase.to_sym
        save(@filenames[doc_type], doc)
      end
    end

    def save(path, content)
      return unless path && has_image?(content)

      image = Base64.decode64(content[:parts][:image])
      full_path = Pathname.new(path)
      File.open(full_path, 'wb') do|f|
        f.write(image)
      end

      full_path
    end

    def has_image?(content)
      content[:parts] && content[:parts][:image]
    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
fedex-3.10.11 lib/fedex/document.rb
fedex-3.10.1 lib/fedex/document.rb
fedex-3.10.0 lib/fedex/document.rb
fedex-3.9.1 lib/fedex/document.rb
fedex-3.9.0 lib/fedex/document.rb
fedex-3.8.0 lib/fedex/document.rb
fedex-3.6.1 lib/fedex/document.rb
fedex-3.6.0 lib/fedex/document.rb
fedex-3.4.0 lib/fedex/document.rb
fedex-3.3.1 lib/fedex/document.rb
fedex-3.3.0 lib/fedex/document.rb
fedex-3.1.0 lib/fedex/document.rb