hyper_navigator 0.2.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/surf.rb +9 -3
- data/lib/hyper_navigator.rb +1 -1
- data/lib/hyper_navigator/node.rb +23 -28
- data/lib/hyper_navigator/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a75b9f286ad4f15c47234f38bc78ec3fb0ed862
|
4
|
+
data.tar.gz: bcefe082de297582f6f239c40e80fc2e393d0ecf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c7be6769655618746cb083c1f23137652a2150b77aeac7ecdd16dcdad54c5c003ded2c8649bb5395463603b71fded8f8fc5599c7a2855142ed58426200d226b
|
7
|
+
data.tar.gz: 50097470c15c94e5de2995c8823fa7862ea841285bd9fca25990b847c62f97afd8c1643bc1ef3ceb07d0ff30b273be2a8d5a8046938e36dd79b5ef08ba04b73b
|
data/examples/surf.rb
CHANGED
@@ -45,6 +45,12 @@ end
|
|
45
45
|
|
46
46
|
result = HyperNavigator.surf(options.url, options.pattern, options.headers, { :verbose => options.verbose })
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
print_tree = lambda do |node|
|
49
|
+
padding = ' ' * node.depth
|
50
|
+
puts "#{padding}#{node.rel} : #{node.href}"
|
51
|
+
node.descendants.each do |d|
|
52
|
+
print_tree.call(d)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
print_tree.call(result)
|
data/lib/hyper_navigator.rb
CHANGED
data/lib/hyper_navigator/node.rb
CHANGED
@@ -38,44 +38,44 @@ module HyperNavigator
|
|
38
38
|
|
39
39
|
def match_here(exp, node)
|
40
40
|
if exp == nil
|
41
|
-
return
|
41
|
+
return nil
|
42
42
|
elsif exp[1] == :star
|
43
43
|
match_star(exp[0], exp.drop(2), node)
|
44
44
|
return node
|
45
45
|
elsif exp[0] == :any
|
46
|
-
match_here_descendants(exp, node)
|
46
|
+
node.descendants = match_here_descendants(exp.drop(1), node)
|
47
47
|
return node
|
48
48
|
elsif exp[0] == node.rel
|
49
|
-
match_here_descendants(exp, node)
|
49
|
+
node.descendants = match_here_descendants(exp.drop(1), node)
|
50
50
|
return node
|
51
51
|
end
|
52
|
-
return
|
52
|
+
return nil
|
53
53
|
end
|
54
54
|
|
55
55
|
def match_here_descendants(exp, node)
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
return [] if exp == nil
|
57
|
+
nodes = node.links.map {|link| make_node(link, node.depth + 1)}
|
58
|
+
matched_nodes = nodes.map {|n| match_here(exp, n) }
|
59
|
+
return matched_nodes.compact
|
59
60
|
end
|
60
61
|
|
61
62
|
def match_star(exp_star, exp, node)
|
62
|
-
#
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
match_star_descendants(exp_star, exp, node)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def match_star_descendants(exp_star, exp, node)
|
71
|
-
if exp_star == :any
|
72
|
-
links = node.links
|
63
|
+
# exp can match here, ending the descent
|
64
|
+
exp_node_here = match_here(exp, node)
|
65
|
+
if exp_node_here
|
66
|
+
# finish the star match
|
73
67
|
else
|
74
|
-
|
68
|
+
nodes = match_here_descendants([exp_star], node)
|
69
|
+
|
70
|
+
# continue the star matching for each descendant
|
71
|
+
nodes.each {|n| match_star(exp_star, exp, n) }
|
72
|
+
if exp.empty?
|
73
|
+
node.descendants = nodes
|
74
|
+
else
|
75
|
+
# removing nodes that are not in the path to the next exp
|
76
|
+
node.descendants = nodes.reject {|n| n.descendants.empty? && n.rel != exp[0] }
|
77
|
+
end
|
75
78
|
end
|
76
|
-
|
77
|
-
node.descendants = links.map { |link| make_node(link, node.depth + 1) }
|
78
|
-
node.descendants.map {|desc| match_star(exp_star, exp, desc) }
|
79
79
|
end
|
80
80
|
|
81
81
|
def make_node(link, depth=nil)
|
@@ -90,7 +90,7 @@ module HyperNavigator
|
|
90
90
|
|
91
91
|
IGNORE_REFS = ["self", "up", "next", "prev"]
|
92
92
|
|
93
|
-
attr_reader :rel, :href, :
|
93
|
+
attr_reader :rel, :href, :response
|
94
94
|
attr_accessor :descendants, :depth
|
95
95
|
|
96
96
|
def initialize(rel, href, headers={}, depth=nil)
|
@@ -121,9 +121,4 @@ module HyperNavigator
|
|
121
121
|
|
122
122
|
end
|
123
123
|
|
124
|
-
class NullNode < Node
|
125
|
-
def initialize()
|
126
|
-
super(nil, nil)
|
127
|
-
end
|
128
|
-
end
|
129
124
|
end
|