magnum-pi 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/CHANGELOG.rdoc +4 -0
- data/VERSION +1 -1
- data/lib/magnum-pi.rb +1 -1
- data/lib/magnum-pi/api/consumer.rb +1 -1
- data/lib/magnum-pi/gem_ext.rb +2 -1
- data/lib/magnum-pi/gem_ext/ox.rb +1 -0
- data/lib/magnum-pi/gem_ext/ox/ax.rb +115 -0
- data/lib/magnum-pi/version.rb +2 -2
- data/magnum-pi.gemspec +1 -1
- data/test/unit/api/test_consumer.rb +46 -1
- data/test/unit/core_ext/test_ax.rb +57 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YTc1YTljNjBjYzcwMmY5YmQxZmNhNGQ1MjhmNzU1ZjRhNzQ3MGQyMg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YjNlNjUyY2VlNzUxOThjY2U0MmMyMTBhMGNhMGJjZGJkNDBhMDFiMw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OGI1NzVkYWYxZjcxMmZkNzgyMjc1YzhmNThhZGQ2M2FlOGYzOGJjMzM2NTkw
|
10
|
+
NTExMTNkYjM0MGY3Nzc5NmI2ZWQ2MmM1ZDE3YmYzZjgwMDkzYzcwMWQ3N2Ey
|
11
|
+
YzY1ODVlNWQwZTFlNDBjODdlZTMyNTk4YzI0YTEwYjM2YjA5MDI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MGMzMjA5N2ViZjA5MGIyN2Y1ZmFjNmM5MmFmMzVkZTg5NDM4NmQzZmY1MTJk
|
14
|
+
MWJlZjIzZjIzOTBhNjdjODMxNWRjMjcyM2NhZWU4NmViODQxYjQ3ZjRiZWY0
|
15
|
+
OTU1ZjdjOTEzMjVkYjJkOWY2ZWM5ODFkZjI0M2QyNDczNzNmMDA=
|
data/CHANGELOG.rdoc
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/magnum-pi.rb
CHANGED
data/lib/magnum-pi/gem_ext.rb
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
require "magnum-pi/gem_ext/mechanize"
|
1
|
+
require "magnum-pi/gem_ext/mechanize"
|
2
|
+
require "magnum-pi/gem_ext/ox"
|
@@ -0,0 +1 @@
|
|
1
|
+
require "magnum-pi/gem_ext/ox/ax"
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require "stringio"
|
2
|
+
|
3
|
+
class Ax < Ox::Sax
|
4
|
+
|
5
|
+
def initialize(xml)
|
6
|
+
@stringio = StringIO.new xml
|
7
|
+
end
|
8
|
+
|
9
|
+
def each(pattern, &block)
|
10
|
+
@pattern = pattern.split("/").collect{|x| x == "" ? "*" : x}
|
11
|
+
@block = block
|
12
|
+
|
13
|
+
Ox.sax_parse self, @stringio
|
14
|
+
|
15
|
+
ensure
|
16
|
+
@current_path = nil
|
17
|
+
@elements = nil
|
18
|
+
@entry_regexp = nil
|
19
|
+
@last_call = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def start_element(name)
|
23
|
+
current_path << name
|
24
|
+
if current_element
|
25
|
+
add_element name
|
26
|
+
elsif entry?
|
27
|
+
add_element
|
28
|
+
else
|
29
|
+
return
|
30
|
+
end
|
31
|
+
@last_call = :start_element
|
32
|
+
end
|
33
|
+
|
34
|
+
def attr(name, str)
|
35
|
+
if element = current_element
|
36
|
+
element[name.to_s] = str
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def text(str)
|
41
|
+
if element = current_element
|
42
|
+
element[:text] = str
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def end_element(name)
|
47
|
+
element = finalize_element
|
48
|
+
if entry?
|
49
|
+
@block.call element, current_path[-1]
|
50
|
+
elsif element
|
51
|
+
@last_call = :end_element
|
52
|
+
end
|
53
|
+
current_path.pop
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_hash
|
57
|
+
hash = {}
|
58
|
+
each "*" do |entry|
|
59
|
+
hash.merge! entry
|
60
|
+
end
|
61
|
+
hash
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def current_path
|
67
|
+
@current_path ||= []
|
68
|
+
end
|
69
|
+
|
70
|
+
def elements
|
71
|
+
@elements ||= []
|
72
|
+
end
|
73
|
+
|
74
|
+
def entry?
|
75
|
+
@entry_regexp ||= begin
|
76
|
+
pattern = @pattern.join(%q{\/}).gsub("*", %q{[^\/]+})
|
77
|
+
Regexp.new "^#{pattern}$"
|
78
|
+
end
|
79
|
+
!!current_path.join("/").match(@entry_regexp)
|
80
|
+
end
|
81
|
+
|
82
|
+
def current_element
|
83
|
+
elements[-1]
|
84
|
+
end
|
85
|
+
|
86
|
+
def add_element(name = nil)
|
87
|
+
element = {}
|
88
|
+
if parent = current_element
|
89
|
+
values = parent[name.to_s] ||= []
|
90
|
+
element[:values] = values
|
91
|
+
element[:index] = values.size
|
92
|
+
end
|
93
|
+
elements << element
|
94
|
+
end
|
95
|
+
|
96
|
+
def finalize_element
|
97
|
+
if element = elements.pop
|
98
|
+
values = element.delete :values
|
99
|
+
index = element.delete :index
|
100
|
+
if text = element.delete(:text)
|
101
|
+
if element.empty?
|
102
|
+
element = text
|
103
|
+
elsif element["content"]
|
104
|
+
element["content"] = [element["content"]] unless element["content"].is_a?(Array)
|
105
|
+
element["content"] << text
|
106
|
+
else
|
107
|
+
element["content"] = text
|
108
|
+
end
|
109
|
+
end
|
110
|
+
values[index] = element if values
|
111
|
+
end
|
112
|
+
element
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
data/lib/magnum-pi/version.rb
CHANGED
data/magnum-pi.gemspec
CHANGED
@@ -154,16 +154,61 @@ module Unit
|
|
154
154
|
@consumer.expects(:api).returns :format => "xml"
|
155
155
|
assert_equal(
|
156
156
|
{
|
157
|
+
"foo" => [{}],
|
157
158
|
"bar" => ["BAR"],
|
158
159
|
"baz" => [{"hello" => "world", "content" => "Baz!"}]
|
159
160
|
}, @consumer.send(:parse_content,
|
160
161
|
<<-XML
|
161
162
|
<xml>
|
163
|
+
<foo></foo>
|
162
164
|
<bar>BAR</bar>
|
163
165
|
<baz hello="world">Baz!</baz>
|
164
166
|
</xml>
|
165
167
|
XML
|
166
|
-
)
|
168
|
+
).to_hash
|
169
|
+
)
|
170
|
+
@consumer.expects(:api).returns :format => "xml"
|
171
|
+
assert_equal(
|
172
|
+
{
|
173
|
+
"bar" => ["BAR"],
|
174
|
+
"baz" => [
|
175
|
+
{
|
176
|
+
"hello" => "world",
|
177
|
+
"content" => ["foo", "Baz!"]
|
178
|
+
}, {
|
179
|
+
"hello" => "goodbye",
|
180
|
+
"content" => "Bye!"
|
181
|
+
}
|
182
|
+
],
|
183
|
+
"foos" => [
|
184
|
+
{
|
185
|
+
"foo" => ["Hello!", "You!", {"bar" => ["Bar!"]}]
|
186
|
+
},
|
187
|
+
"Baz!!!",
|
188
|
+
{
|
189
|
+
"foo" => ["Qux!"]
|
190
|
+
}
|
191
|
+
]
|
192
|
+
}, @consumer.send(:parse_content,
|
193
|
+
<<-XML
|
194
|
+
<xml>
|
195
|
+
<bar>BAR</bar>
|
196
|
+
<baz hello="world" content="foo">Baz!</baz>
|
197
|
+
<baz hello="goodbye">Bye!</baz>
|
198
|
+
<foos>
|
199
|
+
<foo>Hello!</foo>
|
200
|
+
<foo>You!</foo>
|
201
|
+
<foo>
|
202
|
+
<bar>Bar!</bar>
|
203
|
+
</foo>
|
204
|
+
</foos>
|
205
|
+
<foos>Baz!!!</foos>
|
206
|
+
<foos>
|
207
|
+
<foo>Qux!</foo>
|
208
|
+
</foos>
|
209
|
+
</xml>
|
210
|
+
XML
|
211
|
+
).to_hash
|
167
212
|
)
|
168
213
|
@consumer.expects(:api).returns :format => "unknown"
|
169
214
|
assert_equal("foobar", @consumer.send(:parse_content, "foobar"))
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require_relative "../../test_helper"
|
2
|
+
|
3
|
+
module Unit
|
4
|
+
class TestAx < MiniTest::Test
|
5
|
+
|
6
|
+
describe Ax do
|
7
|
+
before do
|
8
|
+
@xml = <<-XML
|
9
|
+
<?xml version="1.0" encoding="utf-8"?>
|
10
|
+
<response>
|
11
|
+
<data>
|
12
|
+
<article>
|
13
|
+
<title>MagnumPI is awesome!!!</title>
|
14
|
+
<category>ruby</category>
|
15
|
+
</article>
|
16
|
+
<article>
|
17
|
+
<title>Netherlands beats Spain with 1-5 :)</title>
|
18
|
+
<category>sport</category>
|
19
|
+
</article>
|
20
|
+
</data>
|
21
|
+
</response>
|
22
|
+
XML
|
23
|
+
end
|
24
|
+
it "parses XML like XmlSimple" do
|
25
|
+
assert_equal({
|
26
|
+
"data" => [{
|
27
|
+
"article" => [
|
28
|
+
{
|
29
|
+
"title" => ["MagnumPI is awesome!!!"],
|
30
|
+
"category" => ["ruby"]
|
31
|
+
}, {
|
32
|
+
"title" => ["Netherlands beats Spain with 1-5 :)"],
|
33
|
+
"category" => ["sport"]
|
34
|
+
}
|
35
|
+
]
|
36
|
+
}]
|
37
|
+
}, Ax.new(@xml).to_hash)
|
38
|
+
end
|
39
|
+
it "can iterate through an XML document" do
|
40
|
+
articles = []
|
41
|
+
Ax.new(@xml).each("/*/article") do |article|
|
42
|
+
articles << article
|
43
|
+
end
|
44
|
+
assert_equal [
|
45
|
+
{
|
46
|
+
"title" => ["MagnumPI is awesome!!!"],
|
47
|
+
"category" => ["ruby"]
|
48
|
+
}, {
|
49
|
+
"title" => ["Netherlands beats Spain with 1-5 :)"],
|
50
|
+
"category" => ["sport"]
|
51
|
+
}
|
52
|
+
], articles
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magnum-pi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Engel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mechanize
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: ox
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ! '>='
|
@@ -166,6 +166,8 @@ files:
|
|
166
166
|
- lib/magnum-pi/gem_ext/mechanize/http.rb
|
167
167
|
- lib/magnum-pi/gem_ext/mechanize/http/agent.rb
|
168
168
|
- lib/magnum-pi/gem_ext/mechanize/util.rb
|
169
|
+
- lib/magnum-pi/gem_ext/ox.rb
|
170
|
+
- lib/magnum-pi/gem_ext/ox/ax.rb
|
169
171
|
- lib/magnum-pi/version.rb
|
170
172
|
- magnum-pi.gemspec
|
171
173
|
- script/console
|
@@ -176,6 +178,7 @@ files:
|
|
176
178
|
- test/unit/api/test_instance.rb
|
177
179
|
- test/unit/api/test_resources.rb
|
178
180
|
- test/unit/api/test_scheme.rb
|
181
|
+
- test/unit/core_ext/test_ax.rb
|
179
182
|
- test/unit/core_ext/test_deep_clone.rb
|
180
183
|
- test/unit/test_api.rb
|
181
184
|
- test/unit/test_dsl.rb
|
@@ -211,6 +214,7 @@ test_files:
|
|
211
214
|
- test/unit/api/test_instance.rb
|
212
215
|
- test/unit/api/test_resources.rb
|
213
216
|
- test/unit/api/test_scheme.rb
|
217
|
+
- test/unit/core_ext/test_ax.rb
|
214
218
|
- test/unit/core_ext/test_deep_clone.rb
|
215
219
|
- test/unit/test_api.rb
|
216
220
|
- test/unit/test_dsl.rb
|