Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add more specs

  • Loading branch information...
commit 915278bf813b2acdad90131b32d7ae689a5d039d 1 parent a10f847
@siuying authored
View
13 lib/wakizashi/base.rb
@@ -0,0 +1,13 @@
+module Wakizashi
+ class ParseError < StandardError; end
+
+ module_function
+
+ def XML(xml, *options)
+ ::GDataXMLDocument.with_xml(xml, *options)
+ end
+
+ def HTML(html, *options)
+ ::GDataXMLDocument.with_html(html, *options)
+ end
+end
View
3  lib/wakizashi/version.rb
@@ -0,0 +1,3 @@
+module Wakizashi
+ VERSION = '0.1.0'
+end
View
54 lib/wakizashi/xml_element.rb
@@ -1,19 +1,45 @@
-class GDataXMLElement
- module CreationMethods
- def with_html(html)
- error_ptr = Pointer.new(:id)
- elem = self.alloc.initWithHTMLString(html, error:error_ptr)
- raise Wakizashi::ParseError, error_ptr[0].description if error_ptr[0]
- elem
- end
+module Wakizashi
+ module Element
+ module CreationMethods
+ def with_html(html)
+ error_ptr = Pointer.new(:id)
+ elem = self.alloc.initWithHTMLString(html, error:error_ptr)
+ raise Wakizashi::ParseError, error_ptr[0].description if error_ptr[0]
+ elem
+ end
- def with_xml(xml)
- error_ptr = Pointer.new(:id)
- elem = self.alloc.initWithXMLString(xml, error:error_ptr)
- raise Wakizashi::ParseError, error_ptr[0].description if error_ptr[0]
- elem
+ def with_xml(xml)
+ error_ptr = Pointer.new(:id)
+ elem = self.alloc.initWithXMLString(xml, error:error_ptr)
+ raise Wakizashi::ParseError, error_ptr[0].description if error_ptr[0]
+ elem
+ end
+ end
+
+ module InstanceMethods
+ def []=(key, val)
+ attr = self.attributeForName(key)
+ if attr
+ attr.stringValue = val
+ else
+ attr = self.addAttribute(GDataXMLNode.attributeWithName(key, stringValue:val))
+ end
+ val
+ end
+
+ def [](key)
+ attr = self.attributeForName(key)
+ if attr
+ attr.stringValue
+ else
+ nil
+ end
+ end
end
end
+end
- extend CreationMethods
+class GDataXMLElement
+ extend Wakizashi::Element::CreationMethods
+ include Wakizashi::Element::InstanceMethods
end
View
14 spec/wakizashi_spec.rb
@@ -11,4 +11,18 @@
doc.rootElement.to_xml.should == "<p>Hello World</p>"
end
+ it "should search document and modify content" do
+ html = "<html><body><h1>Hello World</h1><p>Welcome</p><p>Foo</p><a href='http://www.google.com'>Google</a></body></html>"
+ doc = Wakizashi.HTML(html)
+
+ link = doc.xpath("//a").first
+ link["href"].should == "http://www.google.com"
+ link.stringValue.should == "Google"
+
+ link["href"] = "http://wikipedia.org"
+ link.stringValue = "Wiki"
+
+ link.to_html.should == "<html><body><h1>Hello World</h1><p>Welcome</p><p>Foo</p><a href=\"http://wikipedia.org\">Wiki</a></body></html>"
+
+ end
end
View
6 spec/xml_document_spec.rb
@@ -17,12 +17,6 @@
it "search elment with xpath" do
doc = GDataXMLDocument.with_html("<ul><li><p>Hello World</p></li><li>Boom</li></ul>")
- nodes = doc.xpath("//p")
- nodes.size.should == 1
- nodes.first.class.should == GDataXMLElement
- nodes.first.name == "p"
- nodes.first.to_s == "Hello World"
-
nodes = doc.xpath("//li")
nodes.size.should == 2
nodes.first.class.should == GDataXMLElement
View
9 spec/xml_element_spec.rb
@@ -4,4 +4,13 @@
element.to_s.should == "Hello World"
element.childCount.should == 1
end
+
+ it "should read and write attribute and value" do
+ element = GDataXMLElement.with_xml("<a href='http://www.google.com'>Google</a>")
+ element["href"].should == "http://www.google.com"
+
+ element["href"] = "http://wikipedia.org"
+ element.stringValue = "Wikipedia"
+ element.to_xml.should == '<a href="http://wikipedia.org">Wikipedia</a>'
+ end
end
View
2  spec/xml_node_spec.rb
@@ -0,0 +1,2 @@
+describe "XmlNode" do
+end
Please sign in to comment.
Something went wrong with that request. Please try again.