ruby-query 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -14,70 +14,59 @@
14
14
  Extract an attribute from a webpage:
15
15
 
16
16
  require 'open-uri'
17
- require 'ruby-query'
17
+ require 'ruby_query'
18
18
 
19
- RQuery::Query.query(open("http://twitter.com"), "a#logo img", "attr", "alt")
19
+ RubyQuery::Query.query(open("http://twitter.com"), "a#logo img", "attr", "alt")
20
20
  => "Twitter"
21
21
 
22
22
  Extract a form input value :
23
23
 
24
- require 'ruby-query'
24
+ require 'ruby_query'
25
25
 
26
- RQuery::Query.query('<input type="text" value="tj@vision-media.ca"/>', "input", "val")
26
+ RubyQuery::Query.query('<input type="text" value="tj@vision-media.ca"/>', "input", "val")
27
27
  => "tj@vision-media.ca"
28
28
 
29
29
  ## Command Line Examples
30
30
 
31
31
  Twitter logo alt text:
32
32
 
33
- $ curl http://twitter.com | query 'a#logo img' attr alt
33
+ $ curl http://twitter.com | rquery 'a#logo img' attr alt
34
34
  Twitter
35
35
 
36
36
  Alternately, since the output is simply more html, we can achieve this same result via pipes:
37
37
 
38
- $ curl http://twitter.com | query 'a#logo' | query img attr alt
38
+ $ curl http://twitter.com | rquery 'a#logo' | rquery img attr alt
39
39
  Twitter
40
40
 
41
41
  Check if a class is present:
42
42
 
43
- $ curl http://twitter.com | query .article '#timeline' hasClass statuses
43
+ $ curl http://twitter.com | rquery '.article #timeline' hasClass statuses
44
44
  true
45
-
46
- $ echo $?
47
- 0
48
-
49
- Exit status for bools:
50
-
51
- $ echo '<div class="foo bar"></div>' | ./index.js div hasClass baz
52
- false
53
-
54
- $ echo $?
55
- 1
56
45
 
57
46
  Grab width or height attributes:
58
47
 
59
- $ echo '<div class="user" width="300"></div>' | query div.user width
48
+ $ echo '<div class="user" width="300"></div>' | rquery div.user width
60
49
  300
61
50
 
62
51
  Output element text:
63
52
 
64
- $ echo '<p>very <em>slick</em></p>' | query p text
53
+ $ echo '<p>very <em>slick</em></p>' | rquery p text
65
54
  very slick
66
55
 
67
56
  Values:
68
57
 
69
- $ echo '<input type="text" value="tj@vision-media.ca"/>' | query input val
58
+ $ echo '<input type="text" value="tj@vision-media.ca"/>' | rquery input val
70
59
  tj@vision-media.ca
71
60
 
72
61
  Get second li's text:
73
62
 
74
- $ echo $list | query ul li get 1 text
75
- two
63
+ $ echo '<ul><li>Apple</li><li>Orange</li><li>Cat</li></ul>' | rquery ul li get 1 text
64
+ Orange
76
65
 
77
66
  Get third li's text using `next`:
78
67
 
79
- $ echo $list | query ul li get 1 next text
80
- three
68
+ $ echo '<ul><li>Apple</li><li>Orange</li><li>Cat</li></ul>' | rquery ul li get 1 next text
69
+ Cat
81
70
 
82
71
  Get length:
83
72
 
@@ -35,7 +35,12 @@ module RubyQuery
35
35
  :next_sibling => {:type => 'traverse'},
36
36
  :previous_sibling => {:type => 'traverse'},
37
37
  :at => {:type => 'traverse', :arity => 1},
