class Device::Magnetic

Constants

HARDWARE_NOT_READ
HARDWARE_RET_OK
HARDWARE_SUCCESSFUL_READ
STATUS_CLOSE
STATUS_OPEN
STATUS_OPEN_FAIL
STATUS_READ_TRACKS
STATUS_SUCCESSFUL_READ

Attributes

status[R]
track1[R]
track2[R]
track3[R]
tracks[R]

Public Class Methods

adapter() click to toggle source
# File lib/device/magnetic.rb, line 3
def self.adapter
  Device.adapter::Magnetic
end
new() click to toggle source
# File lib/device/magnetic.rb, line 33
def initialize
  @status = STATUS_CLOSE
  @open = self.start
end
read_card(timeout) click to toggle source
# File lib/device/magnetic.rb, line 11
def self.read_card(timeout)
  time = Time.now + (timeout.to_f / 1000.0)
  magnetic = self.new
  loop do
    break if magnetic.swiped? || time <= Time.now
  end
  magnetic.tracks
ensure
  magnetic.close if magnetic
end

Public Instance Methods

adapter() click to toggle source
# File lib/device/magnetic.rb, line 7
def adapter
  Device.adapter::Magnetic
end
bin?(value) click to toggle source
# File lib/device/magnetic.rb, line 77
def bin?(value)
  return false if value.to_s.empty?
  tracks if self.read?

  digits = extract_digits(value)
  if value.is_a?(Range) && ! digits.empty? && digits.integer?
    value.include? digits.to_f
  else
    digits.to_s == value.to_s
  end
end
close() click to toggle source
# File lib/device/magnetic.rb, line 64
def close
  adapter.close
end
open?() click to toggle source
# File lib/device/magnetic.rb, line 73
def open?
  @open
end
read() click to toggle source
# File lib/device/magnetic.rb, line 60
def read
  adapter.read
end
read?() click to toggle source
# File lib/device/magnetic.rb, line 56
def read?
  @status == STATUS_SUCCESSFUL_READ
end
start() click to toggle source
# File lib/device/magnetic.rb, line 38
def start
  if self.adapter.open == HARDWARE_RET_OK
    @status = STATUS_OPEN
    true
  else
    @status = STATUS_OPEN_FAIL
    false
  end
end
swiped?() click to toggle source
# File lib/device/magnetic.rb, line 48
def swiped?
  if self.read == HARDWARE_SUCCESSFUL_READ
    @status = STATUS_SUCCESSFUL_READ
    return true
  end
  false
end

Private Instance Methods

extract_digits(value) click to toggle source
# File lib/device/magnetic.rb, line 91
def extract_digits(value)
  if value.is_a?(Range)
    total = value.first.to_s.size
  else #String
    total = value.size
  end

  return "" if total == 0
  total -= 1

  track2.to_s[0..total]
end
read_tracks() click to toggle source
# File lib/device/magnetic.rb, line 104
def read_tracks
  @tracks = adapter.tracks
  @track1 = @tracks[:track1]
  @track2 = @tracks[:track2]
  @track3 = @tracks[:track3]

  @status = STATUS_READ_TRACKS
  @tracks
end