klepto 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/klepto/bot.rb +17 -1
- data/lib/klepto/structure.rb +5 -0
- data/lib/klepto/version.rb +1 -1
- data/spec/lib/klepto/bot_spec.rb +42 -0
- metadata +2 -2
data/lib/klepto/bot.rb
CHANGED
@@ -1,9 +1,24 @@
|
|
1
1
|
module Klepto
|
2
2
|
class Bot
|
3
|
-
attr_reader :config
|
3
|
+
attr_reader :config, :vars
|
4
|
+
|
5
|
+
def set(hash)
|
6
|
+
@vars = vars.merge(hash)
|
7
|
+
end
|
8
|
+
|
9
|
+
def get(key)
|
10
|
+
val = vars[key]
|
11
|
+
|
12
|
+
if val.is_a? Proc
|
13
|
+
val = val.call(@browser.page)
|
14
|
+
set(key => val)
|
15
|
+
end
|
16
|
+
val
|
17
|
+
end
|
4
18
|
|
5
19
|
def initialize(url=nil, &block)
|
6
20
|
@config = Klepto::Config.new
|
21
|
+
@vars = {}
|
7
22
|
@config.url url
|
8
23
|
@queue = []
|
9
24
|
@browser = Klepto::Browser.new
|
@@ -92,6 +107,7 @@ EOS
|
|
92
107
|
|
93
108
|
def __structure(context)
|
94
109
|
structure = Structure.new(context)
|
110
|
+
structure._bot = self
|
95
111
|
|
96
112
|
# A queue of DSL instructions
|
97
113
|
queue.each do |instruction|
|
data/lib/klepto/structure.rb
CHANGED
@@ -9,6 +9,7 @@ module Klepto
|
|
9
9
|
attr_reader :_parent
|
10
10
|
attr_reader :_hash
|
11
11
|
attr_reader :_context
|
12
|
+
attr_accessor :_bot
|
12
13
|
|
13
14
|
def initialize(_context=nil, _parent=nil)
|
14
15
|
Klepto.logger.debug("\tnew Structure (#{_parent}) -> (#{_context})")
|
@@ -32,6 +33,10 @@ module Klepto
|
|
32
33
|
options[:parser] ||= nil
|
33
34
|
selector = args.shift
|
34
35
|
|
36
|
+
if selector.is_a?(Proc)
|
37
|
+
selector = selector.call(_bot)
|
38
|
+
end
|
39
|
+
|
35
40
|
if !block_given? && options[:parser]
|
36
41
|
block = options[:parser].new
|
37
42
|
end
|
data/lib/klepto/version.rb
CHANGED
data/spec/lib/klepto/bot_spec.rb
CHANGED
@@ -2,6 +2,48 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Klepto::Bot do
|
4
4
|
describe 'Klepto::Bot.new' do
|
5
|
+
describe 'vars' do
|
6
|
+
describe 'structuring with a variable' do
|
7
|
+
before(:each) do
|
8
|
+
@bot = Klepto::Bot.new("https://www.twitter.com/justinbieber"){
|
9
|
+
name 'h1.fullname'
|
10
|
+
set :status_id => lambda{|doc|
|
11
|
+
doc.first('.stream-items li.stream-item')['data-item-id'].to_i
|
12
|
+
}
|
13
|
+
|
14
|
+
got_the_node(lambda{|bot| "[data-item-id='#{bot.get(:status_id)}']"}) do |node|
|
15
|
+
true
|
16
|
+
end
|
17
|
+
get_the_id 'a' do |node|
|
18
|
+
get(:status_id)
|
19
|
+
end
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should have the variables' do
|
24
|
+
@bot.get(:status_id).should be_kind_of(Integer)
|
25
|
+
@bot.get(:status_id).should_not be(0)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should be able to structure with the variable' do
|
29
|
+
@bot.structure[:get_the_id].should be @bot.get(:status_id)
|
30
|
+
@bot.structure[:got_the_node].should be true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
describe 'setting a variable' do
|
34
|
+
before(:each) do
|
35
|
+
@bot = Klepto::Bot.new("https://www.twitter.com/justinbieber"){
|
36
|
+
name 'h1.fullname'
|
37
|
+
set :cool => 'chickens'
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should have the variables' do
|
42
|
+
@bot.get(:cool).should eq 'chickens'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
5
47
|
describe 'create a bot with a redirect' do
|
6
48
|
describe 'that aborts on redirect' do
|
7
49
|
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.7.
|
4
|
+
version: 0.7.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: poltergeist
|