Class: Endicia::Label

Inherits:
Request show all
Defined in:
lib/endicia_ruby/label.rb

Constant Summary

Constants inherited from Request

Request::ENDICIA_API_HOSTS

Instance Method Summary collapse

Methods inherited from Request

#initialize

Constructor Details

This class inherits a constructor from Endicia::Request

Instance Method Details

#request_label(values = {}, options = {}) ⇒ Object

values is a Hash of the API values you wish to send to Endicia, nested as the documentation describes options is a Hash of other options (see default_options() for available options)



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
36
37
38
39
40
41
# File 'lib/endicia_ruby/label.rb', line 11

def request_label(values = {}, options = {})
  # Build the options for this method with passed in values overriding defaults
  root_node_attrs = root_attributes.merge!(values[:attrs] || {})
  # Include API credentials in the values we use to build the XML
  node_values = @options[:credentials].merge!(values[:nodes] || {})

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.LabelRequest(root_node_attrs) do
      recursive_build_xml_nodes!(xml, node_values)
    end
  end
  xml_body = builder.to_xml

  # Log the XML of the request if desired
  log("ENDICIA LABEL REQUEST: #{format_xml_for_logging(xml_body)}") if options[:log_requests]

  # Post the XML to the appropriate API URL
  url = "#{api_url_base}/GetPostageLabelXML"

  # Endicia's test server has an invalid certificate o_O
  raw_response = self.class.post(url, body: "labelRequestXML=#{xml_body}", verify: environment != :test)

  # Log the XML of the response if desired
  log("ENDICIA LABEL RESPONSE: #{format_xml_for_logging(raw_response.body)}") if options[:log_responses]

  # Build a nice response object and return it
  Endicia::LabelResponse.new(raw_response).tap do |the_label|
    the_label.request_body = xml_body.to_s
    the_label.request_url = url
  end
end