Class: Bisac::POA
- Inherits:
-
Object
- Object
- Bisac::POA
- Defined in:
- lib/bisac/poa.rb
Overview
Represents a single BISAC purchase order acknowledgement
Generating
It’s currently not possible to generate a POA with this class. All it needs is a to_s method though, much like Bisac::PO. Patches welcome.
Reading
Each POA file can contain multiple POA’s, so use pasrse_file() to iterate over them all.
Bisac::POA.parse_file("filename.bsc") do |msg|
puts msg.source_san
puts msg.source_name
puts msg.items.size
...
end
Instance Attribute Summary collapse
-
#ack_type ⇒ Object
Returns the value of attribute ack_type.
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#customer_san ⇒ Object
Returns the value of attribute customer_san.
-
#customer_suffix ⇒ Object
Returns the value of attribute customer_suffix.
-
#date ⇒ Object
Returns the value of attribute date.
-
#destination_san ⇒ Object
Returns the value of attribute destination_san.
-
#destination_suffix ⇒ Object
Returns the value of attribute destination_suffix.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#format_version ⇒ Object
Returns the value of attribute format_version.
-
#items ⇒ Object
Returns the value of attribute items.
-
#po_cancel_date ⇒ Object
Returns the value of attribute po_cancel_date.
-
#po_date ⇒ Object
Returns the value of attribute po_date.
-
#po_number ⇒ Object
poa header attributes.
-
#po_type ⇒ Object
Returns the value of attribute po_type.
-
#poa_date ⇒ Object
Returns the value of attribute poa_date.
-
#source_name ⇒ Object
file header attributes.
-
#source_san ⇒ Object
file header attributes.
-
#source_suffix ⇒ Object
file header attributes.
-
#supplier_poa_number ⇒ Object
poa header attributes.
-
#supplier_san ⇒ Object
Returns the value of attribute supplier_san.
-
#supplier_suffix ⇒ Object
Returns the value of attribute supplier_suffix.
Class Method Summary collapse
-
.parse_file(input, &block) ⇒ Object
return all POs from a BISAC file.
-
.parse_string(input, &block) ⇒ Object
return all POs from a BISAC string.
Instance Method Summary collapse
-
#build_message(data) ⇒ Object
Populate the current object with the message contained in data.
-
#initialize ⇒ POA
constructor
creates a new Bisac::POA object.
- #to_s ⇒ Object
- #total_qty ⇒ Object
Constructor Details
#initialize ⇒ POA
creates a new Bisac::POA object
38 39 40 41 42 43 44 45 |
# File 'lib/bisac/poa.rb', line 38 def initialize @items = [] # default values @cancellation_date = "000000" @do_not_ship_before = "000000" @backorder = true end |
Instance Attribute Details
#ack_type ⇒ Object
Returns the value of attribute ack_type.
27 28 29 |
# File 'lib/bisac/poa.rb', line 27 def ack_type @ack_type end |
#currency ⇒ Object
Returns the value of attribute currency.
33 34 35 |
# File 'lib/bisac/poa.rb', line 33 def currency @currency end |
#customer_san ⇒ Object
Returns the value of attribute customer_san.
31 32 33 |
# File 'lib/bisac/poa.rb', line 31 def customer_san @customer_san end |
#customer_suffix ⇒ Object
Returns the value of attribute customer_suffix.
31 32 33 |
# File 'lib/bisac/poa.rb', line 31 def customer_suffix @customer_suffix end |
#date ⇒ Object
Returns the value of attribute date.
25 26 27 |
# File 'lib/bisac/poa.rb', line 25 def date @date end |
#destination_san ⇒ Object
Returns the value of attribute destination_san.
26 27 28 |
# File 'lib/bisac/poa.rb', line 26 def destination_san @destination_san end |
#destination_suffix ⇒ Object
Returns the value of attribute destination_suffix.
26 27 28 |
# File 'lib/bisac/poa.rb', line 26 def destination_suffix @destination_suffix end |
#filename ⇒ Object
Returns the value of attribute filename.
25 26 27 |
# File 'lib/bisac/poa.rb', line 25 def filename @filename end |
#format_version ⇒ Object
Returns the value of attribute format_version.
25 26 27 |
# File 'lib/bisac/poa.rb', line 25 def format_version @format_version end |
#items ⇒ Object
Returns the value of attribute items.
35 36 37 |
# File 'lib/bisac/poa.rb', line 35 def items @items end |
#po_cancel_date ⇒ Object
Returns the value of attribute po_cancel_date.
34 35 36 |
# File 'lib/bisac/poa.rb', line 34 def po_cancel_date @po_cancel_date end |
#po_date ⇒ Object
Returns the value of attribute po_date.
33 34 35 |
# File 'lib/bisac/poa.rb', line 33 def po_date @po_date end |
#po_number ⇒ Object
poa header attributes
30 31 32 |
# File 'lib/bisac/poa.rb', line 30 def po_number @po_number end |
#po_type ⇒ Object
Returns the value of attribute po_type.
34 35 36 |
# File 'lib/bisac/poa.rb', line 34 def po_type @po_type end |
#poa_date ⇒ Object
Returns the value of attribute poa_date.
33 34 35 |
# File 'lib/bisac/poa.rb', line 33 def poa_date @poa_date end |
#source_name ⇒ Object
file header attributes
24 25 26 |
# File 'lib/bisac/poa.rb', line 24 def source_name @source_name end |
#source_san ⇒ Object
file header attributes
24 25 26 |
# File 'lib/bisac/poa.rb', line 24 def source_san @source_san end |
#source_suffix ⇒ Object
file header attributes
24 25 26 |
# File 'lib/bisac/poa.rb', line 24 def source_suffix @source_suffix end |
#supplier_poa_number ⇒ Object
poa header attributes
30 31 32 |
# File 'lib/bisac/poa.rb', line 30 def supplier_poa_number @supplier_poa_number end |
#supplier_san ⇒ Object
Returns the value of attribute supplier_san.
32 33 34 |
# File 'lib/bisac/poa.rb', line 32 def supplier_san @supplier_san end |
#supplier_suffix ⇒ Object
Returns the value of attribute supplier_suffix.
32 33 34 |
# File 'lib/bisac/poa.rb', line 32 def supplier_suffix @supplier_suffix end |
Class Method Details
.parse_file(input, &block) ⇒ Object
return all POs from a BISAC file
48 49 50 51 52 53 54 |
# File 'lib/bisac/poa.rb', line 48 def self.parse_file(input, &block) raise ArgumentError, 'no file provided' if input.nil? raise ArgumentError, 'Invalid file' unless File.file?(input) self.parse_string(File.read(input)) do |msg| yield msg end end |
.parse_string(input, &block) ⇒ Object
return all POs from a BISAC string
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bisac/poa.rb', line 57 def self.parse_string(input, &block) raise ArgumentError, 'no data provided' if input.nil? data = [] input.split("\n").each do |l| data << l # yield each message found in the string. A line starting with # 90 is the footer to a PO if data.last[0,2] == "90" msg = Bisac::POA.new msg.(data) yield msg data = [] end end # if we've got to the end of the file, and haven't hit a footer line yet, the file # is probably malformed. Call build_message anyway, and let it detect any errors if data.size > 0 msg = Bisac::POA.new msg.(data) yield msg end end |
Instance Method Details
#build_message(data) ⇒ Object
Populate the current object with the message contained in data
An array of lines making a complete, single POA message
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/bisac/poa.rb', line 163 def (data) raise Bisac::InvalidFileError, 'File appears to be too short' unless data.size >= 3 raise Bisac::InvalidFileError, 'Missing header information' unless data[0][0,2].eql?("02") raise Bisac::InvalidFileError, 'Missing footer information' unless data[-1][0,2].eql?("91") data.each do |line| # ensure each line is at least 80 chars long line = line.ljust(80) case line[0,2] when "02" # file header self.source_san = line[7,7].strip self.source_suffix = line[14,5].strip self.source_name = line[19,13].strip self.date = line[32,6].strip self.filename = line[38,22].strip self.format_version = line[60,3].strip self.destination_san = line[63,7].strip self.destination_suffix = line[70,5].strip self.ack_type = line[75,1].strip when "11" # poa header self.supplier_poa_number = line[7,13].strip self.po_number = line[20,13].strip self.customer_san = line[33,7].strip self.customer_suffix = line[40,5].strip self.supplier_san = line[45,7].strip self.supplier_suffix = line[52,5].strip self.poa_date = line[57,6].strip self.currency = line[63,3].strip self.po_date = line[66,6].strip self.po_cancel_date = line[72,6].strip self.po_type = line[78,2].strip when "40" # line item item = Bisac::POALineItem.load_from_string(line) self.items << item when "41" when "42" when "59" # poa footer # check the built objects match the file when "91" # file footer # check the built objects match the file end end self end |
#to_s ⇒ Object
86 87 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/bisac/poa.rb', line 86 def to_s lines = [] # file header line = " " * 80 line[0,2] = "00" # line type line[2,5] = "00001" # line counter line[7,7] = pad_trunc(@source_san, 7) line[14,5] = pad_trunc(@source_suffix, 5) line[19,13] = pad_trunc(@source_name, 13) line[32,6] = pad_trunc(@date, 6) line[38,22] = pad_trunc(@filename, 22) line[60,3] = pad_trunc(@format_version, 3) line[63,7] = pad_trunc(@destination_san, 7) line[70,5] = pad_trunc(@destination_suffix, 5) lines << line # po header lines << "" lines.last << "10" lines.last << "00002" # line counter lines.last << " " lines.last << @po_number.to_s.ljust(11, " ") lines.last << " " # TODO lines.last << pad_trunc(@source_san, 7) lines.last << pad_trunc("",5) # TODO lines.last << pad_trunc(@destination_san, 7) lines.last << pad_trunc("",5) # TODO lines.last << pad_trunc(@date, 6) lines.last << pad_trunc(@cancellation_date,6) lines.last << yes_no(@backorder) lines.last << pad_trunc(@do_not_exceed_action,1) lines.last << pad_trunc(@do_not_exceed_amount,7) lines.last << pad_trunc(@invoice_copies,2) lines.last << yes_no(@special_instructions) lines.last << pad_trunc("",5) # TODO lines.last << pad_trunc(@do_not_ship_before,6) sequence = 3 @items.each_with_index do |item, idx| item.line_item_number = idx + 1 item.sequence_number = sequence lines += item.to_s.split("\n") sequence += 3 end # PO control line = " " * 80 line[0,2] = "50" line[2,5] = (lines.size + 1).to_s.rjust(5,"0") # line counter line[8,12] = @po_number.to_s.ljust(13, " ") line[20,5] = "00001" # number of POs in file line[25,10] = @items.size.to_s.rjust(10,"0") line[35,10] = total_qty.to_s.rjust(10,"0") lines << line # file trailer line = " " * 80 line[0,2] = "90" line[2,5] = (lines.size+1).to_s.rjust(5,"0") # line counter line[7,20] = @items.size.to_s.rjust(13,"0") line[20,5] = "00001" # total '10' (PO) records line[25,10] = total_qty.to_s.rjust(10,"0") line[35,5] = "00001" # number of '00'-'09' records line[40,5] = "00001" # number of '10'-'19' records line[55,5] = (@items.size * 3).to_s.rjust(5,"0") # number of '40'-'49' records line[60,5] = "00000" # number of '50'-'59' records line[45,5] = "00000" # number of '60'-'69' records lines << line lines.join("\n") end |
#total_qty ⇒ Object
82 83 84 |
# File 'lib/bisac/poa.rb', line 82 def total_qty @items.collect { |i| i.qty }.inject { |sum, x| sum ? sum+x : x} end |