Permalink
Browse files

add the letter status gets and utility methods

  • Loading branch information...
1 parent 803959b commit 2f916d7f55f8e6f21eeefaebfac47e0dc0e96671 @imajes committed Aug 29, 2008
View
@@ -14,12 +14,14 @@ class Client
require 'postalmethods/document_processor.rb'
require 'postalmethods/send_letter.rb'
require 'postalmethods/get_letter_status.rb'
+ require 'postalmethods/utility.rb'
# include modules
include SendLetter
include DocumentProcessor
include GetLetterStatus
+ include UtilityMethods
API_URI = "http://api.postalmethods.com/PostalWS.asmx?WSDL"
@@ -1,5 +1,11 @@
module PostalMethods
+ class GenericCodeError < Exception
+ def initialize
+ super("You have reached a code error in the ruby gem. Please report to the forums")
+ end
+ end
+
class NoCredentialsException < Exception
def initialize
super("You have failed to provide any credentials")
@@ -12,6 +18,12 @@ def initialize
end
end
+ class InvalidLetterIDsRange < Exception
+ def initialize
+ super("You need to supply an array or a list of ids, comma seperated.")
+ end
+ end
+
## base api exception - stolen from Halcyon
## http://github.com/mtodd/halcyon/tree/master/LICENSE
@@ -24,6 +24,59 @@ def get_letter_status(id)
end
end
+
+ def get_letter_status_multiple(ids)
+ raise PostalMethods::NoPreparationException unless self.prepared
+
+ if ids.class == Array
+ ids = ids.join(",")
+ end
+
+ # minimal input checking - let api take care of it
+ return PostalMethods::InvalidLetterIDsRange unless ids.class == String
+
+ ## get status
+ opts = {:Username => self.username, :Password => self.password, :ID => ids}
+
+ rv = @rpc_driver.getLetterStatus_Multiple(opts)
+
+ puts rv
+ puts rv.inspect
+
+ ws_status = rv.getLetterStatus_MultipleResult.resultCode.to_i
+ delivery_status = rv.getLetterStatus_MultipleResult.status.to_i
+ last_status = rv.getLetterStatus_MultipleResult.lastUpdateTime
+
+ if ws_status == -3000
+ return [delivery_status, last_status, ws_status]
+ elsif API_STATUS_CODES.has_key?(ws_status)
+ instance_eval("raise APIStatusCode#{status_code.to_s.gsub(/( |\-)/,'')}Exception")
+ end
+
+ end
+
+ def get_letter_status_range(minid, maxid)
+ raise PostalMethods::NoPreparationException unless self.prepared
+
+ ## get status
+ opts = {:Username => self.username, :Password => self.password, :ID => id}
+
+ rv = @rpc_driver.getLetterStatus_Range(opts)
+
+ puts rv
+ puts rv.inspect
+
+ ws_status = rv.getLetterStatus_RangeResult.resultCode.to_i
+ delivery_status = rv.getLetterStatus_RangeResult.status.to_i
+ last_status = rv.getLetterStatus_RangeResult.lastUpdateTime
+
+ if ws_status == -3000
+ return [delivery_status, last_status, ws_status]
+ elsif API_STATUS_CODES.has_key?(ws_status)
+ instance_eval("raise APIStatusCode#{status_code.to_s.gsub(/( |\-)/,'')}Exception")
+ end
+
+ end
end
end
@@ -0,0 +1,62 @@
+module PostalMethods
+
+ module UtilityMethods
+
+ def get_letter_details(id)
+ raise PostalMethods::NoPreparationException unless self.prepared
+
+ ## get a letter as pdf over the wire
+ rv = @rpc_driver.getLetterDetails(:Username => self.username, :Password => self.password, :ID => id)
+
+ status_code = rv.getLetterDetailsResult.resultCode.to_i
+ letter_data = rv.getLetterDetailsResult
+
+ puts "status code is #{status_code} "
+
+ if status_code == -3000 # successfully received the req
+ return rv
+ elsif API_STATUS_CODES.has_key?(status_code)
+ instance_eval("raise APIStatusCode#{status_code.to_s.gsub(/( |\-)/,'')}Exception")
+ end
+ raise PostalMethods::GenericCodeError
+ end
+
+ def get_pdf(id)
+ raise PostalMethods::NoPreparationException unless self.prepared
+
+ ## get a letter as pdf over the wire
+ rv = @rpc_driver.getPDF(:Username => self.username, :Password => self.password, :ID => id)
+
+ status_code = rv.getPDFResult.resultCode.to_i
+ file_data = rv.getPDFResult.fileData
+
+ puts "status code is #{status_code} "
+
+ if status_code == -3000 # successfully received the req
+ return rv
+ elsif API_STATUS_CODES.has_key?(status_code)
+ instance_eval("raise APIStatusCode#{status_code.to_s.gsub(/( |\-)/,'')}Exception")
+ end
+ raise PostalMethods::GenericCodeError
+ end
+
+ def cancel_delivery(id)
+ raise PostalMethods::NoPreparationException unless self.prepared
+
+ ## get a letter as pdf over the wire
+ rv = @rpc_driver.cancelDelivery(:Username => self.username, :Password => self.password, :ID => id)
+
+ status_code = rv.cancelDeliveryResult.to_i
+
+ puts "status code is #{status_code} "
+
+ if status_code == -3000 # successfully received the req
+ return rv
+ elsif API_STATUS_CODES.has_key?(status_code)
+ instance_eval("raise APIStatusCode#{status_code.to_s.gsub(/( |\-)/,'')}Exception")
+ end
+ raise PostalMethods::GenericCodeError
+ end
+
+ end
+end
View
@@ -0,0 +1,21 @@
+require File.dirname(__FILE__) + '/spec_helper.rb'
+
+describe "Utility Methods" do
+
+ before :each do
+ @doc = open(File.dirname(__FILE__) + '/../doc/sample.pdf')
+ @client = PostalMethods::Client.new(PM_OPTS)
+ @client.prepare!
+ end
+
+ it "Should get the same pdf for a file as was sent" do
+ res = @client.send_letter(@doc, "the long goodbye").sendLetterResult.to_i
+ res.should > 0
+
+ rv = @client.get_pdf(res)
+
+ rv.length.should == @doc.length
+
+ end
+
+end

0 comments on commit 2f916d7

Please sign in to comment.