Module: AeEasy::Core::Mock::FakeExecutor
- Includes:
- AnswersEngine::Plugin::ContextExposer
- Included in:
- FakeParser, FakeSeeder
- Defined in:
- lib/ae_easy/core/mock/fake_executor.rb
Overview
Fake executor that emulates `AnswersEngine` executor.
Constant Summary collapse
- MAX_FIND_OUTPUTS_PER_PAGE =
Max allowed page size when query outputs (see #find_outputs).
500
Instance Attribute Summary collapse
-
#content ⇒ String?
Page content.
-
#failed_content ⇒ String?
Failed page content.
Instance Method Summary collapse
-
#db ⇒ Object
Fake database to represent what it is saved.
-
#execute_script(file_path, vars = {}) ⇒ Object
Execute an script file as an executor.
-
#find_output(collection = 'default', query = {}, opts = {}) ⇒ Hash?
Find one output by collection and query with pagination.
-
#find_outputs(collection = 'default', query = {}, page = 1, per_page = 30, opts = {}) ⇒ Array
Find outputs by collection and query with pagination.
-
#flush ⇒ Object
Save all drafts into db and clear draft queues.
-
#flush_outputs ⇒ Object
Save draft outputs into db and clear draft queue.
-
#flush_pages ⇒ Object
Save draft pages into db and clear draft queue.
-
#initialize(opts = {}) ⇒ Object
Initialize object.
-
#job_id ⇒ Integer?
Fake job ID used by executor.
-
#job_id=(value) ⇒ Object
Set fake job ID value.
-
#latest_job_by(scraper_name, filter = {}) ⇒ Hash?
Get latest job by scraper_name.
-
#outputs ⇒ Array
Draft outputs, usually get saved after execution.
-
#page ⇒ Hash?
Current page used by executor.
-
#page=(value) ⇒ Object
Set current page.
-
#pages ⇒ Array
Draft pages, usually get saved after execution.
-
#save_jobs(list) ⇒ Object
Save a job collection on db and remove all the element from
list
. -
#save_outputs(list) ⇒ Object
Save an output collection on db and remove all the element from
list
. -
#save_pages(list) ⇒ Object
Save a page collection on db and remove all the element from
list
. -
#saved_jobs ⇒ Object
Retrive a list of saved jobs.
-
#saved_outputs ⇒ Object
Retrive a list of saved outputs.
-
#saved_pages ⇒ Object
Retrive a list of saved pages.
-
#scraper_name ⇒ Integer?
Fake scraper name used by executor.
-
#scraper_name=(value) ⇒ Object
Set fake scraper name value.
Instance Attribute Details
#content ⇒ String?
Page content.
13 14 15 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 13 def content @content end |
#failed_content ⇒ String?
Failed page content.
16 17 18 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 16 def failed_content @failed_content end |
Instance Method Details
#db ⇒ Object
Fake database to represent what it is saved.
75 76 77 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 75 def db @db ||= AeEasy::Core::Mock::FakeDb.new end |
#execute_script(file_path, vars = {}) ⇒ Object
Execute an script file as an executor.
305 306 307 308 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 305 def execute_script file_path, vars = {} eval(File.read(file_path), isolated_binding(vars), file_path) flush end |
#find_output(collection = 'default', query = {}, opts = {}) ⇒ Hash?
*opts `:job_id` option is prioritize over `:scraper_name` when both exists. If none add provided or nil values, then current job will be used to query instead, this is the defaul behavior.
Find one output by collection and query with pagination.
297 298 299 300 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 297 def find_output collection = 'default', query = {}, opts = {} result = find_outputs(collection, query, 1, 1, opts) result.nil? ? nil : result.first end |
#find_outputs(collection = 'default', query = {}, page = 1, per_page = 30, opts = {}) ⇒ Array
*opts `:job_id` option is prioritize over `:scraper_name` when both exists. If none add provided or nil values, then current job will be used to query instead, this is the defaul behavior.
Find outputs by collection and query with pagination.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 249 def find_outputs collection = 'default', query = {}, page = 1, per_page = 30, opts = {} raise ArgumentError.new("collection needs to be a String.") unless collection.is_a?(String) raise ArgumentError.new("query needs to be a Hash.") unless query.is_a?(Hash) unless page.is_a?(Integer) && page > 0 raise ArgumentError.new("page needs to be an Integer greater than 0.") end unless per_page.is_a?(Integer) && per_page > 0 && per_page <= MAX_FIND_OUTPUTS_PER_PAGE raise ArgumentError.new("per_page needs to be an Integer between 1 and #{MAX_FIND_OUTPUTS_PER_PAGE}.") end count = 0 offset = (page - 1) * per_page job = latest_job_by(opts[:scraper_name]) fixed_query = query.merge( '_collection' => collection, '_job_id' => opts[:job_id] || (job.nil? ? job_id : job['job_id']) ) db.query :outputs, fixed_query, offset, per_page end |
#flush ⇒ Object
Save all drafts into db and clear draft queues.
195 196 197 198 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 195 def flush flush_pages flush_outputs end |
#flush_outputs ⇒ Object
Save draft outputs into db and clear draft queue.
189 190 191 192 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 189 def flush_outputs save_outputs outputs clear_draft_outputs end |
#flush_pages ⇒ Object
Save draft pages into db and clear draft queue.
183 184 185 186 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 183 def flush_pages save_pages pages clear_draft_pages end |
#initialize(opts = {}) ⇒ Object
Initialize object.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 88 def initialize opts = {} unless opts[:pages].nil? || opts[:pages].is_a?(Array) raise ArgumentError.new "Pages must be an array." end @pages = opts[:pages] unless opts[:outputs].nil? || opts[:outputs].is_a?(Array) raise ArgumentError.new "Outputs must be an array." end @outputs = opts[:outputs] self.job_id = opts[:job_id] self.scraper_name = opts[:scraper_name] self.page = opts[:page] end |
#job_id ⇒ Integer?
Fake job ID used by executor.
115 116 117 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 115 def job_id db.job_id end |
#job_id=(value) ⇒ Object
Set fake job ID value.
120 121 122 123 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 120 def job_id= value db.job_id = value page['job_id'] = value end |
#latest_job_by(scraper_name, filter = {}) ⇒ Hash?
Get latest job by scraper_name.
207 208 209 210 211 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 207 def latest_job_by scraper_name, filter = {} return nil if scraper_name.nil? data = db.query :jobs, filter.merge('scraper_name' => scraper_name) data.max{|a,b| a['created_at'] <=> b['created_at']} end |
#outputs ⇒ Array
Draft outputs, usually get saved after execution.
58 59 60 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 58 def outputs @outputs ||= [] end |
#page ⇒ Hash?
Current page used by executor.
127 128 129 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 127 def page @page ||= AeEasy::Core::Mock::FakeDb.build_fake_page job_id: job_id end |
#page=(value) ⇒ Object
Set current page.
132 133 134 135 136 137 138 139 140 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 132 def page= value unless value.nil? value = AeEasy::Core::Mock::FakeDb.build_page value self.job_id = value['job_id'] unless value['job_id'].nil? value['job_id'] ||= job_id db.page_gid = value['gid'] unless value['gid'].nil? end @page = value end |
#pages ⇒ Array
Draft pages, usually get saved after execution.
52 53 54 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 52 def pages @pages ||= [] end |
#save_jobs(list) ⇒ Object
Save a job collection on db and remove all the element from
list
.
160 161 162 163 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 160 def save_jobs list list.each{|job| db.jobs << job} list.clear end |
#save_outputs(list) ⇒ Object
Save an output collection on db and remove all the element from
+list+.
177 178 179 180 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 177 def save_outputs list list.each{|output| db.outputs << output} list.clear end |
#save_pages(list) ⇒ Object
Save a page collection on db and remove all the element from
list
.
168 169 170 171 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 168 def save_pages list list.each{|page| db.pages << page} list.clear end |
#saved_jobs ⇒ Object
Retrive a list of saved jobs.
143 144 145 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 143 def saved_jobs db.jobs end |
#saved_outputs ⇒ Object
Retrive a list of saved outputs.
153 154 155 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 153 def saved_outputs db.outputs end |
#saved_pages ⇒ Object
Retrive a list of saved pages. Drafted pages can be included.
148 149 150 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 148 def saved_pages db.pages end |
#scraper_name ⇒ Integer?
Fake scraper name used by executor.
104 105 106 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 104 def scraper_name db.scraper_name end |
#scraper_name=(value) ⇒ Object
Set fake scraper name value.
109 110 111 |
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 109 def scraper_name= value db.scraper_name = value end |