38
-
38
+
39
+ :search => {:type => 'proc',
40
+ :proc => Proc.new {|context, param, query|
41
+ context.css(param)
42
+ }
43
+ },
39
44
  :attr => {:type => 'proc',
40
45
  :arity => 1,
41
46
  :proc => Proc.new {|context, param, query|
@@ -58,21 +63,32 @@ module RubyQuery
58
63
 
59
64
  ALIAS = {
60
65
  :html => :to_html,
61
- :len => :length,
66
+ :len => :size,
67
+ :length => :size,
62
68
  :count => :size,
63
69
  :get => :at,
64
70
  :"has-class" => :hasClass,
65
- :val => :value
71
+ :val => :value,
72
+ :next => :next_sibling,
73
+ :previous => :previous_sibling
66
74
  }
67
75
 
68
76
  def self.query(html, *query)
69
- doc = Nokogiri::HTML(html)
70
- ctx = doc.search(query.shift)
77
+ ctx = Nokogiri::HTML(html)
71
78
 
72
79
  while method = query.shift
73
- command = COMMANDS[method.to_sym] || COMMANDS[ALIAS[method.to_sym]] || COMMANDS[:to_html]
74
- param = query.length > 0 ? (command[:arity] ? query.shift : nil) : nil
75
- ctx = handle_command(ctx, ALIAS[method.to_sym] || method.to_sym, command[:type], param, query)
80
+ if COMMANDS.keys.include?(method.to_sym) || ALIAS.keys.include?(method.to_sym)
81
+ command = COMMANDS[method.to_sym] || COMMANDS[ALIAS[method.to_sym]]
82
+ param = query.length > 0 ? (command[:arity] ? query.shift : nil) : nil
83
+ ctx = handle_command(ctx, ALIAS[method.to_sym] || method.to_sym, command[:type], param, query)
84
+
85
+ elsif
86
+ command = COMMANDS[:search]
87
+ ctx = handle_command(ctx, :search, command[:type], method, query)
88
+
89
+ end
90
+
91
+ ctx
76
92
  end
77
93
 
78
94
  if ctx.is_a?(Nokogiri::XML::Element) || ctx.is_a?(Nokogiri::XML::NodeSet)
@@ -116,7 +132,7 @@ module RubyQuery
116
132
  end
117
133
  context[param.to_i]
118
134
  else
119
- if context.is_a?(Nokogiri::XML::Element) || context.is_a?(Nokogiri::XML::NodeSet)
135
+ if context.is_a?(Nokogiri::XML::Element) || context.is_a?(Nokogiri::XML::NodeSet) || context.is_a?(Nokogiri::HTML::Document)
120
136
  if param
121
137
  context.send(name, param)
122
138
  else
@@ -44,11 +44,13 @@ describe RubyQuery do
44
44
 
45
45
  it "get second li's text" do
46
46
  RubyQuery::Query.query(HTML_LIST, "ul li", "get", "1", "text").should == "Orange"
47
+ RubyQuery::Query.query(HTML_LIST, "ul", "li", "get", "1", "text").should == "Orange"
47
48
  RubyQuery::Query.query(HTML_LIST, "ul li", "get", "10", "text").should be_nil
48
49
  end
49
50
 
50
51
  it "get third li's text using next" do
51
52
  RubyQuery::Query.query(HTML_LIST, "ul li", "get", "1", "next", "text").should == "Cat"
53
+ RubyQuery::Query.query(HTML_LIST, "ul", "li", "get", "1", "next", "text").should == "Cat"
52
54
  end
53
55
 
54
56
  it "get length" do
@@ -62,4 +64,8 @@ describe RubyQuery do
62
64
  RubyQuery::Query.query(HTML_LIST, "li", "html").should == "<li>Apple</li>\n<li>Orange</li>\n<li>Cat</li>"
63
65
  end
64
66
 
67
+ it "should return empty for strange query" do
68
+ RubyQuery::Query.query(HTML_LIST, "liaa").should be_empty
69
+ RubyQuery::Query.query(HTML_LIST, "lixaa", "len").should 0
70
+ end
65
71
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-query
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Francis Chong