Class: ISO8583::Bitmap

Inherits:
Object
  • Object
show all
Includes:
DaFunk::Helper
Defined in:
lib/iso8583/bitmap.rb

Overview

This class constructs an object for handling bitmaps with which ISO8583 messages typically begin. Bitmaps are either 8 or 16 bytes long, an extended length bitmap is indicated by the first bit being set. In all likelyhood, you won’t be using this class much, it’s used transparently by the Message class.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, hex_bitmap = false) ⇒ Bitmap

create a new Bitmap object. In case an iso message is passed in, that messages bitmap will be parsed. If not, this initializes and empty bitmap.



18
19
20
21
22
23
# File 'lib/iso8583/bitmap.rb', line 18

def initialize(message = nil, hex_bitmap=false)
  @bmp        = Array.new(128, false)
  @hex_bitmap = hex_bitmap

  message ? initialize_from_message(message) : nil
end

Class Method Details

.parse(str, hex_bitmap = false) ⇒ Object

Parse the bytes in string and return the Bitmap and bytes remaining in str after the bitmap is taken away.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/iso8583/bitmap.rb', line 131

def parse(str, hex_bitmap = false)
  bmp  = Bitmap.new(str, hex_bitmap)

  rest = if bmp.hex_bitmap?
           bmp[1] ? str[32, str.length] : str[16, str.length]
         else
           bmp[1] ? str[16, str.length] : str[8, str.length]
         end

  [ bmp, rest ]
end

Instance Method Details

#[](i) ⇒ Object

Returns whether the bit is set or not.



35
36
37
# File 'lib/iso8583/bitmap.rb', line 35

def [](i)
  @bmp[i-1]
end

#[]=(i, value) ⇒ Object

Set the bit to the indicated value. Only true sets the bit, any other value unsets it.



41
42
43
44
45
46
47
48
# File 'lib/iso8583/bitmap.rb', line 41

def []=(i, value)
  if i > 128
    raise ISO8583Exception.new("Bits > 128 are not permitted.")
  elsif i < 2
    raise ISO8583Exception.new("Bits < 2 are not permitted (continutation bit is set automatically)")
  end
  @bmp[i-1] = (value == true)
end

#eachObject

yield once with the number of each set field.



30
31
32
# File 'lib/iso8583/bitmap.rb', line 30

def each #:yields: each bit set in the bitmap except the first bit.
  @bmp[1..-1].each_with_index {|set, i| yield i+2 if set}
end

#hex_bitmap?Boolean



25
26
27
# File 'lib/iso8583/bitmap.rb', line 25

def hex_bitmap?
  !!@hex_bitmap
end

#set(i) ⇒ Object

Sets bit #i



51
52
53
# File 'lib/iso8583/bitmap.rb', line 51

def set(i)
  self[i] = true
end

#to_bytesObject Also known as: to_b

Generate the bytes representing this bitmap.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/iso8583/bitmap.rb', line 61

def to_bytes
  # Convert binary to hex, by slicing the binary in 4 bytes chuncks
  bitmap_hex = ""
  str = ""
  self.to_s.chars.reverse.each_with_index do |ch, i|
    str << ch
    next if i == 0
    if (i+1) % 4 == 0
      bitmap_hex << str.reverse.to_i(2).to_s(16)
      str = ""
    end
  end
  unless str.empty?
    bitmap_hex << str.reverse.to_i(2).to_s(16)
  end
  bitmap_hex.reverse.upcase
end

#to_hexObject



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

def to_hex
  self.to_s.to_i(2).to_s(16).upcase.unpack("H*").first
end

#to_sObject

Generate a String representation of this bitmap in the form: 01001100110000011010110110010100100110011000001101011011001010



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/iso8583/bitmap.rb', line 86

def to_s
  #check whether any `high` bits are set
  ret           = (65..128).find {|bit| !!self[bit]}
  high, @bmp[0] = ret ? [128, true] : [64, false]

  str = ""
  1.upto(high) do|i|
    str << (self[i] ? '1' : '0')
  end

  str
end

#unset(i) ⇒ Object

Unsets bit #i



56
57
58
# File 'lib/iso8583/bitmap.rb', line 56

def unset(i)
  self[i] = false
end