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.



20
21
22
23
24
25
# File 'lib/iso8583/bitmap.rb', line 20

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.



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

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.



37
38
39
# File 'lib/iso8583/bitmap.rb', line 37

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.



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

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.



32
33
34
# File 'lib/iso8583/bitmap.rb', line 32

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)


27
28
29
# File 'lib/iso8583/bitmap.rb', line 27

def hex_bitmap?
  !!@hex_bitmap
end

#set(i) ⇒ Object

Sets bit #i



53
54
55
# File 'lib/iso8583/bitmap.rb', line 53

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

#to_bytesObject Also known as: to_b

Generate the bytes representing this bitmap.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/iso8583/bitmap.rb', line 63

def to_bytes
  if RUBY_ENGINE == 'mruby'
    # 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
  else
    [to_s].pack("B*").force_encoding('UTF-8')
  end
end

#to_hexObject



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

def to_hex
  value = self.to_s.to_i(2).to_s(16).upcase
  if value.respond_to? :force_encoding
    value.force_encoding('UTF-8')
  else
    value
  end
end

#to_sObject

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



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/iso8583/bitmap.rb', line 97

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



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

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