mapricot 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mapricot/abstract_doc.rb +2 -24
- data/lib/mapricot/associations.rb +3 -3
- data/lib/mapricot/base.rb +20 -7
- data/mapricot.gemspec +4 -4
- data/test/has_attribute/test_has_attribute.rb +1 -1
- data/test/has_many/test_has_many_ids.rb +3 -3
- data/test/has_many/test_has_many_nested.rb +4 -4
- data/test/has_one/test_has_one_id.rb +1 -1
- data/test/has_one/test_has_one_nested.rb +1 -1
- data/test/suite.rb +1 -1
- data/test/test_abstract_doc.rb +2 -7
- data/test/test_mapricot.rb +72 -26
- data/test/test_mapricot_readme.rb +2 -2
- metadata +13 -8
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'open-uri'
|
2
1
|
require 'hpricot'
|
3
2
|
require 'libxml'
|
4
3
|
require 'nokogiri'
|
@@ -13,30 +12,9 @@ module Mapricot
|
|
13
12
|
|
14
13
|
class AbstractDoc
|
15
14
|
|
16
|
-
def
|
17
|
-
adoc = new
|
18
|
-
adoc.url = url
|
19
|
-
adoc
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.from_string(string)
|
23
|
-
adoc = new
|
24
|
-
adoc.string = string
|
25
|
-
adoc
|
26
|
-
end
|
27
|
-
|
28
|
-
def url=(url)
|
29
|
-
if Mapricot.parser == :libxml
|
30
|
-
@udoc = LibXML::XML::Parser.file(url).parse
|
31
|
-
elsif Mapricot.parser == :hpricot
|
32
|
-
@udoc = Hpricot::XML(open(url))
|
33
|
-
elsif Mapricot.parser == :nokogiri
|
34
|
-
@udoc = Nokogiri::HTML(open(url))
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def string=(string)
|
15
|
+
def initialize(string)
|
39
16
|
if Mapricot.parser == :libxml
|
17
|
+
string = string.read if string.is_a?(StringIO)
|
40
18
|
@udoc = LibXML::XML::Parser.string(string).parse
|
41
19
|
elsif Mapricot.parser == :hpricot
|
42
20
|
@udoc = Hpricot::XML(string)
|
@@ -68,7 +68,7 @@ module Mapricot
|
|
68
68
|
@value = nil
|
69
69
|
else
|
70
70
|
if @type == :xml
|
71
|
-
@value = class_from_name.new(
|
71
|
+
@value = class_from_name.new(node_list.first.to_s)
|
72
72
|
else
|
73
73
|
@value = node_list.first.contents
|
74
74
|
typecast
|
@@ -88,7 +88,7 @@ module Mapricot
|
|
88
88
|
@value = []
|
89
89
|
node_list.each do |node|
|
90
90
|
if @type == :xml
|
91
|
-
@value << class_from_name.new(
|
91
|
+
@value << class_from_name.new(node.to_s)
|
92
92
|
else
|
93
93
|
@value << node.contents
|
94
94
|
end
|
@@ -97,4 +97,4 @@ module Mapricot
|
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
100
|
-
end
|
100
|
+
end
|
data/lib/mapricot/base.rb
CHANGED
@@ -39,18 +39,31 @@ module Mapricot
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
# class
|
43
|
-
#
|
44
|
-
#
|
42
|
+
# class Foo < Mapricot::Base; end;
|
43
|
+
# Foo.new :url => "http://www.example.com"
|
44
|
+
# Foo.new :xml => %(<hi></hi>)
|
45
45
|
# the class instance variable @association_list is duplicated in every instance of Feed, as the instance variable @associations.
|
46
46
|
# i.e. Feed.association_list is the template for feed.associations
|
47
|
-
def initialize(
|
48
|
-
@doc = AbstractDoc.
|
49
|
-
@doc = AbstractDoc.from_string(opts[:xml]) if opts[:xml]
|
47
|
+
def initialize(str)
|
48
|
+
@doc = AbstractDoc.new(correct_input_for_legacy_interface(str))
|
50
49
|
dup_associations_and_attributes
|
51
50
|
map_associations
|
52
51
|
map_attributes
|
53
52
|
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def correct_input_for_legacy_interface(opts)
|
57
|
+
return opts if !opts.is_a?(Hash)
|
58
|
+
|
59
|
+
$stderr.puts "Initializing with a hash is deprecated, please pass a String or StringIO, see http://github.com/lzell/mapricot for examples."
|
60
|
+
if opts[:url]
|
61
|
+
require 'open-uri'
|
62
|
+
open(opts[:url])
|
63
|
+
else
|
64
|
+
opts[:xml]
|
65
|
+
end
|
66
|
+
end
|
54
67
|
|
55
68
|
def dup_associations_and_attributes
|
56
69
|
@associations = self.class.association_list.collect {|x| x.dup} # do not do this: self.class.association_list.dup
|
@@ -78,4 +91,4 @@ module Mapricot
|
|
78
91
|
self.class.name.downcase.match(/[^:]+$/)[0]
|
79
92
|
end
|
80
93
|
end
|
81
|
-
end
|
94
|
+
end
|
data/mapricot.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "mapricot"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.5"
|
4
4
|
s.summary = "XML to object mapper"
|
5
5
|
s.email = "lzell11@gmail.com"
|
6
6
|
s.homepage = "http://github.com/lzell/mapricot"
|
@@ -34,7 +34,7 @@ Gem::Specification.new do |s|
|
|
34
34
|
s.require_paths = ["lib"]
|
35
35
|
s.rdoc_options = ["--main", "README.rdoc", "--title", "Mapricot"]
|
36
36
|
s.extra_rdoc_files = ["README.rdoc"]
|
37
|
-
s.add_dependency("hpricot")
|
38
|
-
s.add_dependency("nokogiri")
|
39
|
-
s.add_dependency("libxml-ruby")
|
37
|
+
s.add_dependency("hpricot", ">= 0.7")
|
38
|
+
s.add_dependency("nokogiri", ">= 1.4.1")
|
39
|
+
s.add_dependency("libxml-ruby", ">= 1.1.3")
|
40
40
|
end
|
@@ -30,7 +30,7 @@ class TestResponse < Test::Unit::TestCase
|
|
30
30
|
def test_response
|
31
31
|
@parsers.each do |parser|
|
32
32
|
Mapricot.parser = parser
|
33
|
-
response = Response.new(
|
33
|
+
response = Response.new(@xml)
|
34
34
|
assert_equal "New York", response.location.city
|
35
35
|
assert_equal "NY", response.location.state
|
36
36
|
assert_equal "nyc", response.location.code
|
@@ -23,7 +23,7 @@ class TestResponseWithManyIds < Test::Unit::TestCase
|
|
23
23
|
def test_response
|
24
24
|
@parsers.each do |parser|
|
25
25
|
Mapricot.parser = parser
|
26
|
-
response = ResponseWithManyIds.new(
|
26
|
+
response = ResponseWithManyIds.new(@xml)
|
27
27
|
assert_equal 10, response.ids[0]
|
28
28
|
assert_equal 20, response.ids[1]
|
29
29
|
assert_equal 30, response.ids[2]
|
@@ -39,7 +39,7 @@ class TestResponseWithManyIds < Test::Unit::TestCase
|
|
39
39
|
|
40
40
|
|
41
41
|
def test_internals
|
42
|
-
response = ResponseWithManyIds.new(
|
42
|
+
response = ResponseWithManyIds.new(@xml)
|
43
43
|
template = response.class.association_list.first
|
44
44
|
assert_equal :ids, template.name
|
45
45
|
assert_equal :integer, template.type
|
@@ -52,4 +52,4 @@ class TestResponseWithManyIds < Test::Unit::TestCase
|
|
52
52
|
assert_nil ass.namespace
|
53
53
|
assert_equal [10,20,30], ass.value
|
54
54
|
end
|
55
|
-
end
|
55
|
+
end
|
@@ -32,7 +32,7 @@ class TestResponseWithNesting < Test::Unit::TestCase
|
|
32
32
|
def test_response
|
33
33
|
@parsers.each do |parser|
|
34
34
|
Mapricot.parser = parser
|
35
|
-
response = ResponseWithNesting.new(
|
35
|
+
response = ResponseWithNesting.new(@xml)
|
36
36
|
assert_equal 10, response.users[0].id
|
37
37
|
assert_equal "bob", response.users[0].name
|
38
38
|
assert_equal 20, response.users[1].id
|
@@ -48,7 +48,7 @@ class TestResponseWithNesting < Test::Unit::TestCase
|
|
48
48
|
|
49
49
|
|
50
50
|
def test_response_internals
|
51
|
-
response = ResponseWithNesting.new(
|
51
|
+
response = ResponseWithNesting.new(@xml)
|
52
52
|
template = response.class.association_list.first
|
53
53
|
|
54
54
|
assert_equal :users, template.name
|
@@ -64,7 +64,7 @@ class TestResponseWithNesting < Test::Unit::TestCase
|
|
64
64
|
end
|
65
65
|
|
66
66
|
def test_response_users_internals
|
67
|
-
response = ResponseWithNesting.new(
|
67
|
+
response = ResponseWithNesting.new(@xml)
|
68
68
|
template = response.users.first.class.association_list.first
|
69
69
|
assert_equal :id, template.name
|
70
70
|
assert_equal :integer, template.type
|
@@ -78,4 +78,4 @@ class TestResponseWithNesting < Test::Unit::TestCase
|
|
78
78
|
assert_equal 10, ass.value
|
79
79
|
end
|
80
80
|
|
81
|
-
end
|
81
|
+
end
|
@@ -18,7 +18,7 @@ class TestReponseWithOneId < Test::Unit::TestCase
|
|
18
18
|
def test_response
|
19
19
|
@parsers.each do |parser|
|
20
20
|
Mapricot.parser = parser
|
21
|
-
response = ResponseWithOneId.new(
|
21
|
+
response = ResponseWithOneId.new(@xml)
|
22
22
|
assert_equal 10, response.id
|
23
23
|
end
|
24
24
|
end
|
@@ -24,7 +24,7 @@ class TestReponseWithNesting < Test::Unit::TestCase
|
|
24
24
|
def test_response
|
25
25
|
@parsers.each do |parser|
|
26
26
|
Mapricot.parser = parser
|
27
|
-
response = ResponseWithNesting.new(
|
27
|
+
response = ResponseWithNesting.new(@xml)
|
28
28
|
assert_equal "bob", response.user.name
|
29
29
|
end
|
30
30
|
end
|
data/test/suite.rb
CHANGED
@@ -10,7 +10,7 @@ test_glob = File.join(path, "**", "test_*.rb")
|
|
10
10
|
tests = Dir.glob(test_glob)
|
11
11
|
|
12
12
|
if ARGV[0] != 'rg'
|
13
|
-
tests.each {|test| system("ruby #{test}")}
|
13
|
+
tests.each {|test| system("ruby -rubygems #{test}")}
|
14
14
|
else
|
15
15
|
tests.each {|test| system("rg #{test}")}
|
16
16
|
end
|
data/test/test_abstract_doc.rb
CHANGED
@@ -4,15 +4,10 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'maprico
|
|
4
4
|
|
5
5
|
class TestAbstractDoc < Test::Unit::TestCase
|
6
6
|
|
7
|
-
def test_initialize_from_string_or_url
|
8
|
-
assert_respond_to Mapricot::AbstractDoc, :from_string
|
9
|
-
assert_respond_to Mapricot::AbstractDoc, :from_url
|
10
|
-
end
|
11
|
-
|
12
7
|
def test_getting_node_contents
|
13
8
|
[:libxml, :nokogiri, :hpricot].each do |parser|
|
14
9
|
Mapricot.parser = parser
|
15
|
-
@doc = Mapricot::AbstractDoc.
|
10
|
+
@doc = Mapricot::AbstractDoc.new('<user>bob</user>')
|
16
11
|
@node = @doc.find(:user).first
|
17
12
|
assert_equal "bob", @node.contents
|
18
13
|
assert_equal "<user>bob</user>", @node.to_s
|
@@ -22,7 +17,7 @@ class TestAbstractDoc < Test::Unit::TestCase
|
|
22
17
|
def test_node_list
|
23
18
|
[:libxml, :nokogiri, :hpricot].each do |parser|
|
24
19
|
Mapricot.parser = parser
|
25
|
-
@doc = Mapricot::AbstractDoc.
|
20
|
+
@doc = Mapricot::AbstractDoc.new('<response><user>sally</user><user>bob</user></response>')
|
26
21
|
@node_list = @doc.find(:user)
|
27
22
|
assert_instance_of Mapricot::AbstractNodeList, @node_list
|
28
23
|
assert_respond_to @node_list, :each
|
data/test/test_mapricot.rb
CHANGED
@@ -5,52 +5,94 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'maprico
|
|
5
5
|
# Please look at test_mapricot_readme.rb first!!
|
6
6
|
# !!!!!!!!!!!!!!!!!!
|
7
7
|
|
8
|
+
# Add test to parse uri, I'll use Natural Inputs as the example:
|
9
|
+
module NI
|
10
|
+
class NaturalInputsResponse < Mapricot::Base
|
11
|
+
has_one :message
|
12
|
+
has_many :occurrences, :xml
|
13
|
+
end
|
14
|
+
|
15
|
+
class Occurrence < Mapricot::Base
|
16
|
+
has_one :type
|
17
|
+
has_one :start_date
|
18
|
+
has_one :end_date
|
19
|
+
has_one :start_time
|
20
|
+
has_one :end_time
|
21
|
+
has_one :day_of_week
|
22
|
+
has_one :week_of_month, :integer
|
23
|
+
has_one :date_of_month, :integer
|
24
|
+
has_one :interval, :integer
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class TestNI < Test::Unit::TestCase
|
29
|
+
|
30
|
+
def setup
|
31
|
+
query = "go to the park tomorrow at 10am"
|
32
|
+
now = Time.local(2010, 'Apr', 6)
|
33
|
+
@url = "http://naturalinputs.com/query?q=#{URI.escape(query)}&t=#{now.strftime("%Y%m%dT%H%M%S")}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_url_parsing
|
37
|
+
require 'open-uri'
|
38
|
+
|
39
|
+
[:hpricot, :nokogiri, :libxml].each do |parser|
|
40
|
+
Mapricot.parser = parser
|
41
|
+
response = NI::NaturalInputsResponse.new(open(@url))
|
42
|
+
assert_equal "go to the park", response.message
|
43
|
+
assert_equal "20100407", response.occurrences.first.start_date
|
44
|
+
assert_equal "10:00:00", response.occurrences.first.start_time
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
8
51
|
class FeedA < Mapricot::Base
|
9
|
-
has_one :
|
52
|
+
has_one :name
|
10
53
|
end
|
11
54
|
|
12
55
|
class TestFeedA < Test::Unit::TestCase
|
13
56
|
|
14
|
-
def
|
15
|
-
feeda = FeedA.new(
|
16
|
-
assert feeda.
|
57
|
+
def test_no_name
|
58
|
+
feeda = FeedA.new("<notag></notag>")
|
59
|
+
assert feeda.name.nil?
|
17
60
|
end
|
18
61
|
|
19
|
-
def
|
20
|
-
feeda = FeedA.new(
|
21
|
-
assert feeda.
|
62
|
+
def test_empty_name
|
63
|
+
feeda = FeedA.new("<name></name>")
|
64
|
+
assert feeda.name.empty?
|
22
65
|
end
|
23
66
|
|
24
|
-
def
|
25
|
-
@feed = FeedA.new(
|
26
|
-
assert_equal "
|
67
|
+
def test_full_name
|
68
|
+
@feed = FeedA.new("<name>Lou</name>")
|
69
|
+
assert_equal "Lou", @feed.name
|
27
70
|
end
|
28
71
|
end
|
29
72
|
|
30
73
|
|
31
|
-
|
32
|
-
|
33
74
|
class FeedB < Mapricot::Base
|
34
|
-
has_many :
|
75
|
+
has_many :names
|
35
76
|
end
|
36
77
|
|
37
78
|
class TestFeedB < Test::Unit::TestCase
|
38
|
-
def
|
39
|
-
feedb = FeedB.new(
|
40
|
-
assert feedb.
|
41
|
-
assert feedb.
|
79
|
+
def test_many_empty_names
|
80
|
+
feedb = FeedB.new("<notag></notag>")
|
81
|
+
assert feedb.names.is_a?(Array)
|
82
|
+
assert feedb.names.empty?
|
42
83
|
end
|
43
84
|
|
44
|
-
def
|
45
|
-
|
85
|
+
def test_full_names
|
86
|
+
xml = %(
|
46
87
|
<response>
|
47
|
-
<
|
48
|
-
<
|
88
|
+
<name>Lou</name>
|
89
|
+
<name>Pizza the Hut</name>
|
49
90
|
</response>
|
50
|
-
)
|
91
|
+
)
|
92
|
+
feedb = FeedB.new(xml)
|
51
93
|
|
52
|
-
assert_equal "
|
53
|
-
assert_equal "
|
94
|
+
assert_equal "Lou", feedb.names.first
|
95
|
+
assert_equal "Pizza the Hut", feedb.names.last
|
54
96
|
end
|
55
97
|
end
|
56
98
|
|
@@ -67,12 +109,12 @@ end
|
|
67
109
|
|
68
110
|
class TestFeedC < Test::Unit::TestCase
|
69
111
|
def test_many_empty_bars
|
70
|
-
feedc = FeedC.new(
|
112
|
+
feedc = FeedC.new("<no></no>")
|
71
113
|
assert feedc.bars.empty?
|
72
114
|
end
|
73
115
|
|
74
116
|
def test_many_bars
|
75
|
-
feedc = FeedC.new(
|
117
|
+
feedc = FeedC.new(%(
|
76
118
|
<response>
|
77
119
|
<bar>
|
78
120
|
<name>maroon saloon</name>
|
@@ -87,3 +129,7 @@ class TestFeedC < Test::Unit::TestCase
|
|
87
129
|
assert_equal ['brett', 'chris', 'garon', 'mitch'], feedc.bars.first.patrons
|
88
130
|
end
|
89
131
|
end
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
@@ -21,7 +21,7 @@ class TestSimpleUser < Test::Unit::TestCase
|
|
21
21
|
</user>
|
22
22
|
)
|
23
23
|
|
24
|
-
@user = SimpleUser.new(
|
24
|
+
@user = SimpleUser.new(xml)
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_mapping
|
@@ -77,7 +77,7 @@ class TestUser < Test::Unit::TestCase
|
|
77
77
|
</user>
|
78
78
|
)
|
79
79
|
|
80
|
-
@user = User.new(
|
80
|
+
@user = User.new(xml)
|
81
81
|
end
|
82
82
|
|
83
83
|
def test_id_and_name
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 5
|
9
|
+
version: 0.0.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Lou Zell
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-04-06 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,7 +26,8 @@ dependencies:
|
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
segments:
|
28
28
|
- 0
|
29
|
-
|
29
|
+
- 7
|
30
|
+
version: "0.7"
|
30
31
|
type: :runtime
|
31
32
|
version_requirements: *id001
|
32
33
|
- !ruby/object:Gem::Dependency
|
@@ -37,8 +38,10 @@ dependencies:
|
|
37
38
|
- - ">="
|
38
39
|
- !ruby/object:Gem::Version
|
39
40
|
segments:
|
40
|
-
-
|
41
|
-
|
41
|
+
- 1
|
42
|
+
- 4
|
43
|
+
- 1
|
44
|
+
version: 1.4.1
|
42
45
|
type: :runtime
|
43
46
|
version_requirements: *id002
|
44
47
|
- !ruby/object:Gem::Dependency
|
@@ -49,8 +52,10 @@ dependencies:
|
|
49
52
|
- - ">="
|
50
53
|
- !ruby/object:Gem::Version
|
51
54
|
segments:
|
52
|
-
-
|
53
|
-
|
55
|
+
- 1
|
56
|
+
- 1
|
57
|
+
- 3
|
58
|
+
version: 1.1.3
|
54
59
|
type: :runtime
|
55
60
|
version_requirements: *id003
|
56
61
|
description: XML to object mapper with an interface similar to ActiveRecord associations.
|