sax-machine 0.2.0.rc1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +7 -4
- data/Gemfile +0 -1
- data/lib/sax-machine/sax_attribute_config.rb +3 -3
- data/lib/sax-machine/sax_collection_config.rb +7 -15
- data/lib/sax-machine/sax_document.rb +12 -1
- data/lib/sax-machine/sax_element_config.rb +5 -13
- data/lib/sax-machine/sax_handler.rb +2 -1
- data/lib/sax-machine/version.rb +1 -1
- data/sax-machine.gemspec +5 -4
- data/spec/sax-machine/atom.xml +2 -2
- data/spec/sax-machine/sax_document_spec.rb +43 -2
- metadata +12 -14
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -20,11 +20,11 @@ module SAXMachine
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def value_from_attrs(attrs)
|
23
|
-
attrs.
|
23
|
+
attrs.fetch(@name, nil)
|
24
24
|
end
|
25
25
|
|
26
26
|
def attrs_match?(attrs)
|
27
|
-
attrs.
|
27
|
+
attrs.key?(@name) || attrs.value?(@name)
|
28
28
|
end
|
29
29
|
|
30
30
|
def has_value_and_attrs_match?(attrs)
|
@@ -37,4 +37,4 @@ module SAXMachine
|
|
37
37
|
end
|
38
38
|
|
39
39
|
end
|
40
|
-
end
|
40
|
+
end
|
@@ -5,16 +5,10 @@ module SAXMachine
|
|
5
5
|
attr_reader :name
|
6
6
|
|
7
7
|
def initialize(name, options)
|
8
|
-
@name
|
9
|
-
@class
|
10
|
-
@as
|
11
|
-
|
12
|
-
if options.has_key?(:with)
|
13
|
-
# for faster comparisons later
|
14
|
-
@with = options[:with].to_a.flatten.collect {|o| o.to_s}
|
15
|
-
else
|
16
|
-
@with = nil
|
17
|
-
end
|
8
|
+
@name = name.to_s
|
9
|
+
@class = options[:class]
|
10
|
+
@as = options[:as].to_s
|
11
|
+
@with = options.fetch(:with, {})
|
18
12
|
end
|
19
13
|
|
20
14
|
def accessor
|
@@ -22,10 +16,8 @@ module SAXMachine
|
|
22
16
|
end
|
23
17
|
|
24
18
|
def attrs_match?(attrs)
|
25
|
-
|
26
|
-
|
27
|
-
else
|
28
|
-
true
|
19
|
+
@with.all? do |key, value|
|
20
|
+
value === attrs[key.to_s]
|
29
21
|
end
|
30
22
|
end
|
31
23
|
|
@@ -42,4 +34,4 @@ module SAXMachine
|
|
42
34
|
end
|
43
35
|
|
44
36
|
end
|
45
|
-
end
|
37
|
+
end
|
@@ -3,16 +3,27 @@ require "nokogiri"
|
|
3
3
|
module SAXMachine
|
4
4
|
|
5
5
|
def self.included(base)
|
6
|
+
base.send(:include, InstanceMethods)
|
6
7
|
base.extend ClassMethods
|
7
8
|
end
|
8
9
|
|
9
10
|
def parse(xml_text, on_error = nil, on_warning = nil)
|
10
11
|
sax_handler = SAXHandler.new(self, on_error, on_warning)
|
11
12
|
parser = Nokogiri::XML::SAX::Parser.new(sax_handler)
|
12
|
-
parser.parse(xml_text)
|
13
|
+
parser.parse(xml_text) do |ctx|
|
14
|
+
ctx.replace_entities = true
|
15
|
+
end
|
13
16
|
self
|
14
17
|
end
|
15
18
|
|
19
|
+
module InstanceMethods
|
20
|
+
def initialize(attributes = {})
|
21
|
+
attributes.each do |name, value|
|
22
|
+
send("#{name}=", value)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
16
27
|
module ClassMethods
|
17
28
|
|
18
29
|
def inherited(subclass)
|
@@ -6,14 +6,8 @@ module SAXMachine
|
|
6
6
|
|
7
7
|
def initialize(name, options)
|
8
8
|
@name = name.to_s
|
9
|
-
|
10
|
-
|
11
|
-
# for faster comparisons later
|
12
|
-
@with = options[:with].to_a.flatten.collect {|o| o.to_s}
|
13
|
-
else
|
14
|
-
@with = nil
|
15
|
-
end
|
16
|
-
|
9
|
+
@with = options.fetch(:with, {})
|
10
|
+
|
17
11
|
if options.has_key?(:value)
|
18
12
|
@value = options[:value].to_s
|
19
13
|
else
|
@@ -49,14 +43,12 @@ module SAXMachine
|
|
49
43
|
end
|
50
44
|
|
51
45
|
def value_from_attrs(attrs)
|
52
|
-
attrs.
|
46
|
+
attrs.fetch(@value, nil)
|
53
47
|
end
|
54
48
|
|
55
49
|
def attrs_match?(attrs)
|
56
|
-
|
57
|
-
|
58
|
-
else
|
59
|
-
true
|
50
|
+
@with.all? do |key, value|
|
51
|
+
value === attrs[key.to_s]
|
60
52
|
end
|
61
53
|
end
|
62
54
|
|
@@ -32,7 +32,6 @@ module SAXMachine
|
|
32
32
|
alias cdata_block characters
|
33
33
|
|
34
34
|
def start_element(name, attrs = [])
|
35
|
-
attrs.flatten!
|
36
35
|
|
37
36
|
name = normalize_name(name)
|
38
37
|
node = stack.last
|
@@ -41,6 +40,8 @@ module SAXMachine
|
|
41
40
|
sax_config = sax_config_for(object)
|
42
41
|
|
43
42
|
if sax_config
|
43
|
+
attrs = Hash[attrs]
|
44
|
+
|
44
45
|
if collection_config = sax_config.collection_config(name, attrs)
|
45
46
|
object = collection_config.data_class.new
|
46
47
|
sax_config = sax_config_for(object)
|
data/lib/sax-machine/version.rb
CHANGED
data/sax-machine.gemspec
CHANGED
@@ -6,18 +6,19 @@ Gem::Specification.new do |s|
|
|
6
6
|
s.version = SAXMachine::VERSION
|
7
7
|
|
8
8
|
s.authors = ["Paul Dix", "Julien Kirch", "Ezekiel Templin"]
|
9
|
-
s.date = Date.today
|
10
9
|
s.email = %q{paul@pauldix.net}
|
11
10
|
s.homepage = %q{http://github.com/pauldix/sax-machine}
|
12
11
|
|
13
12
|
s.summary = %q{Declarative SAX Parsing with Nokogiri}
|
14
13
|
|
14
|
+
s.license = %q{MIT}
|
15
|
+
|
15
16
|
s.files = `git ls-files`.split("\n")
|
16
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
18
|
s.require_paths = ["lib"]
|
18
19
|
|
19
20
|
s.platform = Gem::Platform::RUBY
|
20
21
|
|
21
|
-
s.add_dependency 'nokogiri', "~> 1.
|
22
|
-
s.add_development_dependency "rspec", "~> 2.
|
23
|
-
end
|
22
|
+
s.add_dependency 'nokogiri', "~> 1.6.0"
|
23
|
+
s.add_development_dependency "rspec", "~> 2.13.0"
|
24
|
+
end
|
data/spec/sax-machine/atom.xml
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
<generator uri="http://www.typepad.com/">TypePad</generator>
|
10
10
|
<link rel="self" href="http://feeds.feedburner.com/PaulDixExplainsNothing" type="application/atom+xml" /><entry>
|
11
11
|
<title>Marshal data too short error with ActiveRecord</title>
|
12
|
-
<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/PaulDixExplainsNothing/~3/383536354/marshal-data-to.html" />
|
12
|
+
<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/PaulDixExplainsNothing/~3/383536354/marshal-data-to.html?param1=1&param2=2" />
|
13
13
|
<link rel="replies" type="text/html" href="http://www.pauldix.net/2008/09/marshal-data-to.html" thr:count="2" thr:updated="2008-11-17T14:40:06-05:00" />
|
14
14
|
<id>tag:typepad.com,2003:post-55147740</id>
|
15
15
|
<published>2008-09-04T16:07:19-04:00</published>
|
@@ -38,7 +38,7 @@
|
|
38
38
|
</div><img src="http://feeds.feedburner.com/~r/PaulDixExplainsNothing/~4/383536354" height="1" width="1"/></content>
|
39
39
|
|
40
40
|
|
41
|
-
<feedburner:origLink>http://www.pauldix.net/2008/09/marshal-data-to.html</feedburner:origLink></entry>
|
41
|
+
<feedburner:origLink>http://www.pauldix.net/2008/09/marshal-data-to.html?param1=1&param2=2</feedburner:origLink></entry>
|
42
42
|
<entry>
|
43
43
|
<title>Serializing data speed comparison: Marshal vs. JSON vs. Eval vs. YAML</title>
|
44
44
|
<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/PaulDixExplainsNothing/~3/376401099/serializing-dat.html" />
|
@@ -10,6 +10,11 @@ describe "SAXMachine" do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
it "should provide mass assignment through initialize method" do
|
14
|
+
document = @klass.new(title: 'Title')
|
15
|
+
document.title.should == 'Title'
|
16
|
+
end
|
17
|
+
|
13
18
|
it "should provide an accessor" do
|
14
19
|
document = @klass.new
|
15
20
|
document.title = "Title"
|
@@ -54,7 +59,7 @@ describe "SAXMachine" do
|
|
54
59
|
element :date, :class => DateTime
|
55
60
|
end
|
56
61
|
@document = @klass.new
|
57
|
-
@document.date =
|
62
|
+
@document.date = Time.now.iso8601
|
58
63
|
end
|
59
64
|
it "should be available" do
|
60
65
|
@klass.data_class(:date).should == DateTime
|
@@ -306,6 +311,35 @@ describe "SAXMachine" do
|
|
306
311
|
document.second.should == "second match"
|
307
312
|
end
|
308
313
|
end
|
314
|
+
|
315
|
+
describe "with only one element as a regular expression" do
|
316
|
+
before :each do
|
317
|
+
@klass = Class.new do
|
318
|
+
include SAXMachine
|
319
|
+
element :link, :with => {:foo => /ar$/}
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
it "should save the text of an element that has matching attributes" do
|
324
|
+
document = @klass.parse("<link foo=\"bar\">match</link>")
|
325
|
+
document.link.should == "match"
|
326
|
+
end
|
327
|
+
|
328
|
+
it "should not save the text of an element that doesn't have matching attributes" do
|
329
|
+
document = @klass.parse("<link>no match</link>")
|
330
|
+
document.link.should be_nil
|
331
|
+
end
|
332
|
+
|
333
|
+
it "should save the text of an element that has matching attributes when it is the second of that type" do
|
334
|
+
document = @klass.parse("<xml><link>no match</link><link foo=\"bar\">match</link></xml>")
|
335
|
+
document.link.should == "match"
|
336
|
+
end
|
337
|
+
|
338
|
+
it "should save the text of an element that has matching attributes plus a few more" do
|
339
|
+
document = @klass.parse("<xml><link>no match</link><link asdf='jkl' foo='bar'>match</link>")
|
340
|
+
document.link.should == "match"
|
341
|
+
end
|
342
|
+
end
|
309
343
|
end # using the 'with' option
|
310
344
|
|
311
345
|
describe "using the 'value' option" do
|
@@ -429,7 +463,7 @@ describe "SAXMachine" do
|
|
429
463
|
@klass = Class.new do
|
430
464
|
include SAXMachine
|
431
465
|
elements :item, :as => :items, :with => {:type => 'Bar'}, :class => Bar
|
432
|
-
elements :item, :as => :items, :with => {:type =>
|
466
|
+
elements :item, :as => :items, :with => {:type => /Foo/}, :class => Foo
|
433
467
|
end
|
434
468
|
end
|
435
469
|
|
@@ -523,6 +557,7 @@ describe "SAXMachine" do
|
|
523
557
|
element :title
|
524
558
|
element :name, :as => :author
|
525
559
|
element "feedburner:origLink", :as => :url
|
560
|
+
element :link, :as => :alternate, :value => :href, :with => {:type => "text/html", :rel => "alternate"}
|
526
561
|
element :summary
|
527
562
|
element :content
|
528
563
|
element :published
|
@@ -541,6 +576,12 @@ describe "SAXMachine" do
|
|
541
576
|
f = Atom.parse(@xml)
|
542
577
|
f.url.should == "http://www.pauldix.net/"
|
543
578
|
end
|
579
|
+
|
580
|
+
it "should parse entry url" do
|
581
|
+
f = Atom.parse(@xml)
|
582
|
+
f.entries.first.url.should == "http://www.pauldix.net/2008/09/marshal-data-to.html?param1=1¶m2=2"
|
583
|
+
f.entries.first.alternate.should == "http://feeds.feedburner.com/~r/PaulDixExplainsNothing/~3/383536354/marshal-data-to.html?param1=1¶m2=2"
|
584
|
+
end
|
544
585
|
end
|
545
586
|
|
546
587
|
describe "parsing a tree" do
|
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sax-machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.1
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Paul Dix
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2013-10-14 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: nokogiri
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 1.
|
23
|
+
version: 1.6.0
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -28,7 +28,7 @@ dependencies:
|
|
28
28
|
requirements:
|
29
29
|
- - ~>
|
30
30
|
- !ruby/object:Gem::Version
|
31
|
-
version: 1.
|
31
|
+
version: 1.6.0
|
32
32
|
- !ruby/object:Gem::Dependency
|
33
33
|
name: rspec
|
34
34
|
requirement: !ruby/object:Gem::Requirement
|
@@ -36,7 +36,7 @@ dependencies:
|
|
36
36
|
requirements:
|
37
37
|
- - ~>
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: 2.
|
39
|
+
version: 2.13.0
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 2.
|
47
|
+
version: 2.13.0
|
48
48
|
description:
|
49
49
|
email: paul@pauldix.net
|
50
50
|
executables: []
|
@@ -80,7 +80,8 @@ files:
|
|
80
80
|
- spec/sax-machine/sax_document_spec.rb
|
81
81
|
- spec/spec_helper.rb
|
82
82
|
homepage: http://github.com/pauldix/sax-machine
|
83
|
-
licenses:
|
83
|
+
licenses:
|
84
|
+
- MIT
|
84
85
|
post_install_message:
|
85
86
|
rdoc_options: []
|
86
87
|
require_paths:
|
@@ -91,18 +92,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
92
|
- - ! '>='
|
92
93
|
- !ruby/object:Gem::Version
|
93
94
|
version: '0'
|
94
|
-
segments:
|
95
|
-
- 0
|
96
|
-
hash: 3574605312337356673
|
97
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
96
|
none: false
|
99
97
|
requirements:
|
100
|
-
- - ! '
|
98
|
+
- - ! '>='
|
101
99
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
100
|
+
version: '0'
|
103
101
|
requirements: []
|
104
102
|
rubyforge_project:
|
105
|
-
rubygems_version: 1.8.
|
103
|
+
rubygems_version: 1.8.23
|
106
104
|
signing_key:
|
107
105
|
specification_version: 3
|
108
106
|
summary: Declarative SAX Parsing with Nokogiri
|