klepto 0.2.8 → 0.2.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/klepto/structure.rb +15 -2
- data/lib/klepto/version.rb +1 -1
- data/spec/lib/klepto/bot_spec.rb +13 -0
- metadata +11 -11
data/README.md
CHANGED
@@ -183,8 +183,8 @@ end
|
|
183
183
|
* Add after(:all)
|
184
184
|
* :if, :unless for as: (:collection|:resource) to. context should be captured node that block is run against
|
185
185
|
* Access to hash from within a block (for bulk assignment of other attributes) ?
|
186
|
-
* :default => value|Proc should be an option
|
187
186
|
* config.allow_rescue_in_block #should exceptions in blocks be auto rescued with nil as the return value
|
187
|
+
* :default should be able to take a proc
|
188
188
|
|
189
189
|
Async
|
190
190
|
--------
|
data/lib/klepto/structure.rb
CHANGED
@@ -26,46 +26,59 @@ module Klepto
|
|
26
26
|
options[:syntax] ||= :css
|
27
27
|
options[:match] ||= :first
|
28
28
|
options[:attr] ||= nil
|
29
|
+
options[:default] ||= nil
|
29
30
|
options[:limit] ||= nil
|
30
31
|
selector = args.shift
|
31
32
|
|
32
33
|
if options[:as] == :collection
|
34
|
+
|
33
35
|
@_hash[meth] = []
|
34
36
|
result = _context.all( options[:syntax], selector )
|
35
37
|
options[:limit] ||= result.length
|
36
38
|
result[0, options[:limit]].each do |ele|
|
37
39
|
@_hash[meth].push Structure.build(ele, self, &block)
|
38
40
|
end
|
41
|
+
|
39
42
|
elsif options[:as] == :resource
|
43
|
+
|
40
44
|
result = _context.first( options[:syntax], selector )
|
41
45
|
@_hash[meth] = Structure.build(result, self, &block)
|
46
|
+
|
42
47
|
elsif block
|
48
|
+
|
43
49
|
result = selector ?
|
44
50
|
_context.send( options[:match], options[:syntax], selector ) : _context
|
45
51
|
|
46
52
|
if options[:match] == :all
|
53
|
+
|
47
54
|
@_hash[meth] = []
|
48
55
|
options[:limit] ||= result.length
|
49
56
|
result[0, options[:limit]].each do |node|
|
50
57
|
@_hash[meth] << block.call( node )
|
51
58
|
end
|
59
|
+
|
52
60
|
else
|
61
|
+
|
53
62
|
begin
|
54
63
|
@_hash[meth] = block.call( result )
|
55
64
|
rescue Exception => ex
|
56
|
-
@_hash[meth] =
|
65
|
+
@_hash[meth] = options[:default]
|
57
66
|
end
|
58
67
|
end
|
68
|
+
|
59
69
|
else
|
60
70
|
result = _context.send( options[:match], options[:syntax], selector )
|
71
|
+
|
61
72
|
if options[:match] == :all
|
62
73
|
@_hash[meth] = []
|
63
74
|
options[:limit] ||= result.length
|
64
75
|
result[0, options[:limit]].each do |node|
|
65
76
|
@_hash[meth] << (node[options[:attr]] || node.try(:text))
|
66
77
|
end
|
67
|
-
|
78
|
+
elsif result
|
68
79
|
@_hash[meth] = (result[options[:attr]] || result.try(:text))
|
80
|
+
else
|
81
|
+
@_hash[meth] = options[:default]
|
69
82
|
end
|
70
83
|
end
|
71
84
|
end
|
data/lib/klepto/version.rb
CHANGED
data/spec/lib/klepto/bot_spec.rb
CHANGED
@@ -201,6 +201,19 @@ describe Klepto::Bot do
|
|
201
201
|
end
|
202
202
|
end
|
203
203
|
|
204
|
+
describe 'a page missing a selector' do
|
205
|
+
before(:each) do
|
206
|
+
@bot = Klepto::Bot.new("https://twitter.com/justinbieber"){
|
207
|
+
name 'h1.fullname'
|
208
|
+
username "span.screen-NOPE", default: "CHICKENS"
|
209
|
+
}
|
210
|
+
@structure = @bot.resources
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'should have a sensible default for the structure' do
|
214
|
+
@structure.first[:username].should eq('CHICKENS')
|
215
|
+
end
|
216
|
+
end
|
204
217
|
|
205
218
|
describe 'creating a bot with a node limit' do
|
206
219
|
before(:each) do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: klepto
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2013-04-30 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: poltergeist
|
16
|
-
requirement: &
|
16
|
+
requirement: &70208580722140 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - =
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.1.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70208580722140
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: capybara
|
27
|
-
requirement: &
|
27
|
+
requirement: &70208580720340 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - =
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 2.0.2
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70208580720340
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: nokogiri
|
38
|
-
requirement: &
|
38
|
+
requirement: &70208580719740 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.5.6
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70208580719740
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: activesupport
|
49
|
-
requirement: &
|
49
|
+
requirement: &70208580719040 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70208580719040
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: multi_json
|
60
|
-
requirement: &
|
60
|
+
requirement: &70208580716800 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '1.0'
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70208580716800
|
69
69
|
description: Tearing up web pages into ActiveRecord resources
|
70
70
|
email:
|
71
71
|
- github@coryodaniel.com
|