Class: USPS::Client
- Inherits:
-
Object
- Object
- USPS::Client
- Defined in:
- lib/usps/client.rb
Instance Method Summary collapse
Instance Method Details
#request(request, &block) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/usps/client.rb', line 5 def request(request, &block) server = server(request) # Make the API request to the USPS servers. Used to support POST, now it's # just GET request *grumble*. response = Typhoeus::Request.get(server, { :timeout => USPS.config.timeout, :params => { "API" => request.api, "XML" => request.build } }) # Parse the request xml = Nokogiri::XML.parse(response.body) # Process and raise errors if((error = xml.search('Error')).any?) # This is where things get a little tricky. There are a bunch of errors # that the USPS can send back. why = error.search('Description').text code = error.search('Number').text source = error.search('Source').text raise Error.for_code(code).new(why, code, source) end # Initialize the proper response object and parse the message request.response_for(xml) end |
#testing? ⇒ Boolean
36 37 38 |
# File 'lib/usps/client.rb', line 36 def testing? USPS.config.testing end |