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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, hex_bitmap = false, bitmap_size = 64, additional_bitmap = true) ⇒ 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.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/iso8583/bitmap.rb', line 46

def initialize(message = nil, hex_bitmap=false, bitmap_size = 64, additional_bitmap = true)
  raise ISO8583Exception.new "wrong bitmap_size: #{bitmap_size}" if bitmap_size % 8 != 0

  @bitmap_size           = bitmap_size
  @bmp                   = Array.new(bitmap_size, false)
  @hex_bitmap            = hex_bitmap
  @additional_bitmap = additional_bitmap
  @bitmaps               = 1

  message ? initialize_from_message(message) : nil
end

Instance Attribute Details

#additional_bitmapObject (readonly)

additional_bitmap defines if the bit 1 (left to right) indicates the presence of another bitmap just after the current one



39
40
41
# File 'lib/iso8583/bitmap.rb', line 39

def additional_bitmap
  @additional_bitmap
end

#bitmap_sizeObject (readonly)

bitmap_size defines the size in bits of bitmap. It has to be multiple of 8 (a byte of 8 bits)



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

def bitmap_size
  @bitmap_size
end

#bitmapsObject (readonly)

Returns the value of attribute bitmaps.



41
42
43
# File 'lib/iso8583/bitmap.rb', line 41

def bitmaps
  @bitmaps
end

Class Method Details

.parse(str, hex_bitmap = false, bitmap_size = 64) ⇒ Object

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



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/iso8583/bitmap.rb', line 21

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

  rest = if bmp.hex_bitmap?
           str[bmp.size_in_bytes_hex, str.length]
         else
           str[bmp.size_in_bytes, str.length]
         end

  [ bmp, rest ]
end

Instance Method Details

#[](i) ⇒ Object

Returns whether the bit is set or not.



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

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

#additional_bitmap?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/iso8583/bitmap.rb', line 58

def additional_bitmap?
  !!@additional_bitmap
end

#eachObject

yield once with the number of each set field.



67
68
69
# File 'lib/iso8583/bitmap.rb', line 67

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

Returns:

  • (Boolean)


62
63
64
# File 'lib/iso8583/bitmap.rb', line 62

def hex_bitmap?
  !!@hex_bitmap
end

#set(i) ⇒ Object

Sets bit #i



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/iso8583/bitmap.rb', line 77

def set(i)
  if additional_bitmap?
    raise ISO8583Exception, "field #{i} shouldn't be set (continuation bit is set automatically)" if i % bitmap_size == 1
  end

  if i > bitmap_size
    raise ISO8583Exception, "can't set field #{i}, bitmap_size == #{bitmap_size}" unless additional_bitmap?

    quo = i / bitmap_size
    rem = i % bitmap_size
    @bitmaps = rem > 0 ? quo + 1 : quo
    new_bmp = Array.new(@bitmaps * bitmap_size, false)
    @bmp.each_with_index { |v, idx| new_bmp[idx] = v }
    0.upto(@bitmaps - 2) do |pos|
      new_bmp[pos * bitmap_size] = true
    end
    @bmp = new_bmp
  end
  self[i] = true
end

#size_in_bitsObject



98
99
100
# File 'lib/iso8583/bitmap.rb', line 98

def size_in_bits
  bitmaps * bitmap_size
end

#size_in_bytesObject



102
103
104
# File 'lib/iso8583/bitmap.rb', line 102

def size_in_bytes
  size_in_bits / 8
end

#size_in_bytes_hexObject



106
107
108
# File 'lib/iso8583/bitmap.rb', line 106

def size_in_bytes_hex
  size_in_bytes * 2
end

#to_bytesObject Also known as: to_b

Generate the bytes representing this bitmap.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/iso8583/bitmap.rb', line 116

def to_bytes
  if RUBY_ENGINE == 'mruby'
    # Convert binary to hex, by slicing the binary in 4 bytes chuncks
    bitmap_hex = ""
    str = String.new("", encoding: 'ASCII-8BIT')
    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 = String.new("", encoding: 'ASCII-8BIT')
      end
    end
    unless str.empty?
      bitmap_hex << str.reverse.to_i(2).to_s(16)
    end
    bitmap_hex.reverse.upcase
  else
    [to_s].pack("B*")
  end
end

#to_hexObject



139
140
141
# File 'lib/iso8583/bitmap.rb', line 139

def to_hex
  self.to_s.to_i(2).to_s(16).upcase.ljust(size_in_bytes_hex, '0')
end

#to_sObject

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



145
146
147
148
149
150
151
152
# File 'lib/iso8583/bitmap.rb', line 145

def to_s
  str = String.new("", encoding: "ASCII-8BIT")
  1.upto(size_in_bits) do |i|
    str << (self[i] ? '1' : '0')
  end

  str
end

#unset(i) ⇒ Object

Unsets bit #i



111
112
113
# File 'lib/iso8583/bitmap.rb', line 111

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