astro-sax-machine 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,11 @@
1
- require "rubygems"
2
-
3
1
  $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
4
2
 
3
+ require "cgi"
4
+
5
5
  require "sax-machine/sax_document"
6
6
  require "sax-machine/sax_handler"
7
7
  require "sax-machine/sax_config"
8
8
 
9
9
  module SAXMachine
10
- VERSION = "0.0.14"
11
- end
10
+ VERSION = "0.0.20"
11
+ end
@@ -15,13 +15,13 @@ module SAXMachine
15
15
  else nil
16
16
  end
17
17
  @default_xmlns = options[:default_xmlns]
18
- if @default_xmlns && !@xmlns.include?('')
18
+ if @default_xmlns && @xmlns && !@xmlns.include?('')
19
19
  @xmlns << ''
20
20
  end
21
21
  end
22
22
 
23
23
  def handler(nsstack)
24
- if nsstack.nil? || nsstack[''] == ''
24
+ if @default_xmlns && (nsstack.nil? || nsstack[''] == '')
25
25
  nsstack = NSStack.new(nsstack, nsstack)
26
26
  nsstack[''] = @default_xmlns
27
27
  end
@@ -51,7 +51,7 @@ module SAXMachine
51
51
  end
52
52
 
53
53
  def column(sym)
54
- sax_config.top_level_elements[sym.to_s][0]
54
+ (sax_config.top_level_elements[sym.to_s] || []).first
55
55
  end
56
56
 
57
57
  def data_class(sym)
@@ -45,7 +45,7 @@ module SAXMachine
45
45
  end
46
46
 
47
47
  def end_element(name)
48
- if parsing_collection? && @collection_config.name == name
48
+ if parsing_collection? && @collection_config.name == name.split(':').last
49
49
  @collection_handler.end_element(name)
50
50
  @object.send(@collection_config.accessor) << @collection_handler.object
51
51
  reset_current_collection
@@ -109,13 +109,14 @@ module SAXMachine
109
109
  def self.decode_xml(str)
110
110
  return str.map &method(:decode_xml) if str.kind_of?(Array)
111
111
 
