Class: MWS::PackingSlip

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ PackingSlip

Returns a new instance of PackingSlip.



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

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.



24
25
26
# File 'lib/mws/packing_slip.rb', line 24

def access_key_id
  @access_key_id
end

#bucketObject

Returns the value of attribute bucket.



24
25
26
# File 'lib/mws/packing_slip.rb', line 24

def bucket
  @bucket
end

#secret_access_keyObject

Returns the value of attribute secret_access_key.



24
25
26
# File 'lib/mws/packing_slip.rb', line 24

def secret_access_key
  @secret_access_key
end

#ship_from_addressObject

Returns the value of attribute ship_from_address.



24
25
26
# File 'lib/mws/packing_slip.rb', line 24

def ship_from_address
  @ship_from_address
end

#ship_to_addressObject

Returns the value of attribute ship_to_address.



24
25
26
# File 'lib/mws/packing_slip.rb', line 24

def ship_to_address
  @ship_to_address
end

#shipment_idObject

Returns the value of attribute shipment_id.



24
25
26
# File 'lib/mws/packing_slip.rb', line 24

def shipment_id
  @shipment_id
end

#template_urlObject

Returns the value of attribute template_url.



24
25
26
# File 'lib/mws/packing_slip.rb', line 24

def template_url
  @template_url
end

#urlObject

Returns the value of attribute url.



24
25
26
# File 'lib/mws/packing_slip.rb', line 24

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
# File 'lib/mws/packing_slip.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
    puts pdf.text_box(@ship_from_address, common_options.merge({:at => [95,392]}))
    
    # print to address
    pdf.text_box(@ship_to_address, common_options.merge({:at => [308,392]}))
    
    # print shipment ID
    pdf.font "Helvetica", :style => :bold, :size => 16
    pdf.text_box(@shipment_id, common_options.merge({
      :width    => 170,
      :height   => 16,
      :align    => :center,
      :at       => [185,250],
    }))
    
    # Generate barcode, store on s3, insert into pdf from S3
    barcode_png = Barby::Code128A.new(@shipment_id).to_png(:height => 60, :width => 142, :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 => [200,315]
  end

end

#uploadObject

Upload packing slip to S3



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

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