Class: RBook::Pacstream
- Inherits:
-
Object
- Object
- RBook::Pacstream
- Defined in:
- lib/rbook/pacstream.rb
Overview
Ruby class for sending and retrieving electronic orders via pacstream, a service run by the ECN Group (www.ecngroup.com.au/)
Basic Usage
pac = RBook::Pacstream.new(:username => "myusername", :password => "mypass")
pac.login
pac.get(:orders) do |order|
puts order
end
pac.get(:poacks) do |poa|
puts poa
end
pac.put(:order, 1000, order_text)
pac.quit
Alternative Usage
RBook::Pacstream.open(:username => "myusername", :password => "mypass") do |pac|
pac.get(:orders) do |order|
puts order
end
pac.put(:order, 1000, order_text)
end
Constant Summary collapse
- FILE_EXTENSIONS =
{ :orders => "ORD", :invoices => "ASN", :poacks => "POA" }
- FILE_EXTENSIONS_SINGULAR =
{ :order => "ORD", :invoice => "ASN", :poack => "POA" }
Class Method Summary collapse
-
.get(type = :orders, *args, &block) ⇒ Object
Deprecated way to download files from the pacstream server.
-
.open(*args) {|pac| ... } ⇒ Object
Alternative, block syntax.
Instance Method Summary collapse
-
#get(type, &block) ⇒ Object
download all documents of a particular type from the pacstream server.
-
#initialize(*args) ⇒ Pacstream
constructor
A new instance of Pacstream.
-
#list(type, &block) ⇒ Object
list all documents of a particular type available on the pacstream server.
-
#login ⇒ Object
logs into to the pacstream server.
-
#put(type, ref, content) ⇒ Object
upload a file to the pacstream server type - :order, invoice or :poack ref - a reference number, used to name the file content - the content to upload.
-
#quit ⇒ Object
logout from the pacstream server.
Constructor Details
#initialize(*args) ⇒ Pacstream
Returns a new instance of Pacstream.
46 47 48 49 50 51 52 53 54 |
# File 'lib/rbook/pacstream.rb', line 46 def initialize(*args) if args[0][:username].nil? && args[0][:password].nil? raise ArgumentError, 'username and password must be specified' end @server = (args[0][:servername] || "pacstream.tedis.com.au").to_s @username = args[0][:username].to_s @password = args[0][:password].to_s end |
Class Method Details
Instance Method Details
#get(type, &block) ⇒ Object
download all documents of a particular type from the pacstream server
pac.get(:orders) do |order|
puts order
end
WARNING: as soon as you download the order, the file is deleted from the server
and cannot be retrieved again
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/rbook/pacstream.rb', line 64 def get(type, &block) raise PacstreamCommandError, "No current session open" unless @ftp raise ArgumentError, 'unrecognised type' unless FILE_EXTENSIONS.include?(type.to_sym) # determine the filename pattern we're searching for file_regexp = Regexp.new(".*\.#{FILE_EXTENSIONS[type.to_sym]}$", Regexp::IGNORECASE) @ftp.chdir("outgoing/") # loop over each file in the outgoing dir and check if it matches the file type we're after @ftp.nlst.each do |file| if file.match(file_regexp) # for all matching files, download to a temp file, return the contents, then delete the file tempfile = Tempfile.new("pacstream") tempfile.close @ftp.getbinaryfile(file, tempfile.path) yield File.read(tempfile.path) tempfile.unlink end end @ftp.chdir("..") end |
#list(type, &block) ⇒ Object
list all documents of a particular type available on the pacstream server
pac.list(:orders) do |order|
puts order
end
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rbook/pacstream.rb', line 93 def list(type, &block) raise PacstreamCommandError, "No current session open" unless @ftp raise ArgumentError, 'unrecognised type' unless FILE_EXTENSIONS.include?(type.to_sym) ret = [] # determine the filename pattern we're searching for file_regexp = Regexp.new(".*\.#{FILE_EXTENSIONS[type.to_sym]}$", Regexp::IGNORECASE) @ftp.chdir("outgoing/") # loop over each file in the outgoing dir and check if it matches the file type we're after @ftp.nlst.each do |file| ret << file if file.match(file_regexp) end @ftp.chdir("..") return ret end |
#login ⇒ Object
logs into to the pacstream server. Can raise several exceptions RBook::PacstreamConnectionError - Can’t connect to server RBook::PacstreamAuthError - Invalid username or password
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rbook/pacstream.rb', line 115 def login begin @ftp = Net::FTP.open(@server) @ftp.login(@username, @password) rescue Net::FTPPermError => e raise PacstreamAuthError, e. rescue SocketError => e raise PacstreamConnectionError, e. rescue Errno::ECONNREFUSED => e raise PacstreamConnectionError, e. end end |
#put(type, ref, content) ⇒ Object
upload a file to the pacstream server type - :order, invoice or :poack ref - a reference number, used to name the file content - the content to upload
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rbook/pacstream.rb', line 132 def put(type, ref, content) raise PacstreamCommandError, "No current session open" unless @ftp raise ArgumentError, 'unrecognised type' unless FILE_EXTENSIONS_SINGULAR.include?(type.to_sym) remote_filename = "#{ref}.#{FILE_EXTENSIONS_SINGULAR[type.to_sym]}" @ftp.chdir("incoming/") tempfile = Tempfile.new("pacstream") tempfile.write(content) tempfile.close @ftp.putbinaryfile(tempfile.path, remote_filename) tempfile.unlink @ftp.chdir("..") end |
#quit ⇒ Object
logout from the pacstream server
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rbook/pacstream.rb', line 151 def quit raise PacstreamCommandError, "No current session open" unless @ftp begin @ftp.quit rescue Exception => e # do nothing. Sometimes the server closes the connection and causes # the ftp lib to freak out a little end end |