Class: Crabfarm::BaseState
- Inherits:
-
Object
- Object
- Crabfarm::BaseState
show all
- Extended by:
- Forwardable
- Includes:
- Assertion::Context
- Defined in:
- lib/crabfarm/base_state.rb
Constant Summary
collapse
- PARSE_METHOD_RX =
/^parse_(.*)$/
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#assert
Constructor Details
#initialize(_context, _params) ⇒ BaseState
Returns a new instance of BaseState.
26
27
28
29
30
31
32
|
# File 'lib/crabfarm/base_state.rb', line 26
def initialize(_context, _params)
@context = _context
@params = _params
@dsl = Strategies.load(:browser_dsl, class_browser_dsl || Crabfarm.config.browser_dsl)
@builder = Strategies.load(:output_builder, class_output_builder || Crabfarm.config.output_builder)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(_method, *_args, &_block) ⇒ Object
76
77
78
79
80
81
82
83
|
# File 'lib/crabfarm/base_state.rb', line 76
def method_missing(_method, *_args, &_block)
m = PARSE_METHOD_RX.match(_method)
if m
options = _args[1] || {}
options[:using] = (m[1].camelize + 'Parser').constantize
parse _args[0], options
else super end
end
|
Instance Attribute Details
#output ⇒ Object
Returns the value of attribute output.
12
13
14
|
# File 'lib/crabfarm/base_state.rb', line 12
def output
@output
end
|
#params ⇒ Object
Returns the value of attribute params.
12
13
14
|
# File 'lib/crabfarm/base_state.rb', line 12
def params
@params
end
|
Class Method Details
.browser_dsl(_dsl) ⇒ Object
18
19
20
|
# File 'lib/crabfarm/base_state.rb', line 18
def self.browser_dsl(_dsl)
@class_browser_dsl = _dsl
end
|
.output_builder(_builder) ⇒ Object
22
23
24
|
# File 'lib/crabfarm/base_state.rb', line 22
def self.output_builder(_builder)
@class_output_builder = _builder
end
|
Instance Method Details
#browser(_name = nil) ⇒ Object
34
35
36
|
# File 'lib/crabfarm/base_state.rb', line 34
def browser(_name=nil)
@dsl.wrap driver(_name)
end
|
#crawl ⇒ Object
50
51
52
|
# File 'lib/crabfarm/base_state.rb', line 50
def crawl
raise NotImplementedError.new
end
|
#download(_url) ⇒ Object
38
39
40
|
# File 'lib/crabfarm/base_state.rb', line 38
def download(_url)
@context.http.get(_url).body
end
|
#fork_each(_enumerator, &_block) ⇒ Object
66
67
68
69
70
71
72
73
74
|
# File 'lib/crabfarm/base_state.rb', line 66
def fork_each(_enumerator, &_block)
session_id = 0
mutex = Mutex.new
ths = _enumerator.map do |value|
session_id += 1
start_forked_state("th_session_#{session_id}", value, _block, mutex)
end
ThreadsWait.all_waits(*ths)
end
|
#output_as_json ⇒ Object
46
47
48
|
# File 'lib/crabfarm/base_state.rb', line 46
def output_as_json
@builder.serialize @output
end
|
#parse(_target = nil, _options = {}) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/crabfarm/base_state.rb', line 54
def parse(_target=nil, _options={})
parser_class = _options.delete :using
if parser_class.nil?
parser_class = (self.class.name + 'Parser').constantize
end
parser = parser_class.new _target, @params.merge(_options)
parser.parse
return parser
end
|
#respond_to?(_method, _include_all = false) ⇒ Boolean
85
86
87
88
|
# File 'lib/crabfarm/base_state.rb', line 85
def respond_to?(_method, _include_all=false)
return true if PARSE_METHOD_RX === _method
super
end
|