Skip to content

Commit

Permalink
Pass encoding in options hash
Browse files Browse the repository at this point in the history
  • Loading branch information
szajbus committed Aug 23, 2017
1 parent 9af9e3f commit b11ff1b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lib/saxy/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ class Parser < Nokogiri::XML::SAX::Document
# Parser context
attr_reader :context

def initialize(object, object_tag, encoding=nil)
@object, @object_tag = object, object_tag
# Parser options
attr_reader :options

def initialize(object, object_tag, options={})
@object, @object_tag, @options = object, object_tag, options
@tags, @elements = [], []
@encoding = encoding
end

def start_element(tag, attributes=[])
Expand Down Expand Up @@ -71,7 +73,7 @@ def each(&blk)

@callback = blk

args = [self, @encoding].compact
args = [self, options[:encoding]].compact

parser = Nokogiri::XML::SAX::Parser.new(*args)
context_blk = proc { |context| @context = context }
Expand Down
6 changes: 3 additions & 3 deletions spec/saxy/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
describe Saxy::Parser do
include FixturesHelper

let(:parser) { Saxy::Parser.new(fixture_file("webstore.xml"), "product", "UTF-8") }
let(:invalid_parser) { Saxy::Parser.new(fixture_file("invalid.xml"), "product", "UTF-8") }
let(:parser) { Saxy::Parser.new(fixture_file("webstore.xml"), "product") }
let(:invalid_parser) { Saxy::Parser.new(fixture_file("invalid.xml"), "product") }
let(:file_io) { File.new(fixture_file("webstore.xml")) }
let(:io_like) { IOLike.new(file_io) }

Expand All @@ -20,7 +20,7 @@
end

it "should accept optional force-encoding" do
parser = Saxy::Parser.new(file_io, "product", 'UTF-8')
parser = Saxy::Parser.new(file_io, "product", encoding: "UTF-8")
expect(Nokogiri::XML::SAX::Parser).to receive(:new).with(parser, "UTF-8").and_call_original
expect(parser.each.to_a.size).to eq(2)
end
Expand Down

0 comments on commit b11ff1b

Please sign in to comment.