Class: Fedex::Label

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label_details = {}, associated_shipments = false) ⇒ Label

Initialize Fedex::Label Object

Parameters:

  • options (Hash)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fedex/label.rb', line 10

def initialize(label_details = {}, associated_shipments = false)
  if associated_shipments
    package_details = label_details
    @options = package_details[:label]
    @options[:tracking_number] = package_details[:tracking_id]
  else
    @response_details = label_details[:process_shipment_reply]
    package_details = label_details[:process_shipment_reply][:completed_shipment_detail][:completed_package_details]
    @options = package_details[:label]
    @options[:tracking_number] = package_details[:tracking_ids][:tracking_number]
  end
  @options[:format] = label_details[:format]
  @options[:file_name] = label_details[:file_name]
  @image = Base64.decode64(options[:parts][:image]) if has_image?

  if file_name = @options[:file_name]
    save(file_name, false)
  end
end

Instance Attribute Details

#imageObject

Returns the value of attribute image.



6
7
8
# File 'lib/fedex/label.rb', line 6

def image
  @image
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/fedex/label.rb', line 6

def options
  @options
end

#response_detailsObject

Returns the value of attribute response_details.



6
7
8
# File 'lib/fedex/label.rb', line 6

def response_details
  @response_details
end

Instance Method Details

#associated_shipmentsObject



61
62
63
64
65
66
67
68
69
# File 'lib/fedex/label.rb', line 61

def associated_shipments
  if (label_details = @response_details[:completed_shipment_detail][:associated_shipments])
    label_details[:format] = format
    label_details[:file_name] = file_name
    Label.new(label_details, true)
  else
    nil
  end
end

#file_nameObject



38
39
40
# File 'lib/fedex/label.rb', line 38

def file_name
  options[:file_name]
end

#formatObject



34
35
36
# File 'lib/fedex/label.rb', line 34

def format
  options[:format]
end

#has_image?Boolean

Returns:

  • (Boolean)


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

def has_image?
  options[:parts] && options[:parts][:image]
end

#nameObject



30
31
32
# File 'lib/fedex/label.rb', line 30

def name
  [tracking_number, format].join('.')
end

#save(path, append_name = true) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/fedex/label.rb', line 50

def save(path, append_name = true)
  return unless has_image?

  full_path = Pathname.new(path)
  full_path = full_path.join(name) if append_name

  File.open(full_path, 'wb') do|f|
    f.write(@image)
  end
end

#tracking_numberObject



42
43
44
# File 'lib/fedex/label.rb', line 42

def tracking_number
  options[:tracking_number]
end