Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

force API results to be in UTF-8, not ASCII-8BIT

  • Loading branch information...
commit 613c2abce9cabd156cac14742b6a099308026201 1 parent 09cd203
Bill Lipa wlipa authored

Showing 1 changed file with 25 additions and 1 deletion. Show diff stats Hide diff stats

  1. +25 1 app/models/amazon_fulfillment.rb
26 app/models/amazon_fulfillment.rb
@@ -8,6 +8,29 @@ def fetch_tracking_raw(oid)
8 8 commit :outbound, :tracking, build_tracking_request(oid, {})
9 9 end
10 10
  11 + # See http://www.ruby-forum.com/topic/208730#908342
  12 + def clean_encoding(str)
  13 + # Try it as UTF-8 directly
  14 + str.dup.force_encoding('UTF-8')
  15 + rescue EncodingError
  16 + # Force it to UTF-8, throwing out invalid bits
  17 + str.encode!( 'UTF-8', invalid: :replace, undef: :replace )
  18 + end
  19 +
  20 + alias_method :orig_parse_response, :parse_response
  21 + def parse_response(service, op, xml)
  22 + # Force UTF-8 encoding
  23 + cxml = clean_encoding(xml)
  24 + resp = orig_parse_response(service, op, cxml)
  25 + if resp.is_a?(Hash) && resp[:success] == FAILURE && resp.keys.size == 1
  26 + # XML parse error
  27 + Rails.logger.info "*" * 20 + " xml parse error"
  28 + Rails.logger.info cxml
  29 + end
  30 + resp
  31 + end
  32 +
  33 +
11 34 # Monkeypatch of the original parse_tracking_response to include carrier, ship date, and arrival time.
12 35 # Changed lines are marked.
13 36 def parse_tracking_response(document)
@@ -148,10 +171,11 @@ def fulfill
148 171 # shipment that will result in a permanent failure to fulfill, else nil.
149 172 def track
150 173 sleep 1 # avoid throttle from Amazon
  174 + Fulfillment.log "amazon order id #{@shipment.number}"
151 175 resp = remote.fetch_tracking_raw(@shipment.number)
152 176 Fulfillment.log "#{resp.params}"
153 177 # This can happen, for example, if the SKU doesn't exist.
154   - return :error if !resp.success? && resp.faultstring["requested order not found"]
  178 + return :error if !resp.success? && resp.params["faultstring"] && resp.faultstring["requested order not found"]
155 179 return nil unless resp.params["fulfillment_info"] # not known yet
156 180 resp.params["fulfillment_info"][@shipment.number]
157 181 end

0 comments on commit 613c2ab

Please sign in to comment.
Something went wrong with that request. Please try again.