ealdent-sax-machine 0.0.4 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile ADDED
@@ -0,0 +1,78 @@
1
+ h1. SAX Machine
2
+
3
+ "http://github.com/pauldix/sax-machine/wikis":http://github.com/pauldix/sax-machine/wikis
4
+
5
+ "http://github.com/pauldix/sax-machine/tree/master":http://github.com/pauldix/sax-machine/tree/master
6
+
7
+ h2. Description
8
+
9
+ A declarative SAX parsing library backed by Nokogiri
10
+
11
+ h2. Usage
12
+
13
+ <pre>
14
+ require 'sax-machine'
15
+
16
+ # Class for parsing an atom entry out of a feedburner atom feed
17
+ class AtomEntry
18
+ include SAXMachine
19
+ element :title
20
+ # the :as argument makes this available through atom_entry.author instead of .name
21
+ element :name, :as => :author
22
+ element "feedburner:origLink", :as => :url
23
+ element :summary
24
+ element :content
25
+ element :published
26
+ end
27
+
28
+ # Class for parsing Atom feeds
29
+ class Atom
30
+ include SAXMachine
31
+ element :title
32
+ # the :with argument means that you only match a link tag that has an attribute of :type => "text/html"
33
+ # the :value argument means that instead of setting the value to the text between the tag,
34
+ # it sets it to the attribute value of :href
35
+ element :link, :value => :href, :as => :url, :with => {:type => "text/html"}
36
+ element :link, :value => :href, :as => :feed_url, :with => {:type => "application/atom+xml"}
37
+ elements :entry, :as => :entries, :class => AtomEntry
38
+ end
39
+
40
+ # you can then parse like this
41
+ feed = Atom.parse(xml_text)
42
+ # then you're ready to rock
43
+ feed.title # => whatever the title of the blog is
44
+ feed.url # => the main url of the blog
45
+ feed.feed_url # => goes to the feedburner feed
46
+
47
+ feed.entries.first.title # => title of the first entry
48
+ feed.entries.first.author # => the author of the first entry
49
+ feed.entries.first.url # => the permalink on the blog for this entry
50
+ # etc ...
51
+ </pre>
52
+
53
+ h2. LICENSE
54
+
55
+ (The MIT License)
56
+
57
+ Copyright (c) 2009:
58
+
59
+ "Paul Dix":http://pauldix.net
60
+
61
+ Permission is hereby granted, free of charge, to any person obtaining
62
+ a copy of this software and associated documentation files (the
63
+ 'Software'), to deal in the Software without restriction, including
64
+ without limitation the rights to use, copy, modify, merge, publish,
65
+ distribute, sublicense, and/or sell copies of the Software, and to
66
+ permit persons to whom the Software is furnished to do so, subject to
67
+ the following conditions:
68
+
69
+ The above copyright notice and this permission notice shall be
70
+ included in all copies or substantial portions of the Software.
71
+
72
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
73
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
74
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
75
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
76
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
77
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
78
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -22,7 +22,15 @@ module SAXMachine
22
22
  def element(name, options = {})
23
23
  options[:as] ||= name
24
24
  sax_config.add_top_level_element(name, options)
25
- attr_accessor options[:as]
25
+
26
+ # we only want to insert the setter if they haven't defined it from elsewhere.
27
+ # this is how we allow custom parsing behavior. So you could define the setter
28
+ # and have it parse the string into a date or whatever.
29
+ if instance_methods.include?("#{options[:as]}=")
30
+ attr_reader options[:as]
31
+ else
32
+ attr_accessor options[:as]
33
+ end
26
34
  end
27
35
 
28
36
  def elements(name, options = {})
@@ -13,10 +13,14 @@ module SAXMachine
13
13
  if parsing_collection?
14
14
  @collection_handler.characters(string)
15
15
  elsif @element_config
16
- @value = string
16
+ @value << string
17
17
  end
18
18
  end
19
19
 
20
+ def cdata_block(string)
21
+ characters(string)
22
+ end
23
+
20
24
  def start_element(name, attrs = [])
21
25
  @name = name
22
26
  @attrs = attrs
@@ -31,6 +35,7 @@ module SAXMachine
31
35
  parse_element_attribute
32
36
 
33
37
  else
38
+ @value = ""
34
39
  @element_config = sax_config.element_config_for_tag(@name, @attrs)
35
40
  end
36
41
  end
@@ -52,7 +57,7 @@ module SAXMachine
52
57
  end
53
58
 
54
59
  def characaters_captured?
55
- !@value.nil?
60
+ !@value.nil? && !@value.empty?
56
61
  end
57
62
 
58
63
  def parsing_collection?
data/lib/sax-machine.rb CHANGED
@@ -7,5 +7,5 @@ require "sax-machine/sax_handler"
7
7
  require "sax-machine/sax_config"
8
8
 
9
9
  module SAXMachine
10
- VERSION = "0.0.4"
10
+ VERSION = "0.0.7"
11
11
  end
@@ -16,6 +16,19 @@ describe "SAXMachine" do
16
16
  document.title.should == "Title"
17
17
  end
18
18
 
19
+ it "should not overwrite the setter if there is already one present" do
20
+ @klass = Class.new do
21
+ def title=(val)
22
+ @title = "#{val} **"
23
+ end
24
+ include SAXMachine
25
+ element :title
26
+ end
27
+ document = @klass.new
28
+ document.title = "Title"
29
+ document.title.should == "Title **"
30
+ end
31
+
19
32
  it "should not overwrite the accessor when the element is not present" do
20
33
  document = @klass.new
21
34
  document.title = "Title"
@@ -23,7 +36,7 @@ describe "SAXMachine" do
23
36
  document.title.should == "Title"
24
37
  end
25
38
 
26
- it "should overwrite the accessor when the element is present" do
39
+ it "should overwrite the value when the element is present" do
27
40
  document = @klass.new
28
41
  document.title = "Old title"
29
42
  document.parse("<title>New title</title>")
@@ -34,6 +47,11 @@ describe "SAXMachine" do
34
47
  document = @klass.parse("<title>My Title</title>")
35
48
  document.title.should == "My Title"
36
49
  end
50
+
51
+ it "should save cdata into an accessor" do
52
+ document = @klass.parse("<title><![CDATA[A Title]]></title>")
53
+ document.title.should == "A Title"
54
+ end
37
55
 
38
56
  it "should save the element text into an accessor when there are multiple elements" do
39
57
  document = @klass.parse("<xml><title>My Title</title><foo>bar</foo></xml>")
data/spec/spec_helper.rb CHANGED
@@ -7,7 +7,7 @@ begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end
7
7
  path = File.expand_path(File.dirname(__FILE__) + "/../lib/")
8
8
  $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
9
9
 
10
- require "sax-machine"
10
+ require "lib/sax-machine"
11
11
 
12
12
  # Spec::Runner.configure do |config|
13
13
  # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ealdent-sax-machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix
@@ -36,12 +36,12 @@ files:
36
36
  - lib/sax-machine/sax_element_config.rb
37
37
  - lib/sax-machine/sax_document.rb
38
38
  - lib/sax-machine/sax_handler.rb
39
- - README.rdoc
39
+ - README.textile
40
40
  - Rakefile
41
41
  - spec/spec.opts
42
42
  - spec/spec_helper.rb
43
43
  - spec/sax-machine/sax_document_spec.rb
44
- has_rdoc: true
44
+ has_rdoc: false
45
45
  homepage: http://github.com/pauldix/sax-machine
46
46
  post_install_message:
47
47
  rdoc_options: []