awesome_usps 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,139 @@
1
+ module AwesomeUsps
2
+ module ElectricMerchandisReturn
3
+
4
+ MAX_RETRIES = 3
5
+
6
+ LIVE_DOMAIN = 'secure.shippingapis.com'
7
+ LIVE_RESOURCE = '/ShippingAPI.dll'
8
+
9
+ API_CODES = {:live =>'MerchandiseReturnV3',
10
+ :test => "MerchReturnCertifyV3"}
11
+
12
+ def merch_return(service_type, customer, retailer, permit_number, post_office, postage_delivery_unit, ounces, image_type, options={})
13
+ @service_type =service_type
14
+ @customer = customer
15
+ @retailer = retailer
16
+ @permit_number = permit_number
17
+ @post_office =post_offcice
18
+ @postage_delivery_unit = postage_delivery_unit
19
+ @ounces = ounces
20
+ @image_type= image_type
21
+ @options = options
22
+ @api = "EMRSV3.0Request"
23
+ request = merch_return_xml
24
+ #YES THE API IS SO STUPID THAT WE MUST PASS WHAT TYPE OF MIME TYPE!
25
+ commit_merch_return_xml(:live, request, image_type, false)
26
+ end
27
+
28
+ def canned_merch_return_test
29
+ @service_type ="Priority"
30
+ @customer = Location.new( :name=> "Craig Ingle", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770')
31
+ @retailer =Location.new( :name=> "XYZ Corp.", :address2 =>"1100 West Avenue")
32
+ @permit_number = "293829"
33
+ @post_office = Location.new( :state => 'NY', :city => 'New York', :zip5 => '10018')
34
+ @postage_delivery_unit = Location.new( :state => 'NY', :city => 'New York', :address2 =>"223 W 38TH ST" )
35
+ @ounces = "52"
36
+ @options = {:RMA => "13456", :insurance => "500", :confirmation => "true"}
37
+ @image_type ="PDF"
38
+ @api = "EMRSV3.0CertifyRequest"
39
+ request = merch_return_xml
40
+ commit_merch_return_xml(:test, request, @image_type, true)
41
+ end
42
+
43
+ private
44
+ def merch_return_xml
45
+ xm = Builder::XmlMarkup.new
46
+ xm.tag!("#{@api}", "USERID"=>"#{@username}") do
47
+ xm.CustomerName(@customer.name)
48
+ xm.CustomerAddress(@customer.address2)
49
+ xm.CustomerCity(@customer.city) #Used for an apartment or suite number. Yes the API is a bit fucked.
50
+ xm.CustomerState(@customer.state)
51
+ xm.CustomerZip5(@customer.zip5)
52
+ xm.RetailerName(@retailer.name)
53
+ xm.RetailerAddress(@retailer.address2)
54
+ xm.PermitNumber(@permit_number)
55
+ xm.PermitIssuingPOCity(@post_office.city)
56
+ xm.PermitIssuingPOState(@post_office.state)
57
+ xm.PermitIssuingPOZip5(@post_office.zip5)
58
+ xm.PDUPOBox(@postage_delivery_unit.address2)
59
+ xm.PDUCity(@postage_delivery_unit.city)
60
+ xm.PDUState(@postage_delivery_unit.state)
61
+ xm.PDUZip5(@postage_delivery_unit.zip5)
62
+ xm.PDUZip4(@postage_delivery_unit.zip4)
63
+ xm.ServiceType(@service_type)
64
+ xm.DeliveryConfirmation(@options[:confirmation] || "false")
65
+ xm.InsuranceValue(@options[:insurance_value])
66
+ xm.MailingAckPackageID(@options[:id])
67
+ xm.WeightInPounds("0")
68
+ xm.WeightInOunces(@ounces)
69
+ xm.RMA(@options[:rma])
70
+ xm.ImageType(@image_type)
71
+ xm.SenderName(@options[:sender_name])
72
+ xm.SenderEMail(@options[:sender_email])
73
+ xm.RecipientName(@options[:recipient_name])
74
+ xm.RecipientEMail(@options[:recipient_email])
75
+ xm.RMABarcode(@options[:barcode])
76
+ end
77
+ end
78
+
79
+
80
+ def parse_merch_return_label(xml, image_type)
81
+ if image_type == "TIF"
82
+ image_type = "image/tif"
83
+ else
84
+ image_type = "application/pdf"
85
+ end
86
+ parse = Hpricot.parse(xml)/:error
87
+ if parse != []
88
+ RAILS_DEFAULT_LOGGER.info "#{xml}"
89
+ return (Hpricot.parse(xml)/:description).inner_html
90
+ else
91
+ label = Hpricot.parse(xml)/:merchandisereturnlabel
92
+ cost = Hpricot.parse(xml)/:insurancecost
93
+ postnet = Hpricot.parse(xml)/:postnet
94
+ confirmation_number = Hpricot.parse(xml)/:deliveryconfirmationnumber
95
+ confirmation_number = "none" if confirmation_number == []
96
+ return {:image_type => image_type, :confirmation_number => confirmation_number.inner_html, :label => label.inner_html, :cost => cost.inner_html, :postnet => postnet.inner_html}
97
+ end
98
+ end
99
+
100
+
101
+
102
+ def commit_merch_return_xml(action, request, image_type, test=false)
103
+ retries = MAX_RETRIES
104
+ begin
105
+ #If and when their testing resource works again this will be useful tertiary command
106
+ url = URI.parse("https://#{LIVE_DOMAIN}#{LIVE_RESOURCE}")
107
+ req = Net::HTTP::Post.new(url.path)
108
+ req.set_form_data({'API' => API_CODES[action], 'XML' => request})
109
+ response = Net::HTTP.new(url.host, 443)
110
+ response.use_ssl
111
+ response.open_timeout = 5
112
+ response.read_timeout = 5
113
+ response.use_ssl = true
114
+ response.start
115
+
116
+ rescue Timeout::Error
117
+ if retries > 0
118
+ retries -= 1
119
+ retry
120
+ else
121
+ RAILS_DEFAULT_LOGGER.warn "The connection to the remote server timed out"
122
+ return "We appoligize for the inconvience but our USPS service is busy at the moment. To retry please refresh the browser"
123
+ end
124
+ rescue SocketError
125
+ RAILS_DEFAULT_LOGGER.error "There is a socket error with USPS plugin"
126
+ return "We appoligize for the inconvience but there is a problem with our server. To retry please refresh the browser"
127
+ end
128
+
129
+ response = response.request(req)
130
+ case response
131
+ when Net::HTTPSuccess
132
+ parse_merch_return_label(response.body, image_type)
133
+ else
134
+ RAILS_DEFAULT_LOGGER.warn("USPS plugin settings are wrong #{response}")
135
+ return "USPS plugin settings are wrong #{response}"
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,137 @@
1
+ module AwesomeUsps
2
+ module ExpressMail
3
+
4
+ MAX_RETRIES = 3
5
+
6
+ LIVE_DOMAIN = 'secure.shippingapis.com'
7
+ LIVE_RESOURCE = '/ShippingAPI.dll'
8
+
9
+ API_CODES = {:express_mail_label =>'ExpressMailLabel',
10
+ :express_mail_label_certify => "ExpressMailLabelCertify"}
11
+
12
+ def express_mail_label(orgin, destination, ounces, image_type, options={})
13
+ @orgin =orgin
14
+ @destination = destination
15
+ @ounces = ounces
16
+ @image_type = image_type
17
+ @api = "ExpressMailLabelRequest"
18
+ request = express_mail_xml
19
+ #YES THE API IS SO STUPID THAT WE MUST PASS WHAT TYPE OF MIME TYPE!
20
+ commit_express_mail_xml(:express_mail_label, request, image_type, false)
21
+ end
22
+
23
+ def canned_express_mail_label_test
24
+ @orgin = Location.new( :first_name=> "Craig", :last_name=>"Engle", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770', :phone => "2127658576")
25
+ @destination =Location.new( :firm_name=> "XYZ Corp.", :address2 =>"1100 West Avenue", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770')
26
+ @ounces = "50"
27
+ @image_type ="PDF"
28
+ @options = {}
29
+ @api = "ExpressMailLabelCertifyRequest"
30
+ request = express_mail_xml
31
+ commit_express_mail_xml(:express_mail_label_certify, request, @image_type, true)
32
+ end
33
+
34
+ private
35
+ def express_mail_xml
36
+ xm = Builder::XmlMarkup.new
37
+ xm.tag!("#{@api}", "USERID"=>"#{@username}") do
38
+ xm.Option
39
+ xm.EMCAAccount
40
+ xm.EMCAPassword
41
+ xm.ImageParameters
42
+ xm.FromFirstName(@orgin.name)
43
+ xm.FromLastName(@orgin.last_name)
44
+ xm.FromFirm(@orgin.firm_name)
45
+ xm.FromAddress1(@orgin.address1) #Used for an apartment or suite number. Yes the API is a bit fucked.
46
+ xm.FromAddress2(@orgin.address2)
47
+ xm.FromCity(@orgin.city)
48
+ xm.FromState(@orgin.state)
49
+ xm.FromZip5(@orgin.zip5)
50
+ xm.FromZip4(@orgin.zip4)
51
+ xm.FromPhone(@orgin.phone)
52
+ xm.ToFirstName(@destination.name)
53
+ xm.ToLastName(@destination.last_name)
54
+ xm.ToFirm(@destination.firm_name)
55
+ xm.ToAddress1(@destination.address1)
56
+ xm.ToAddress2(@destination.address2)
57
+ xm.ToCity(@destination.city)
58
+ xm.ToState(@destination.state)
59
+ xm.ToZip5(@destination.zip5)
60
+ xm.ToZip4(@destination.zip4)
61
+ xm.ToPhone(@destination.phone)
62
+ xm.WeightInOunces(@ounces)
63
+ xm.FlatRate(@options[:flat_rate])
64
+ xm.StandardizeAddress(@options[:standardize_address])
65
+ xm.WaiverOfSignature(@options[:waiver_signature])
66
+ xm.NoHoliday(@options[:no_holiday])
67
+ xm.NoWeekend(@options[:no_weekend])
68
+ xm.SeparateReceiptPage(@options[:seperate])
69
+ xm.POZipCode(@options[:po_zip_code])
70
+ xm.ImageType(@image_type)
71
+ xm.LabelDate(@options[:labe_date])
72
+ xm.SenderName(@options[:sender_name])
73
+ xm.SenderEMail(@options[:sender_email])
74
+ xm.RecipientName(@options[:recipient_name])
75
+ xm.RecipientEMail(@options[:recipient_email])
76
+ end
77
+ end
78
+
79
+
80
+ def parse_express_mail_label(xml, image_type)
81
+ if image_type == "TIF"
82
+ image_type = "image/tif"
83
+ else
84
+ image_type = "application/pdf"
85
+ end
86
+ parse = Hpricot.parse(xml)/:error
87
+ if parse != []
88
+ RAILS_DEFAULT_LOGGER.info "#{xml}"
89
+ return (Hpricot.parse(xml)/:description).inner_html
90
+ else
91
+ label = Hpricot.parse(xml)/:emlabel
92
+ postage =Hpricot.parse(xml)/:postage
93
+ confirmation_number = Hpricot.parse(xml)/:emconfirmationnumber
94
+ confirmation_number = "none" if confirmation_number == []
95
+ return {:image_type => image_type, :confirmation_number => confirmation_number.inner_html, :postage => postage.inner_html, :label => label.inner_html}
96
+ end
97
+ end
98
+
99
+ def commit_express_mail_xml(action, request, image_type, test=false)
100
+ retries = MAX_RETRIES
101
+ begin
102
+ #If and when their testing resource works again this will be useful tertiary command
103
+ url = URI.parse("https://#{LIVE_DOMAIN}#{LIVE_RESOURCE}")
104
+ req = Net::HTTP::Post.new(url.path)
105
+ req.set_form_data({'API' => API_CODES[action], 'XML' => request})
106
+ response = Net::HTTP.new(url.host, 443)
107
+ response.use_ssl
108
+ response.verify_mode = OpenSSL::SSL::VERIFY_NONE
109
+ response.open_timeout = 5
110
+ response.read_timeout = 5
111
+ response.use_ssl = true
112
+ response.start
113
+
114
+ rescue Timeout::Error
115
+ if retries > 0
116
+ retries -= 1
117
+ retry
118
+ else
119
+ RAILS_DEFAULT_LOGGER.warn "The connection to the remote server timed out"
120
+ return "We appoligize for the inconvience but our USPS service is busy at the moment. To retry please refresh the browser"
121
+ end
122
+ rescue SocketError
123
+ RAILS_DEFAULT_LOGGER.error "There is a socket error with USPS plugin"
124
+ return "We appoligize for the inconvience but there is a problem with our server. To retry please refresh the browser"
125
+ end
126
+
127
+ response = response.request(req)
128
+ case response
129
+ when Net::HTTPSuccess
130
+ parse_express_mail_label(response.body, image_type)
131
+ else
132
+ RAILS_DEFAULT_LOGGER.warn("USPS plugin settings are wrong #{response}")
133
+ return "USPS plugin settings are wrong #{response}"
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,43 @@
1
+ module AwesomeUsps #:nodoc:
2
+ class InternationalItem
3
+
4
+ attr_reader :options,
5
+ :description,
6
+ :quantity,
7
+ :value,
8
+ :pounds,
9
+ :ounces,
10
+ :tariff_number,
11
+ :country
12
+
13
+ alias_method :from_country, :country
14
+ alias_method :country_of_origin, :from_country
15
+
16
+ def initialize(options = {})
17
+ @description = options[:description]
18
+ @quantity= options[:quantity]
19
+ @value = options[:value]
20
+ @pounds = options[:pounds]
21
+ @ounces = options[:ounces]
22
+ @tariff_number = options[:tariff_number]
23
+ @country = options[:country]
24
+ end
25
+
26
+ def to_s
27
+ prettyprint.gsub(/\n/, ' ')
28
+ end
29
+
30
+ def prettyprint
31
+ chunks = []
32
+ chunks << [@description,@quantity,@value, @tariff_number, @country].reject {|e| e.blank?}.join("\n")
33
+ chunks << [@pounds,@ounces].reject {|e| e.blank?}.join(', ')
34
+ chunks.reject {|e| e.blank?}.join("\n")
35
+ end
36
+
37
+ def inspect
38
+ string = prettyprint
39
+ string
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,317 @@
1
+ module AwesomeUsps
2
+ module InternationalMailLabels
3
+
4
+ MAX_RETRIES = 3
5
+
6
+ LIVE_DOMAIN = 'secure.shippingapis.com'
7
+ LIVE_RESOURCE = '/ShippingAPI.dll'
8
+
9
+
10
+ API_CODES = {:express_mail => "ExpressMailIntl",
11
+ :express_mail_certify => "ExpressMailIntlCertify",
12
+ :priority_mail => "PriorityMailIntl",
13
+ :priority_mail_certify => "PriorityMailIntlCertify",
14
+ :first_class_mail => "FirstClassMailIntl",
15
+ :first_class_mail_certify => "FirstClassMailIntlCertify"}
16
+
17
+ RESPONSE = {:express_mail => "ExpressMailIntlResponse",
18
+ :express_mail_certify => "ExpressMailIntlCertifyResponse",
19
+ :priority_mail => "PriorityMailIntlResponse",
20
+ :priority_mail_certify => "PriorityMailIntlCertifyResponse",
21
+ :first_class_mail => "FirstClassMailIntlResponse",
22
+ :first_class_mail_certify => "FirstClassMailIntlCertifyResponse"}
23
+
24
+ def express_mail_international_label(sender, receiver, items, content_type, image_type, po_box_flag ="N",
25
+ image_layout="ALLINONEFILE", label_type="1", options={})
26
+ @sender = sender
27
+ @receiver = receiver
28
+ @items = items
29
+ Array(@items) if not @items.is_a? Array
30
+ @po_box_flag =po_box_flag
31
+ @content_type = content_type
32
+ @image_layout = image_layout
33
+ @label_type =label_type
34
+ @image_type = image_type
35
+ @options =options
36
+ @api = "ExpressMailIntlRequest"
37
+ request = international_mail_labels_xml
38
+ #YES THE API IS THAT STUPID THAT WE MUST PASS WHAT TYPE OF MIME TYPE!
39
+ commit_international_mail_labels_xml(:express_mail, request, image_type, false)
40
+ end
41
+
42
+ def canned_express_mail_international_label_test
43
+ @sender = Location.new( :first_name=> "John", :last_name => "Berger", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770', :phone => "2016585989")
44
+ @receiver =Location.new( :name=> "Fairfax Post Office", :address2 =>"10660 Page Ave", :city => 'Tokyo', :zip5 => "22030", :country => "Japan" )
45
+
46
+ @po_box_flag ="N"
47
+ @content_type = "GIFT"
48
+ @image_layout="ALLINONEFILE"
49
+ @image_type = "PDF"
50
+ @items = [InternationalItem.new(:ounces => 50, :country => "United States", :quantity => "50", :description => "Pens Pens Pens", :value => 50.00), InternationalItem.new(:ounces => 40, :country => "United States", :quantity => "50", :description => "Pens Pens Pens", :value => 50.00)]
51
+ @api = "ExpressMailIntlCertifyRequest"
52
+ @options ={}
53
+ request= international_mail_labels_xml
54
+ commit_international_mail_labels_xml(:express_mail_certify, request, @image_type, true)
55
+ end
56
+ def priority_mail_international_label(sender, receiver, items, content_type, image_type, po_box_flag ="N",
57
+ image_layout="ALLINONEFILE", label_type="1", options={})
58
+ @sender = sender
59
+ @receiver = receiver
60
+ @items = items
61
+ Array(@items) if not @items.is_a? Array
62
+ @po_box_flag =po_box_flag
63
+ @content_type = content_type
64
+ @image_layout = image_layout
65
+ @label_type =label_type
66
+ @image_type = image_type
67
+ @options =options
68
+ @api = "PriorityMailIntlRequest"
69
+ request = international_mail_labels_xml
70
+ #YES THE API IS THAT STUPID THAT WE MUST PASS WHAT TYPE OF MIME TYPE!
71
+ commit_international_mail_labels_xml(:priority_mail, request, image_type, false)
72
+ end
73
+
74
+ def canned_priority_mail_international_label_test
75
+ @sender = Location.new( :first_name=> "John", :last_name => "Berger", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770', :phone => "2016585989")
76
+ @receiver =Location.new( :name=> "Fairfax Post Office", :address2 =>"10660 Page Ave", :city => 'Tokyo', :zip5 => "22030", :country => "Japan" )
77
+
78
+ @po_box_flag ="N"
79
+ @content_type = "GIFT"
80
+ @image_layout="ALLINONEFILE"
81
+ @image_type = "PDF"
82
+ @items = [InternationalItem.new(:ounces => 50, :country => "United States", :quantity => "50", :description => "Pens Pens Pens", :value => 50.00), InternationalItem.new(:ounces => 40, :country => "United States", :quantity => "50", :description => "Pens Pens Pens", :value => 50.00)]
83
+ @api = "PriorityMailIntlCertifyRequest"
84
+ @options ={}
85
+ request= international_mail_labels_xml
86
+ commit_international_mail_labels_xml(:priority_mail_certify, request, @image_type, true)
87
+ end
88
+
89
+ def first_class_international_label(sender, receiver, items, content_type, image_type, po_box_flag ="N",
90
+ image_layout="ALLINONEFILE", label_type="1", options={})
91
+ @sender = sender
92
+ @receiver = receiver
93
+ @items = items
94
+ Array(@items) if not @items.is_a? Array
95
+ @po_box_flag =po_box_flag
96
+ @content_type = content_type
97
+ @image_layout = image_layout
98
+ @label_type =label_type
99
+ @image_type = image_type
100
+ @options =options
101
+ @api = "FirstClassMailIntlRequest"
102
+ request = first_class_international_mail_labels_xml
103
+ #YES THE API IS THAT STUPID THAT WE MUST PASS WHAT TYPE OF MIME TYPE!
104
+ commit_international_mail_labels_xml(:first_class_mail, request, image_type, false)
105
+ end
106
+
107
+ def canned_first_class_mail_international_label_test
108
+ @sender = Location.new( :first_name=> "John", :last_name => "Berger", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770', :phone => "2016585989")
109
+ @receiver =Location.new( :name=> "Fairfax Post Office", :address2 =>"10660 Page Ave", :city => 'Tokyo', :zip5 => "22030", :country => "Japan" )
110
+
111
+ @po_box_flag ="N"
112
+ @content_type = "GIFT"
113
+ @image_layout="ALLINONEFILE"
114
+ @image_type = "PDF"
115
+ @items = [InternationalItem.new(:ounces => 10, :country => "United States", :quantity => "50", :description => "Pens Pens Pens", :value => 50.00), InternationalItem.new(:ounces => 10, :country => "United States", :quantity => "50", :description => "Pens Pens Pens", :value => 50.00)]
116
+ @api = "FirstClassMailIntlCertifyRequest"
117
+ @options ={}
118
+ request= first_class_international_mail_labels_xml
119
+ commit_international_mail_labels_xml(:first_class_mail_certify, request, @image_type, true)
120
+ end
121
+
122
+ private
123
+ def international_mail_labels_xml
124
+ xm = Builder::XmlMarkup.new
125
+ xm.tag!("#{@api}", "USERID"=>"#{@username}") do
126
+ xm.Option
127
+ xm.ImageParameters
128
+ xm.FromFirstName(@sender.first_name)
129
+ xm.FromMiddleInitial(@options[:middle_initial])
130
+ xm.FromLastName(@sender.last_name)
131
+ xm.FromFirm(@sender.firm_name)
132
+ xm.FromAddress1(@sender.address1)
133
+ xm.FromAddress2(@sender.address2)
134
+ xm.FromUrbanization(@sender.from_urbanization)
135
+ xm.FromCity(@sender.city)
136
+ xm.FromState(@sender.state)
137
+ xm.FromZip5(@sender.zip5)
138
+ xm.FromZip4(@sender.zip4)
139
+ xm.FromPhone(@sender.phone)
140
+ xm.FromCustomsReference(@options[:from_customs_reference])
141
+ xm.ToName(@receiver.name)
142
+ xm.ToFirm(@receiver.firm_name)
143
+ xm.ToAddress1(@receiver.address1)
144
+ xm.ToAddress2(@receiver.address2)
145
+ xm.ToAddress3(@receiver.address3)
146
+ xm.ToCity(@receiver.city)
147
+ xm.ToProvince(@receiver.province)
148
+ xm.ToCountry(@receiver.country)
149
+ xm.ToPostalCode(@receiver.postal_code)
150
+ xm.ToPOBoxFlag(@po_box_flag)
151
+ xm.ToPhone(@receiver.phone)
152
+ xm.ToFax(@options[:fax])
153
+ xm.ToEmail(@options[:email])
154
+ xm.ToCustomsReference(@options[:to_customs_reference])
155
+ xm.NonDeliveryOption(@options[:non_delivery_option])
156
+ xm.AltReturnAddress1(@options[:alt_return_address1])
157
+ xm.AltReturnAddress2(@options[:alt_return_address2])
158
+ xm.AltReturnAddress3(@options[:alt_return_address3])
159
+ xm.AltReturnCountry(@options[:alt_return_country])
160
+ xm.Container(@options[:container])
161
+ xm.ShippingContents do
162
+ @items.each do |item|
163
+ xm.ItemDetail do
164
+ xm.Description(item.description)
165
+ xm.Quantity(item.quantity)
166
+ xm.Value(item.value)
167
+ xm.NetPounds(item.pounds)
168
+ xm.NetOunces(item.ounces)
169
+ xm.HSTariffNumber(item.tariff_number)
170
+ xm.CountryOfOrigin(item.country)
171
+ end
172
+ end
173
+ end
174
+ xm.InsuredNumber(@options[:insurance_number])
175
+ xm.InsuredAmount(@options[:insured_amount])
176
+ xm.Postage(@options[:postage])
177
+ xm.GrossPounds("0")
178
+ xm.GrossOunces(@items.sum {|item| item.ounces.to_f})
179
+ xm.ContentType(@content_type)
180
+ xm.ContentTypeOther(@options[:other])
181
+ xm.Agreement("Y")
182
+ xm.Comments(@options[:comments])
183
+ xm.LicenseNumber(@options[:license_number])
184
+ xm.CertificateNumber(@options[:certificate_number])
185
+ xm.InvoiceNumber(@options[:invoice_number])
186
+ xm.ImageType(@image_type)
187
+ xm.ImageLayout(@image_layout)
188
+ xm.CustomerRefNo(@options[:reference_number])
189
+ xm.POZipCode(@options[:po_zip_code])
190
+ xm.LabelDate(@options[:label_date])
191
+ xm.HoldForManifest(@options[:hold])
192
+ end
193
+ end
194
+
195
+ def first_class_international_mail_labels_xml
196
+ xm = Builder::XmlMarkup.new
197
+ xm.tag!("#{@api}", "USERID"=>"#{@username}") do
198
+ xm.Option
199
+ xm.ImageParameters
200
+ xm.FromFirstName(@sender.first_name)
201
+ xm.FromMiddleInitial(@options[:middle_initial])
202
+ xm.FromLastName(@sender.last_name)
203
+ xm.FromFirm(@sender.firm_name)
204
+ xm.FromAddress1(@sender.address1)
205
+ xm.FromAddress2(@sender.address2)
206
+ xm.FromUrbanization(@sender.from_urbanization)
207
+ xm.FromCity(@sender.city)
208
+ xm.FromState(@sender.state)
209
+ xm.FromZip5(@sender.zip5)
210
+ xm.FromZip4(@sender.zip4)
211
+ xm.FromPhone(@sender.phone)
212
+ xm.ToName(@receiver.name)
213
+ xm.ToFirm(@receiver.firm_name)
214
+ xm.ToAddress1(@receiver.address1)
215
+ xm.ToAddress2(@receiver.address2)
216
+ xm.ToAddress3(@receiver.address3)
217
+ xm.ToCity(@receiver.city)
218
+ xm.ToProvince(@receiver.province)
219
+ xm.ToCountry(@receiver.country)
220
+ xm.ToPostalCode(@receiver.postal_code)
221
+ xm.ToPOBoxFlag(@po_box_flag)
222
+ xm.ToPhone(@receiver.phone)
223
+ xm.ToFax(@options[:fax])
224
+ xm.ToEmail(@options[:email])
225
+ xm.FirstClassMailType(@options[:first_class_mail_type])
226
+ xm.ShippingContents do
227
+ @items.each do |item|
228
+ xm.ItemDetail do
229
+ xm.Description(item.description)
230
+ xm.Quantity(item.quantity)
231
+ xm.Value(item.value)
232
+ xm.NetPounds(item.pounds)
233
+ xm.NetOunces(item.ounces)
234
+ xm.HSTariffNumber(item.tariff_number)
235
+ xm.CountryOfOrigin(item.country)
236
+ end
237
+ end
238
+ end
239
+ xm.GrossPounds("0")
240
+ xm.GrossOunces(@items.sum {|item| item.ounces.to_f})
241
+ xm.Machinable(@options[:machinable])
242
+ xm.ContentType(@content_type)
243
+ xm.ContentTypeOther(@options[:other])
244
+ xm.Agreement("Y")
245
+ xm.Comments(@options[:comments])
246
+ xm.ImageType(@image_type)
247
+ xm.ImageLayout(@image_layout)
248
+ xm.LabelDate(@options[:label_date])
249
+ xm.HoldForManifest(@options[:hold])
250
+ end
251
+ end
252
+
253
+
254
+ def parse_internation_label(xml, image_type, response)
255
+ label_hash = {}
256
+ image_type = image_mime(image_type)
257
+ parse = Hpricot.parse(xml)/:error
258
+ if parse != []
259
+ RAILS_DEFAULT_LOGGER.info "#{xml}"
260
+ return parse.inner_html
261
+ end
262
+ parse = Hpricot.parse(xml).search("#{response.downcase}")
263
+ parse.each do |detail|
264
+ h = {}
265
+ detail.children.each { |elem| label_hash[elem.name.to_sym] = elem.inner_text unless elem.inner_text.blank? }
266
+ end
267
+ label_hash[:image_type] = image_type
268
+ return label_hash
269
+ end
270
+
271
+ def image_mime(image_type)
272
+ if image_type == "TIF"
273
+ image_type = "image/tif"
274
+ else
275
+ image_type = "application/pdf"
276
+ end
277
+ end
278
+
279
+ def commit_international_mail_labels_xml(action, request, image_type, test=false)
280
+ retries = MAX_RETRIES
281
+ begin
282
+ #If and when their testing resource works again this will be useful tertiary command
283
+ url = URI.parse("https://#{LIVE_DOMAIN}#{LIVE_RESOURCE}")
284
+ req = Net::HTTP::Post.new(url.path)
285
+ req.set_form_data({'API' => API_CODES[action], 'XML' => request})
286
+ response = Net::HTTP.new(url.host, 443)
287
+ response.use_ssl
288
+ response.open_timeout = 5
289
+ response.read_timeout = 5
290
+ response.use_ssl = true
291
+ response.start
292
+
293
+ rescue Timeout::Error
294
+ if retries > 0
295
+ retries -= 1
296
+ retry
297
+ else
298
+ RAILS_DEFAULT_LOGGER.warn "The connection to the remote server timed out"
299
+ return "We appoligize for the inconvience but our USPS service is busy at the moment. To retry please refresh the browser"
300
+ end
301
+ rescue SocketError
302
+ RAILS_DEFAULT_LOGGER.error "There is a socket error with USPS plugin"
303
+ return "We appoligize for the inconvience but there is a problem with our server. To retry please refresh the browser"
304
+ end
305
+
306
+ response = response.request(req)
307
+ case response
308
+ when Net::HTTPSuccess
309
+
310
+ parse_internation_label(response.body, image_type, RESPONSE[action])
311
+ else
312
+ RAILS_DEFAULT_LOGGER.warn("USPS plugin settings are wrong #{response.body}")
313
+ return "USPS plugin settings are wrong #{response.body}"
314
+ end
315
+ end
316
+ end
317
+ end