Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
card#parse accepts tracks without names for track 2 parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Balsdon committed Dec 16, 2014
1 parent 95f75af commit 096015f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/magnet/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def parse(track_data, parser = Parser.new(:auto))
attributes = parser.parse(track_data)
position1, position2, position3 = (attributes[:service_code] || "").scan(/\d/).map(&:to_i)
year, month = (attributes[:expiration] || "").scan(/\d\d/).map(&:to_i)
title, first_name, initial, last_name = parse_name(attributes[:name].rstrip)
title, first_name, initial, last_name = parse_name(attributes[:name].rstrip) if attributes[:name]

card = new
card.allowed_services = hash_lookup(ALLOWED_SERVICES, position3)
Expand Down
2 changes: 1 addition & 1 deletion lib/magnet/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Magnet
VERSION = "1.5.0"
VERSION = "1.5.1"
end
47 changes: 47 additions & 0 deletions test/magnet/card_parser_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "test_helper"

describe Magnet::Card do
describe "Parse" do
before do
@parser = Magnet::Parser.new(:auto)
end

it "track 2 should work" do
track_data = ";4716916000001234=1809901123?"
card = Magnet::Card.parse(track_data, @parser)
assert_equal "4716916000001234", card.number
assert_equal :no_restrictions, card.allowed_services
assert_equal :normal, card.authorization_processing
assert_equal "123", card.discretionary_data
assert_equal 9, card.expiration_month
assert_equal 18, card.expiration_year
assert_equal :test, card.interchange
assert_nil card.first_name
assert_nil card.format
assert_nil card.initial
assert_nil card.last_name
assert_nil card.pin_requirements
assert_nil card.technology
assert_nil card.title
end

it "track 1 should work" do
track_data = "%B5452300551227189^HOGAN/PAUL ^08043210000000725000000?"
card = Magnet::Card.parse(track_data, @parser)
assert_equal "5452300551227189", card.number
assert_equal :no_restrictions, card.allowed_services
assert_equal :by_issuer, card.authorization_processing
assert_equal "0000000725000000", card.discretionary_data
assert_equal 4, card.expiration_month
assert_equal 8, card.expiration_year
assert_nil card.interchange
assert_equal "PAUL", card.first_name
assert_equal :bank, card.format
assert_nil card.initial
assert_equal "HOGAN", card.last_name
assert_nil card.pin_requirements
assert_nil card.technology
assert_nil card.title
end
end
end
59 changes: 32 additions & 27 deletions test/magnet/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,6 @@
@parser = Magnet::Parser.new(1)
end

it "should parse track 2 data sample #1" do
attributes = @parser.parse(";4716916000001234=1809901123?")

assert_equal "4716916000001234", attributes[:pan]
assert_equal "1809", attributes[:expiration]
assert_equal "901", attributes[:service_code]
assert_equal "123", attributes[:discretionary_data]
end

it "should parse track 2 data sample #2" do
attributes = @parser.parse(";5301250070000191=08051010912345678901?")

assert_equal "5301250070000191", attributes[:pan]
assert_equal "0805", attributes[:expiration]
assert_equal "101", attributes[:service_code]
assert_equal "0912345678901", attributes[:discretionary_data]
end

it "should parse track 2 data sample #3" do
attributes = @parser.parse(";3540599999991047=080501234567?")

assert_equal "3540599999991047", attributes[:pan]
assert_equal "0805", attributes[:expiration]
assert_equal "012", attributes[:service_code]
assert_equal "34567", attributes[:discretionary_data]
end

it "should parse sample #1" do
attributes = @parser.parse("%B6011898748579348^DOE/ JOHN ^37829821000123456789?")

Expand Down Expand Up @@ -222,4 +195,36 @@
assert_equal "ALISON MAYNE/B", attributes[:name]
end
end

describe "Track 2" do
before do
@parser = Magnet::Parser.new(2)
end
it "should parse track 2 data sample #1" do
attributes = @parser.parse(";4716916000001234=1809901123?")

assert_equal "4716916000001234", attributes[:pan]
assert_equal "1809", attributes[:expiration]
assert_equal "901", attributes[:service_code]
assert_equal "123", attributes[:discretionary_data]
end

it "should parse track 2 data sample #2" do
attributes = @parser.parse(";5301250070000191=08051010912345678901?")

assert_equal "5301250070000191", attributes[:pan]
assert_equal "0805", attributes[:expiration]
assert_equal "101", attributes[:service_code]
assert_equal "0912345678901", attributes[:discretionary_data]
end

it "should parse track 2 data sample #3" do
attributes = @parser.parse(";3540599999991047=080501234567?")

assert_equal "3540599999991047", attributes[:pan]
assert_equal "0805", attributes[:expiration]
assert_equal "012", attributes[:service_code]
assert_equal "34567", attributes[:discretionary_data]
end
end
end

0 comments on commit 096015f

Please sign in to comment.