Class: Device::Magnetic

Inherits:
Object
  • Object
show all
Defined in:
lib/device/magnetic.rb

Constant Summary collapse

HARDWARE_RET_OK =
0
HARDWARE_SUCCESSFUL_READ =
1
HARDWARE_NOT_READ =
0
STATUS_SUCCESSFUL_READ =
:success
STATUS_READ_TRACKS =
:read
STATUS_CLOSE =
:close
STATUS_OPEN =
:open
STATUS_OPEN_FAIL =
:open_fail

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMagnetic

Returns a new instance of Magnetic.



33
34
35
36
# File 'lib/device/magnetic.rb', line 33

def initialize
  @status = STATUS_CLOSE
  @open = self.start
end

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



31
32
33
# File 'lib/device/magnetic.rb', line 31

def status
  @status
end

#track1Object (readonly)

Returns the value of attribute track1.



31
32
33
# File 'lib/device/magnetic.rb', line 31

def track1
  @track1
end

#track2Object (readonly)

Returns the value of attribute track2.



31
32
33
# File 'lib/device/magnetic.rb', line 31

def track2
  @track2
end

#track3Object (readonly)

Returns the value of attribute track3.



31
32
33
# File 'lib/device/magnetic.rb', line 31

def track3
  @track3
end

#tracksObject (readonly)

Returns the value of attribute tracks.



31
32
33
# File 'lib/device/magnetic.rb', line 31

def tracks
  @tracks
end

Class Method Details

.adapterObject



3
4
5
# File 'lib/device/magnetic.rb', line 3

def self.adapter
  Device.adapter::Magnetic
end

.read_card(timeout) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#adapterObject



7
8
9
# File 'lib/device/magnetic.rb', line 7

def adapter
  Device.adapter::Magnetic
end

#bin?(value) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
# 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

#closeObject



64
65
66
# File 'lib/device/magnetic.rb', line 64

def close
  adapter.close
end

#open?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/device/magnetic.rb', line 73

def open?
  @open
end

#readObject



60
61
62
# File 'lib/device/magnetic.rb', line 60

def read
  adapter.read
end

#read?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/device/magnetic.rb', line 56

def read?
  @status == STATUS_SUCCESSFUL_READ
end

#startObject



38
39
40
41
42
43
44
45
46
# 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?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/device/magnetic.rb', line 48

def swiped?
  if self.read == HARDWARE_SUCCESSFUL_READ
    @status = STATUS_SUCCESSFUL_READ
    return true
  end
  false
end