Class: Crabfarm::BaseState
- Inherits:
-
Object
- Object
- Crabfarm::BaseState
- Extended by:
- Forwardable
- Defined in:
- lib/crabfarm/base_state.rb
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
Instance Method Summary collapse
- #alert(_msg) ⇒ Object
- #browser(_name = nil) ⇒ Object
- #crawl ⇒ Object
- #event(_type, _msg) ⇒ Object
- #fork_each(_enumerator, &_block) ⇒ Object
- #info(_msg) ⇒ Object
-
#initialize(_pool, _store, _params) ⇒ BaseState
constructor
A new instance of BaseState.
- #output_as_json ⇒ Object
Constructor Details
#initialize(_pool, _store, _params) ⇒ BaseState
Returns a new instance of BaseState.
21 22 23 24 25 26 27 28 29 |
# File 'lib/crabfarm/base_state.rb', line 21 def initialize(_pool, _store, _params) @pool = _pool @store = _store @params = _params @events = [] @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 |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
8 9 10 |
# File 'lib/crabfarm/base_state.rb', line 8 def output @output end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/crabfarm/base_state.rb', line 8 def params @params end |
Class Method Details
.browser_dsl(_dsl) ⇒ Object
13 14 15 |
# File 'lib/crabfarm/base_state.rb', line 13 def self.browser_dsl(_dsl) @class_browser_dsl = _dsl end |
.output_builder(_builder) ⇒ Object
17 18 19 |
# File 'lib/crabfarm/base_state.rb', line 17 def self.output_builder(_builder) @class_output_builder = _builder end |
Instance Method Details
#alert(_msg) ⇒ Object
51 52 53 |
# File 'lib/crabfarm/base_state.rb', line 51 def alert(_msg) event(:alert, _msg) end |
#browser(_name = nil) ⇒ Object
31 32 33 |
# File 'lib/crabfarm/base_state.rb', line 31 def browser(_name=nil) @dsl.wrap driver(_name) end |
#crawl ⇒ Object
43 44 45 |
# File 'lib/crabfarm/base_state.rb', line 43 def crawl raise NotImplementedError.new end |
#event(_type, _msg) ⇒ Object
47 48 49 |
# File 'lib/crabfarm/base_state.rb', line 47 def event(_type, _msg) @events << { created_at: Time.current, type: _type, msg: _msg } end |
#fork_each(_enumerator, &_block) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/crabfarm/base_state.rb', line 59 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 |
#info(_msg) ⇒ Object
55 56 57 |
# File 'lib/crabfarm/base_state.rb', line 55 def info(_msg) event(:info, _msg) end |
#output_as_json ⇒ Object
39 40 41 |
# File 'lib/crabfarm/base_state.rb', line 39 def output_as_json @builder.serialize @output end |