Class: Magtek::CardReader

Inherits:
Object
  • Object
show all
Defined in:
lib/magtek_card_reader/reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(product_id = nil) ⇒ CardReader

Returns a new instance of CardReader.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/magtek_card_reader/reader.rb', line 5

def initialize(product_id = nil)
  product_id = Magtek.available_devices.first unless product_id
  @usb = LIBUSB::Context.new
  @device = @usb.devices(:idVendor => 0x0801, :idProduct => product_id).first
  fail "Device not found" unless @device
  @interface = @device.interfaces.first
  fail "Interface not found" unless @interface
  @endpoint = @interface.endpoints.first
  fail "Endpoint not found" unless @endpoint
  @open = false
end

Instance Method Details

#closeObject



31
32
33
34
35
36
# File 'lib/magtek_card_reader/reader.rb', line 31

def close
  return true unless @open 
  @handle.release_interface(0)
  @handle.close 
  true
end

#openObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/magtek_card_reader/reader.rb', line 17

def open
  close
  begin 
    @handle = @device.open
    @handle.detach_kernel_driver(0) if @handle.kernel_driver_active?(0)
    @handle.set_configuration(1)  
    @handle.claim_interface(0) 
    @open = true
    return true 
  rescue
    return false
  end
end

#read(options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/magtek_card_reader/reader.rb', line 38

def read(options)
  timeout = options[:timeout] || 5000
  
  @interrupt_transfer = LIBUSB::InterruptTransfer.new
  @interrupt_transfer.dev_handle = @handle
  @interrupt_transfer.endpoint = @endpoint
  @interrupt_transfer.alloc_buffer(337)
  @interrupt_transfer.timeout = timeout
  begin
    @interrupt_transfer.submit_and_wait!
  rescue => e
    @handle.reset_device unless e.message == "error TRANSFER_TIMED_OUT"
    return false
  end
  return false unless @interrupt_transfer.actual_length==337
  
  match = /B([0-9]{16})\^(.*)\^([0-9]{2})([0-9]{2})/.match(@interrupt_transfer.actual_buffer)
  return false unless match
  
  number, name, exp_year, exp_month = match.captures
  return [true, number, name, exp_year, exp_month]
end