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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f83d77d20b8bbb093c8a1cc3e32a49e1079ebcb
4
- data.tar.gz: cae40da73f186e5ce41d4a9f07dd9551bcce4695
3
+ metadata.gz: 1a75b9f286ad4f15c47234f38bc78ec3fb0ed862
4
+ data.tar.gz: bcefe082de297582f6f239c40e80fc2e393d0ecf
5
5
  SHA512:
6
- metadata.gz: 42fb7fc920df883e1965f6c316086812a2f0daafbcaab385091d163cbc801bcd8bfdebcd115a80f4dd6f5dbabbd01d3b87b17dff598ea07324e6933868a7c62f
7
- data.tar.gz: c44fe57cf3cf77c4c769e6222d0d6c5b6d666cc90512ab9e1c163584aa549c3202abaf44b8e2bae38177802c9ae508fe0beb3dd5567ecc3d13cb077e8126c24a
6
+ metadata.gz: 3c7be6769655618746cb083c1f23137652a2150b77aeac7ecdd16dcdad54c5c003ded2c8649bb5395463603b71fded8f8fc5599c7a2855142ed58426200d226b
7
+ data.tar.gz: 50097470c15c94e5de2995c8823fa7862ea841285bd9fca25990b847c62f97afd8c1643bc1ef3ceb07d0ff30b273be2a8d5a8046938e36dd79b5ef08ba04b73b
@@ -45,6 +45,12 @@ end
45
45
 
46
46
  result = HyperNavigator.surf(options.url, options.pattern, options.headers, { :verbose => options.verbose })
47
47
 
48
- result.map do |n|
49
- pp JSON.parse(n.response.body) rescue nil
50
- end
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)
@@ -4,7 +4,7 @@ require "hyper_navigator/node"
4
4
  module HyperNavigator
5
5
 
6
6
  def self.surf(root_url, exp, headers={}, options={})
7
- PatternMatcher.new(headers, options).match(root_url, exp).flatten_branch
7
+ PatternMatcher.new(headers, options).match(root_url, exp)
8
8
  end
9
9
 
10
10
  end
@@ -38,44 +38,44 @@ module HyperNavigator
38
38
 
39
39
  def match_here(exp, node)
40
40
  if exp == nil
41
- return node
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 NullNode.new
52
+ return nil
53
53
  end
54
54
 
55
55
  def match_here_descendants(exp, node)
56
- links = node.links.map {|link| make_node(link, node.depth + 1)}
57
- descendants = links.map {|n| match_here(exp.drop(1), n) }
58
- node.descendants = descendants.select {|d| d.class == Node }
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
- # in case of zero matches, exp can match here
63
- node_here = match_here(exp, node)
64
-
65
- if node_here.is_a? NullNode
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
- links = node.links.select { |link| link["rel"] == exp_star }
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, :headers, :response
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
@@ -1,3 +1,3 @@
1
1
  module HyperNavigator
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyper_navigator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Douglas