Class: USPS::Client

Inherits:
Object
  • Object
show all
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
35
# 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*.
  options = { timeout: USPS.config.timeout, 
              params: { "API" => request.api, "XML" => request.build } }

  unless USPS.config.proxy.blank?
    options.merge!({ proxy: USPS.config.proxy })
  end
  
  response = Typhoeus::Request.get(server, options)

  # 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

Returns:

  • (Boolean)


37
38
39
# File 'lib/usps/client.rb', line 37

def testing?
  USPS.config.testing
end