Class: Viapost::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/viapost/interface.rb

Defined Under Namespace

Classes: InvalidUserOrPass, UnknownEnvironment

Constant Summary collapse

TEST_WSDL =
'http://apitest.viapost.com/viapostcustomer.asmx?WSDL'
LIVE_WSDL =
'http://api.viapost.com/Viapostcustomer.asmx?WSDL'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, pass, environment = :live) ⇒ Interface

Returns a new instance of Interface.



15
16
17
18
19
# File 'lib/viapost/interface.rb', line 15

def initialize(user, pass, environment = :live)
  @driver = SOAP::WSDLDriverFactory.new(wsdl(environment)).create_rpc_driver
  @user = user
  @pass = pass
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



13
14
15
# File 'lib/viapost/interface.rb', line 13

def driver
  @driver
end

#login_tokenObject (readonly)

private



54
55
56
# File 'lib/viapost/interface.rb', line 54

def 
  @login_token
end

Instance Method Details

#balanceObject



21
22
23
24
# File 'lib/viapost/interface.rb', line 21

def balance
  response = @driver.GetBalance(:sLoginToken => )
  response.dBalance.to_f
end

#letter(letter_id) ⇒ Object



48
49
50
# File 'lib/viapost/interface.rb', line 48

def letter(letter_id)
  @driver.GetLetterByID(:sLoginToken => , :LetterID => letter_id)
end

#letters(query = '') ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/viapost/interface.rb', line 26

def letters(query = '')
  response = @driver.GetLetters(:sLoginToken => , :sSearchDocumentName => query)
  soap_letters = response.returnLetters.anyType
  
  # for some reason their API returns just the letter instead of an array if it only returns one letter
  if soap_letters.is_a?(Array)
    soap_letters.map { |l| Letter.build_from_soap_object(l) }
  else
    [Letter.build_from_soap_object(soap_letters)]
  end
end

#send_letter(letter, name, address_1, address_2, address_3, address_4, address_5, postcode) ⇒ Object



44
45
46
# File 'lib/viapost/interface.rb', line 44

def send_letter(letter, name, address_1, address_2, address_3, address_4, address_5, postcode)
  response = @driver.SendLetter(:sLoginToken => , :myCustomerAPILetter => letter, :addressName => name, :address1 => address_1, :address2 => address_2, :address3 => address_3, :address4 => address_4, :address5 => address_5, :postcode => postcode)
end

#upload_letter(file, title, colour = false, double_sided = false) ⇒ Object



38
39
40
41
42
# File 'lib/viapost/interface.rb', line 38

def upload_letter(file, title, colour = false, double_sided = false)
  file = File.open(file, 'r') if file.is_a?(String) # allow path instead of file
  
  response = @driver.UploadDocument(:sLoginToken => , :sDocumentName => title, :DocumentData => file.read, :Colour => colour, :DoubleSided => double_sided)
end

#wsdl(environment) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/viapost/interface.rb', line 64

def wsdl(environment)
  case environment.to_sym
  when :live
    wsdl = LIVE_WSDL
  when :test
    wsdl = TEST_WSDL
  else
    raise UnknownEnvironment
  end
end