Class: ISO8583::Codec

Inherits:
Object
  • Object
show all
Defined in:
lib/iso8583/codec.rb

Overview

Codec provides functionality to encode and decode values, codecs are used internally by Field instances in order to do character conversions and checking for proper values. Although they are used internally, you will probably need to write your own Codec sooner or later. The codecs used by Field instances are typically instances of Codec, it may or may not be usefull to subclass Codec.

Say, for example, a text field needs to be encoded in EBCDIC in the message, this is how a corresponding codec would be constructed:

EBCDIC_Codec = Codec.new
EBCDIC_Codec.encoder = lambda {|ascii_str|
   raise ISO8583Exception.new("String (#{ascii_str})not valid!") unless =~ /someregexp/
   ascii2ebcdic ascii_str  # implementing ascii_str is left as an excercise
}
EBCDIC_Codec.decode = lambda {|ebcdic_str|
   # you may or may not want to raise exceptions at this point ....
   # strip removes any padding...
   ebcdic2ascii(ebcdic_str).strip
}

This instance of Codec would then be used be the corresponding Field encoder/decoder, which may look similar to this:

EBCDIC         = Field.new
EBCDIC.codec   = EBCDIC_Codec
EBCDIC.padding = PADDING_LEFT_JUSTIFIED_SPACES

Notice there is a bit of inconsistancy: the padding is added by the field, but removed by the codec. I would like to find a better solution to this…

See also: Field, files/lib/fields_rb.html

The following codecs are already implemented:

ASCII_Number

encodes either a Number or String representation of a number to the ASCII represenation of the number, decodes ASCII numerals to a number

A_Codec

passes through ASCII string checking they conform to [A-Za-z] during encoding, no validity check during decoding.

AN_Codec

passes through ASCII string checking they conform to [A-Za-z0-9] during encoding, no validity check during decoding.

ANP_Codec

passes through ASCII string checking they conform to [A-Za-z0-9 ] during encoding, no validity check during decoding.

ANS_Codec

passes through ASCII string checking they conform to [x20-x7E] during encoding, no validity check during decoding.

Null_Codec

passes anything along untouched.

Track2

rudimentary check that string conforms to Track2

MMDDhhmmssCodec

encodes Time, Datetime or String to the described date format, checking that it is a valid date. Decodes to a DateTime instance, decoding and encoding perform validity checks!

MMDDCodec

encodes Time, Datetime or String to the described date format, checking

that it is a valid date. Decodes to a DateTime instance, decoding and 
encoding perform validity checks!
YYMMDDhhmmssCodec

encodes Time, Datetime or String to the described date format, checking that it is a valid date. Decodes to a DateTime instance, decoding and encoding perform validity checks!

YYMMCodec

encodes Time, Datetime or String to the described date format (exp date), checking that it is a valid date. Decodes to a DateTime instance, decoding and encoding perform validity checks!

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#decoderObject

Returns the value of attribute decoder.



73
74
75
# File 'lib/iso8583/codec.rb', line 73

def decoder
  @decoder
end

#encoderObject

Returns the value of attribute encoder.



72
73
74
# File 'lib/iso8583/codec.rb', line 72

def encoder
  @encoder
end

Instance Method Details

#decode(raw) ⇒ Object



75
76
77
# File 'lib/iso8583/codec.rb', line 75

def decode(raw)
  decoder.call(raw)
end

#encode(value) ⇒ Object

length is either a fixnum or a lenth encoder.



80
81
82
# File 'lib/iso8583/codec.rb', line 80

def encode(value)
  encoder.call(value)
end