Module: LabelServer

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

Constant Summary collapse

LABEL_SERVER_BASE_URL =
'https://elstestserver.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.



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

def 
  @account_id
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#requester_idObject

Returns the value of attribute requester_id.



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

def requester_id
  @requester_id
end

#testObject

Returns the value of attribute test.



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

def test
  @test
end

Instance Method Details

#buy_postage(amount) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/label_server.rb', line 88

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

  begin
    response = RestClient.post "#{LABEL_SERVER_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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/label_server.rb', line 8

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

  response = RestClient.post "#{LABEL_SERVER_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



33
34
35
36
37
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
# File 'lib/label_server.rb', line 33

def get_postage_label(args)
  xml = %!
    <LabelRequest Test="#{test ? 'YES' : 'NO'}" LabelType="Default" ImageFormat="PDF" LabelSize="4x6">
      <RequesterID>#{requester_id}</RequesterID>
      <AccountID>#{account_id}</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]}</ToPostalCode>
      <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]}</FromPostalCode>
    </LabelRequest>!

  begin
    response = RestClient.post "#{LABEL_SERVER_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