112
- entities = {
113
- '#38' => '&amp;',
114
- '#13' => "\r",
115
- }
116
- entities.keys.inject(str) { |string, key|
117
- string.gsub(/&#{key};/, entities[key])
118
- }
112
+ # entities = {
113
+ # '#38' => '&amp;',
114
+ # '#13' => "\r",
115
+ # }
116
+ # entities.keys.inject(str) { |string, key|
117
+ # string.gsub(/&#{key};/, entities[key])
118
+ # }
119
+ CGI.unescapeHTML(str)
119
120
  end
120
121
 
121
122
  end
@@ -138,7 +138,7 @@ describe "SAXMachine" do
138
138
 
139
139
  it "should escape correctly the ampersand" do
140
140
  document = @klass.parse("<link href='http://api.flickr.com/services/feeds/photos_public.gne?id=49724566@N00&amp;lang=en-us&amp;format=atom' foo='bar'>asdf</link>")
141
- document.link.should == "http://api.flickr.com/services/feeds/photos_public.gne?id=49724566@N00&amp;lang=en-us&amp;format=atom"
141
+ document.link.should == "http://api.flickr.com/services/feeds/photos_public.gne?id=49724566@N00&lang=en-us&format=atom"
142
142
  end
143
143
 
144
144
  it "should save the value of a matching element" do
@@ -344,7 +344,7 @@ describe "SAXMachine" do
344
344
  end
345
345
 
346
346
  context "when passing a default namespace" do
347
- before :each do
347
+ before :all do
348
348
  @xmlns = 'urn:test'
349
349
  class Inner
350
350
  include SAXMachine
@@ -369,11 +369,55 @@ describe "SAXMachine" do
369
369
  end
370
370
 
371
371
  end
372
+
373
+ describe "when desiring sax events" do
374
+ XHTML_XMLNS = "http://www.w3.org/1999/xhtml"
375
+
376
+ before :all do
377
+ @klass = Class.new do
378
+ include SAXMachine
379
+ elements :body, :events => true
380
+ end
381
+ end
382
+
383
+ it "should parse a simple child" do
384
+ document = @klass.parse("<body><p/></body>")
385
+ document.body.should == [[:start_element, "", "p", []],
386
+ [:end_element, "", "p"]]
387
+ end
388
+ it "should parse a simple child with text" do
389
+ document = @klass.parse("<body><p>Hello</p></body>")
390
+ document.body.should == [[:start_element, "", "p", []],
391
+ [:chars, "Hello"],
392
+ [:end_element, "", "p"]]
393
+ end
394
+ it "should parse nested children" do
395
+ document = @klass.parse("<body><p><span/></p></body>")
396
+ document.body.should == [[:start_element, "", "p", []],
397
+ [:start_element, "", "span", []],
398
+ [:end_element, "", "span"],
399
+ [:end_element, "", "p"]]
400
+ end
401
+ it "should parse multiple children" do
402
+ document = @klass.parse("<body><p>Hello</p><p>World</p></body>")
403
+ document.body.should == [[:start_element, "", "p", []],
404
+ [:chars, "Hello"],
405
+ [:end_element, "", "p"],
406
+ [:start_element, "", "p", []],
407
+ [:chars, "World"],
408
+ [:end_element, "", "p"]]
409
+ end
410
+ it "should pass namespaces" do
411
+ document = @klass.parse("<body xmlns='#{XHTML_XMLNS}'><p/></body>")
412
+ document.body.should == [[:start_element, XHTML_XMLNS, "p", []],
413
+ [:end_element, XHTML_XMLNS, "p"]]
414
+ end
415
+ end
372
416
  end
373
417
 
374
418
  describe "elements" do
375
419
  describe "when parsing multiple elements" do
376
- before :each do
420
+ before :all do
377
421
  @klass = Class.new do
378
422
  include SAXMachine
379
423
  elements :entry, :as => :entries
@@ -542,37 +586,71 @@ eoxml
542
586
  @document.link.should == 'http://feeds.delicious.com/v2/rss/tag/pubsubhubbub?count=15'
543
587
  end
544
588
  end
589
+ end
590
+
591
+ describe "yet another full example" do
592
+
545
593
  context "when parsing a Twitter example" do
546
594
  before :all do
547
- @document = Root.parse(<<-eoxml).channels[0]
595
+
596
+ RSS_XMLNS = ['http://purl.org/rss/1.0/', '']
597
+
598
+ ATOM_XMLNS = 'http://www.w3.org/2005/Atom' unless defined? ATOM_XMLNS
599
+ class Link
600
+ include SAXMachine
601
+ end
602
+
603
+ class Entry
604
+ include SAXMachine
605
+ element :title, :xmlns => RSS_XMLNS
606
+ element :link, :xmlns => RSS_XMLNS, :as => :entry_link
607
+ element :title, :xmlns => ATOM_XMLNS, :as => :title
608
+ elements :link, :xmlns => ATOM_XMLNS, :as => :links, :class => Link
609
+ end
610
+
611
+ class Feed
612
+ include SAXMachine
613
+ element :title, :xmlns => RSS_XMLNS, :as => :title
614
+ element :link, :xmlns => RSS_XMLNS, :as => :feed_link
615
+ elements :item, :xmlns => RSS_XMLNS, :as => :entries, :class => Entry
616
+ element :title, :xmlns => ATOM_XMLNS, :as => :title
617
+ elements :link, :xmlns => ATOM_XMLNS, :as => :links, :class => Link
618
+ end
619
+
620
+ @document = Feed.parse(<<-eoxml)
548
621
  <?xml version="1.0" encoding="UTF-8"?>
549
- <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
550
- <channel>
551
- <atom:link type="application/rss+xml" rel="self" href="http://twitter.com/statuses/user_timeline/5381582.rss"/>
552
- <title>Twitter / julien51</title>
553
- <link>http://twitter.com/julien51</link>
554
- <description>Twitter updates from julien / julien51.</description>
555
- <language>en-us</language>
556
- <ttl>40</ttl>
557
- <item>
558
- <title>julien51: @github : I get an error when trying to build one of my gems (julien51-sax-machine), it seems related to another gem's gemspec.</title>
559
- <description>julien51: @github : I get an error when trying to build one of my gems (julien51-sax-machine), it seems related to another gem's gemspec.</description>
560
- <pubDate>Thu, 30 Jul 2009 01:00:30 +0000</pubDate>
561
- <guid>http://twitter.com/julien51/statuses/2920716033</guid>
562
- <link>http://twitter.com/julien51/statuses/2920716033</link>
563
- </item>
564
- </channel>
565
- </rss>
622
+ <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
623
+ <channel>
624
+ <atom:link type="application/rss+xml" rel="self" href="http://twitter.com/statuses/user_timeline/5381582.rss"/>
625
+ <title>Twitter / julien51</title>
626
+ <link>http://twitter.com/julien51</link>
627
+ <description>Twitter updates from julien / julien51.</description>
628
+ <language>en-us</language>
629
+ <ttl>40</ttl>
630
+ <item>
631
+ <title>julien51: @github : I get an error when trying to build one of my gems (julien51-sax-machine), it seems related to another gem's gemspec.</title>
632
+ <description>julien51: @github : I get an error when trying to build one of my gems (julien51-sax-machine), it seems related to another gem's gemspec.</description>
633
+ <pubDate>Thu, 30 Jul 2009 01:00:30 +0000</pubDate>
634
+ <guid>http://twitter.com/julien51/statuses/2920716033</guid>
635
+ <link>http://twitter.com/julien51/statuses/2920716033</link>
636
+ </item>
637
+ <item>
638
+ <title>julien51: Hum, San Francisco's summer are delightful. http://bit.ly/VeXt4</title>
639
+ <description>julien51: Hum, San Francisco's summer are delightful. http://bit.ly/VeXt4</description>
640
+ <pubDate>Wed, 29 Jul 2009 23:07:32 +0000</pubDate>
641
+ <guid>http://twitter.com/julien51/statuses/2918869948</guid>
642
+ <link>http://twitter.com/julien51/statuses/2918869948</link>
643
+ </item>
644
+ </channel>
645
+ </rss>
566
646
  eoxml
567
647
  end
568
648
  it "should parse the title" do
569
649
  @document.title.should == 'Twitter / julien51'
570
650
  end
571
- it "should parse the link" do
572
- @document.link.should == 'http://twitter.com/statuses/user_timeline/5381582.rss'
573
- end
651
+
574
652
  it "should find an entry" do
575
- @document.entries.length.should == 1
653
+ @document.entries.length.should == 2
576
654
  end
577
655
  end
578
656
  end
metadata CHANGED
@@ -1,10 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: astro-sax-machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix
8
+ - astro
9
+ - julien51
10
+ - superfeedr
8
11
  autorequire:
9
12
  bindir: bin
10
13
  cert_chain: []