Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input data string can be any encoding #2

Merged
merged 1 commit into from Nov 29, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Expand Up @@ -23,9 +23,9 @@ Or install it yourself as:
Use the convenience method `BankgiroInbetalningar.parse` to parse a file:

```ruby
res = BankgiroInbetalningar.parse('BgMaxfil4.txt')
res = BankgiroInbetalningar.parse("BgMaxfil4.txt")
# Or
data = File.read("BgMaxfil4.txt")
data = File.read("BgMaxfil4.txt").force_encoding("ISO-8859-1")
res = BankgiroInbetalningar.parse_data(data)

raise "oops" unless res.valid?
Expand All @@ -44,8 +44,11 @@ res.payments.each do |p|
end
```

See the specs for more details. Note that all text is in UTF-8, as it should be,
and not in ISO-8859-1 as Bankgirot prefers. It is the 21st century.
See the specs for more details.

Files are expected to be ISO-8859-1 (as Bankgirot prefers), but data strings
can be in any encoding, as long as `String#encoding` is correct. The library
returns UTF-8. It *is* the 21st century.

## Todo / Missing features

Expand Down
2 changes: 1 addition & 1 deletion lib/bankgiro_inbetalningar.rb
Expand Up @@ -4,7 +4,7 @@

module BankgiroInbetalningar
def self.parse(filename)
data = File.read(filename)
data = File.read(filename).force_encoding("ISO-8859-1")
parse_data(data)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bankgiro_inbetalningar/parser.rb
Expand Up @@ -5,7 +5,7 @@ class Parser
attr_accessor :result

def initialize(data)
@raw_data ||= data.encode('utf-8', 'iso-8859-1')
@raw_data ||= data.encode("UTF-8")
end

def run
Expand Down
12 changes: 10 additions & 2 deletions spec/bankgiro_inbetalningar/parser_spec.rb
Expand Up @@ -4,10 +4,18 @@
module BankgiroInbetalningar
describe Parser do
context "parsing sample file 4" do
let(:data) { File.read(fixture_path('BgMaxfil4.txt')) }
let(:data) { data_from_file('BgMaxfil4.txt') }
let(:parser) { Parser.new(data) }
let(:result) { parser.run ; parser.result }

context "with non Latin-1 data" do
let(:data) { data_from_file('BgMaxfil4.txt').encode("UTF-8") }

it "handles that fine" do
result.payments[1].payer.name.should include "Olles färg"
end
end

it "returns valid results" do
result.should be_valid
end
Expand Down Expand Up @@ -74,7 +82,7 @@ module BankgiroInbetalningar

end
context "parsing a broken sample file 4" do
let(:data) { File.read(fixture_path('BgMaxfil4_broken.txt')) }
let(:data) { data_from_file('BgMaxfil4_broken.txt') }
let(:parser) { Parser.new(data) }
let(:result) { parser.run ; parser.result }

Expand Down
2 changes: 1 addition & 1 deletion spec/bankgiro_inbetalningar_spec.rb
Expand Up @@ -18,7 +18,7 @@

describe BankgiroInbetalningar, ".parse_data" do
context "parsing a minimal file" do
let(:data) { File.read(fixture_path('minimal.txt')) }
let(:data) { data_from_file('minimal.txt') }
subject { BankgiroInbetalningar.parse_data(data) }

it "finds the timestamp" do
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
@@ -1,6 +1,10 @@
$: << File.expand_path('../../lib', __FILE__)
require 'bankgiro_inbetalningar'

def data_from_file(name)
File.read(fixture_path(name)).force_encoding("ISO-8859-1")
end

def fixture_path(name)
File.expand_path("../fixtures/#{name}", __FILE__)
end