Class: Fedex::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/fedex/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shipment_details = {}) ⇒ Document

Initialize Fedex::Document Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fedex/document.rb', line 7

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

Instance Attribute Details

#filenamesObject (readonly)

Returns the value of attribute filenames


3
4
5
# File 'lib/fedex/document.rb', line 3

def filenames
  @filenames
end

#response_detailsObject (readonly)

Returns the value of attribute response_details


3
4
5
# File 'lib/fedex/document.rb', line 3

def response_details
  @response_details
end

#tracking_numberObject (readonly)

Returns the value of attribute tracking_number


3
4
5
# File 'lib/fedex/document.rb', line 3

def tracking_number
  @tracking_number
end

Instance Method Details

#has_image?(content) ⇒ Boolean


46
47
48
# File 'lib/fedex/document.rb', line 46

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

#save(path, content) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fedex/document.rb', line 34

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