Module: LabelServer

Included in:
Endpost
Defined in:
lib/label_server.rb

Constant Summary collapse

SANDBOX_BASE_URL =
'https://elstestserver.endicia.com/LabelService/EwsLabelService.asmx'
PRODUCTION_BASE_URL =
'https://labelserver.endicia.com/LabelService/EwsLabelService.asmx'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



7
8
9
# File 'lib/label_server.rb', line 7

def 
  @account_id
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/label_server.rb', line 7

def password
  @password
end

#requester_idObject

Returns the value of attribute requester_id.



7
8
9
# File 'lib/label_server.rb', line 7

def requester_id
  @requester_id
end

#testObject

Returns the value of attribute test.



7
8
9
# File 'lib/label_server.rb', line 7

def test
  @test
end

Instance Method Details

#base_urlObject



9
10
11
# File 'lib/label_server.rb', line 9

def base_url
  test ? SANDBOX_BASE_URL : PRODUCTION_BASE_URL
end

#buy_postage(amount) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/label_server.rb', line 95

def buy_postage(amount)
  xml = %!
  <RecreditRequest>
    <RequesterID>#{requester_id}</RequesterID>
    <RequestID>0</RequestID>
    <CertifiedIntermediary>
      <AccountID>#{}</AccountID>
      <PassPhrase>#{password}</PassPhrase>
    </CertifiedIntermediary>
    <RecreditAmount>#{amount}</RecreditAmount>
  </RecreditRequest>!

  begin
    response = RestClient.post "#{base_url}/BuyPostageXML", :recreditRequestXML => xml

    response_xml = Nokogiri::XML(response.body)
    status_node_xml = response_xml.css('RecreditRequestResponse Status').first
    endicia_response_code = status_node_xml ? status_node_xml.text : nil

    unless endicia_response_code == '0'
      error_message_node_xml = response_xml.css('RecreditRequestResponse ErrorMessage').first
      endicia_response_message = error_message_node_xml ? error_message_node_xml.text : 'Unknown error'
      fail endicia_response_message
    end

  rescue => e
    fail e.to_s
  end
end

#change_pass_phrase(old_password, new_password) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/label_server.rb', line 13

def change_pass_phrase(old_password, new_password)
  xml = %!
    <ChangePassPhraseRequest>
      <RequesterID>#{requester_id}</RequesterID>
      <RequestID>0</RequestID>
      <CertifiedIntermediary>
        <AccountID>#{}</AccountID>
        <PassPhrase>#{old_password}</PassPhrase>
      </CertifiedIntermediary>
      <NewPassPhrase>#{new_password}</NewPassPhrase>
    </ChangePassPhraseRequest>!

  response = RestClient.post "#{base_url}/ChangePassPhraseXML", :changePassPhraseRequestXML => xml

  response_xml = Nokogiri::XML(response.body)
  status_node_xml = response_xml.css('ChangePassPhraseRequestResponse Status').first
  endicia_response_code = status_node_xml ? status_node_xml.text : nil

  unless endicia_response_code == '0'
    error_message_node_xml = response_xml.css('ChangePassPhraseRequestResponse ErrorMessage').first
    endicia_response_message = error_message_node_xml ? error_message_node_xml.text : 'Unknown error'
    fail endicia_response_message
  end
end

#get_postage_label(args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/label_server.rb', line 38

def get_postage_label(args)
  xml = %!
    <LabelRequest Test="#{test ? 'YES' : 'NO'}" LabelType="Default" ImageFormat="PDF" LabelSize="4x6">
      <RequesterID>#{requester_id}</RequesterID>
      <AccountID>#{}</AccountID>
      <PassPhrase>#{password}</PassPhrase>
      <MailClass>#{args[:mail_class]}</MailClass>
      <MailpieceShape>#{args[:mailpiece_shape]}</MailpieceShape>
      <SortType>#{args[:sort_type]}</SortType>
      <DateAdvance>0</DateAdvance>
      <WeightOz>#{args[:weight]}</WeightOz>
      <Services DeliveryConfirmation="ON" SignatureConfirmation="OFF"/>
      <ReferenceID>#{args[:order_number]}</ReferenceID>
      <PartnerCustomerID>1</PartnerCustomerID>
      <PartnerTransactionID>1</PartnerTransactionID>
      <ToName>#{args[:to][:full_name]}</ToName>
      <ToCompany>#{args[:to][:company]}</ToCompany>
      <ToAddress1>#{args[:to][:address]}</ToAddress1>
      <ToCity>#{args[:to][:city]}</ToCity>
      <ToState>#{args[:to][:state]}</ToState>
      <ToPostalCode>#{args[:to][:zipcode] ? args[:to][:zipcode].split('-')[0] : ''}</ToPostalCode>
      <ToZIP4>#{args[:to][:zipcode] ? args[:to][:zipcode].split('-')[1] : ''}</ToZIP4>
      <ToPhone>#{args[:to][:phone]}</ToPhone>
      <FromName>#{args[:from][:full_name]}</FromName>
      <ReturnAddress1>#{args[:from][:address]}</ReturnAddress1>
      <FromCity>#{args[:from][:city]}</FromCity>
      <FromState>#{args[:from][:state]}</FromState>
      <FromPostalCode>#{args[:from][:zipcode] ? args[:from][:zipcode].split('-')[0] : ''}</FromPostalCode>
      <FromZIP4>#{args[:from][:zipcode] ? args[:from][:zipcode].split('-')[1] : ''}</FromZIP4>
    </LabelRequest>!

  begin
    response = RestClient.post "#{base_url}/GetPostageLabelXML", :labelRequestXML => xml

    response_xml = Nokogiri::XML(response.body)
    status_node_xml = response_xml.css('LabelRequestResponse Status').first
    endicia_response_code = status_node_xml ? status_node_xml.text : nil

    unless endicia_response_code == '0'
      error_message_node_xml = response_xml.css('LabelRequestResponse ErrorMessage').first
      endicia_response_message = error_message_node_xml ? error_message_node_xml.text : 'Unknown error'
      fail endicia_response_message
    end

    label_node_xml = response_xml.css('LabelRequestResponse Base64LabelImage').first
    tracking_number_node_xml = response_xml.css('LabelRequestResponse TrackingNumber').first

    return {
      :label => Base64.decode64(label_node_xml.text),
      :tracking_number => tracking_number_node_xml.text
    }

  rescue => e
    fail e.to_s
  end
end