Class: MWS::FBA::ShippingLabel

Inherits:
Object
  • Object
show all
Defined in:
lib/mws/fba_label.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ShippingLabel

Returns a new instance of ShippingLabel.



27
28
29
30
31
32
33
34
35
# File 'lib/mws/fba_label.rb', line 27

def initialize(args={})
  args.each { |k, v| send("#{k}=", v) }

  AWS::S3::Base.establish_connection!(
    :access_key_id => @access_key_id,
    :secret_access_key => @secret_access_key
  )
  upload
end

Instance Attribute Details

#access_key_idObject

Returns the value of attribute access_key_id.



25
26
27
# File 'lib/mws/fba_label.rb', line 25

def access_key_id
  @access_key_id
end

#bucketObject

Returns the value of attribute bucket.



25
26
27
# File 'lib/mws/fba_label.rb', line 25

def bucket
  @bucket
end

#secret_access_keyObject

Returns the value of attribute secret_access_key.



25
26
27
# File 'lib/mws/fba_label.rb', line 25

def secret_access_key
  @secret_access_key
end

#ship_from_addressObject

Returns the value of attribute ship_from_address.



25
26
27
# File 'lib/mws/fba_label.rb', line 25

def ship_from_address
  @ship_from_address
end

#ship_to_addressObject

Returns the value of attribute ship_to_address.



25
26
27
# File 'lib/mws/fba_label.rb', line 25

def ship_to_address
  @ship_to_address
end

#shipment_idObject

Returns the value of attribute shipment_id.



25
26
27
# File 'lib/mws/fba_label.rb', line 25

def shipment_id
  @shipment_id
end

#template_urlObject

Returns the value of attribute template_url.



25
26
27
# File 'lib/mws/fba_label.rb', line 25

def template_url
  @template_url
end

#urlObject

Returns the value of attribute url.



25
26
27
# File 'lib/mws/fba_label.rb', line 25

def url
  @url
end

Instance Method Details

#build_pdfObject

Build PDF document and return Prawn::Document, ready to be saved locally or uploaded to S3



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mws/fba_label.rb', line 40

def build_pdf
  Prawn::Document.new(:template => open(@template_url)) do |pdf|
    
    common_options = {
      :width => 200,
      :height => 50,
      :align => :left,
      :valign => :top,
      :overflow => :shrink_to_fit,
    }
    # print from address
    pdf.font "Helvetica", :size => 8
    pdf.text_box(@ship_from_address, common_options.merge({:at => [375,498]}))

    # print to address
    pdf.font "Helvetica", :style => :bold, :size => 11
    pdf.text_box(@ship_to_address, common_options.merge({:at => [540,498], :height => 40}))

    # print shipment ID
    pdf.font "Helvetica", :style => :bold, :size => 11
    puts pdf.text_box(@shipment_id, common_options.merge({
      :width    => 170,
      :height   => 16,
      :align    => :center,
      :at       => [455,374],
    }))

    # Generate barcode, store on s3, insert into pdf from S3
    barcode_png = Barby::Code128A.new(@shipment_id).to_png(:height => 50, :margin => 0)
    barcode_filename = "barcode_#{@shipment_id}.png"
    AWS::S3::S3Object.store(barcode_filename, barcode_png, @bucket)
    barcode_url = AWS::S3::S3Object.url_for(barcode_filename, @bucket, :expires_in => 3600)
    pdf.image open(barcode_url), :at => [470,430]
  end

end

#uploadObject



77
78
79
80
81
# File 'lib/mws/fba_label.rb', line 77

def upload
  filename = "FBA_label_#{@shipment_id}.pdf"
  AWS::S3::S3Object.store(filename, build_pdf.render, @bucket)
  @url = AWS::S3::S3Object.url_for(filename, @bucket, :expires_in => 3600)
end