Class: Brownie::Shipment

Inherits:
Object
  • Object
show all
Defined in:
lib/brownie/shipment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shipment = nil) ⇒ Shipment

Returns a new instance of Shipment.



13
14
15
16
17
18
19
20
21
22
# File 'lib/brownie/shipment.rb', line 13

def initialize(shipment=nil)
	    self.shipment_data = shipment if !shipment.nil?
       self.credentials = Credentials.new
       self.shipper = Shipper.new
       self.ship_to = ShipTo.new
       self.ship_from = ShipFrom.new
       self.package = Package.new
       self.template = Common::template_to_hash(:ship_confirm)
       
end

Instance Attribute Details

#account_numberObject

Returns the value of attribute account_number.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def 
  @account_number
end

#api_domainObject

Returns the value of attribute api_domain.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def api_domain
  @api_domain
end

#credentialsObject

Returns the value of attribute credentials.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def credentials
  @credentials
end

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def data
  @data
end

#environmentObject

Returns the value of attribute environment.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def environment
  @environment
end

#errorsObject

Returns the value of attribute errors.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def errors
  @errors
end

#label_binaryObject

Returns the value of attribute label_binary.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def label_binary
  @label_binary
end

#packageObject

Returns the value of attribute package.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def package
  @package
end

#requestObject

Returns the value of attribute request.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def request
  @request
end

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def response
  @response
end

#service_codeObject

Returns the value of attribute service_code.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def service_code
  @service_code
end

#ship_fromObject

Returns the value of attribute ship_from.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def ship_from
  @ship_from
end

#ship_toObject

Returns the value of attribute ship_to.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def ship_to
  @ship_to
end

#shipment_digestObject

Returns the value of attribute shipment_digest.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def shipment_digest
  @shipment_digest
end

#shipment_xmlObject

Returns the value of attribute shipment_xml.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def shipment_xml
  @shipment_xml
end

#shipperObject

Returns the value of attribute shipper.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def shipper
  @shipper
end

#templateObject

Returns the value of attribute template.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def template
  @template
end

#tracking_numberObject

Returns the value of attribute tracking_number.



8
9
10
# File 'lib/brownie/shipment.rb', line 8

def tracking_number
  @tracking_number
end

Instance Method Details

#confirmObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/brownie/shipment.rb', line 26

def confirm
   
   data = self.template["Shipment"]
   data["PaymentInformation"]["Prepaid"]["BillShipper"]["AccountNumber"] = self.
   data["Service"]["Code"] = self.service_code 
   data["Package"] = self.package.create 
   data["Shipper"] = self.shipper.create
   data["ShipTo"] = self.ship_to.create
   data["ShipFrom"] = self.ship_from.create
  
   self.template["Shipment"] = data
   request_template = {"ShipmentConfirmRequest" => self.template}
shipment_xml_str = request_template["ShipmentConfirmRequest"].to_xml(:root => "ShipmentConfirmRequest")
 
	self.response = RestClient.post("https://#{Common::domain}/ups.app/xml/ShipConfirm","#{access_request_header}#{shipment_xml_str}")
	if self.response.code.eql?(200)
		response_hash = Hash.from_xml(response)
          if response_hash["ShipmentConfirmResponse"]["Response"]["ResponseStatusCode"].eql?("0")
             self.errors = response_hash["ShipmentConfirmResponse"]["Response"]["Error"]
             return false
          end

		self.tracking_number = response_hash["ShipmentConfirmResponse"]["ShipmentIdentificationNumber"]
		self.shipment_digest = response_hash["ShipmentConfirmResponse"]["ShipmentDigest"]
		return true

	else 
		raise "In-valid request #{response}"
	end
end

#label(_shipment_digest, path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/brownie/shipment.rb', line 57

def label(_shipment_digest,path)
   shipment_digest = _shipment_digest.nil? ? @shipment_digest : _shipment_digest
    label_xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
       <ShipmentAcceptRequest>
       <Request>
       <TransactionReference>
       <CustomerContext></CustomerContext>
       </TransactionReference>
       <RequestAction>ShipAccept</RequestAction>
       <RequestOption>1</RequestOption>
       </Request>
       <ShipmentDigest>' + shipment_digest +  '</ShipmentDigest>
       </ShipmentAcceptRequest>'


   

   label_response = RestClient.post("https://#{Common::domain}/ups.app/xml/ShipAccept","#{access_request_header}#{label_xml}")
   label_hash = Hash.from_xml(label_response)
   self.label_binary = label_hash["ShipmentAcceptResponse"]["ShipmentResults"]["PackageResults"]["LabelImage"]["GraphicImage"]
   File.open(path, 'wb') do|f|
     f.write(Base64.decode64(self.label_binary))
   end

end