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

Instance Method Summary collapse

Instance Attribute Details

#contentString?

Page content.

Returns:

  • (String, nil)


13
14
15
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 13

def content
  @content
end

#failed_contentString?

Failed page content.

Returns:

  • (String, nil)


16
17
18
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 16

def failed_content
  @failed_content
end

Instance Method Details

#dbObject

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.

Parameters:

  • file_path (String)

    Script file path to execute.



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?

Note:

*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.

Examples:

find_output
find_output 'my_collection'
find_output 'my_collection', {}

Find from another scraper by name

find_output 'my_collection', {}, scraper_name: 'my_scraper'

Find from another scraper by job_id

find_output 'my_collection', {}, job_id: 123

Parameters:

  • collection (String) (defaults to: 'default')

    ('default') Collection name.

  • query (Hash) (defaults to: {})

    ({}) Filters to query.

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :scraper_name (String, nil) — default: nil

    Scraper name to query from.

  • :job_id (Integer, nil) — default: nil

    Job's id to query from.

Returns:

  • (Hash, nil)

Raises:

  • (ArgumentError)

    collection is not String.

  • (ArgumentError)

    query is not a Hash.



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

Note:

*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.

Examples:

find_outputs
find_outputs 'my_collection'
find_outputs 'my_collection', {}
find_outputs 'my_collection', {}, 1
find_outputs 'my_collection', {}, 1, 30

Find from another scraper by name

find_outputs 'my_collection', {}, 1, 30, scraper_name: 'my_scraper'

Find from another scraper by job_id

find_outputs 'my_collection', {}, 1, 30, job_id: 123

Parameters:

  • collection (String) (defaults to: 'default')

    ('default') Collection name.

  • query (Hash) (defaults to: {})

    ({}) Filters to query.

  • page (Integer) (defaults to: 1)

    (1) Page number.

  • per_page (Integer) (defaults to: 30)

    (30) Page size.

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :scraper_name (String, nil) — default: nil

    Scraper name to query from.

  • :job_id (Integer, nil) — default: nil

    Job's id to query from.

Returns:

  • (Array)

Raises:

  • (ArgumentError)

    collection is not String.

  • (ArgumentError)

    query is not a Hash.

  • (ArgumentError)

    page is not an Integer greater than 0.

  • (ArgumentError)

    per_page is not an Integer between 1 and 500.



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

#flushObject

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_outputsObject

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_pagesObject

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.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :pages (Array) — default: nil

    Array to initialize pages, can be nil for empty.

  • :outputs (Array) — default: nil

    Array to initialize outputs, can be nil for empty.

  • :job_id (Integer) — default: nil

    A number to represent the job_id.

  • :page (Hash) — default: nil

    Current page.

Raises:

  • (ArgumentError)

    When pages or outputs are not Array.



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_idInteger?

Fake job ID used by executor.

Returns:

  • (Integer, nil)


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.

Parameters:

  • scraper_name (String)

    Scraper name.

  • filter (Hash) (defaults to: {})

    ({}) Additional_filters.

Returns:

  • (Hash, nil)

    Return nil if no scraper_name or scraper_name is nil.



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

#outputsArray

Draft outputs, usually get saved after execution.

Returns:

  • (Array)


58
59
60
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 58

def outputs
  @outputs ||= []
end

#pageHash?

Current page used by executor.

Returns:

  • (Hash, nil)


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

#pagesArray

Draft pages, usually get saved after execution.

Returns:

  • (Array)


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.

Parameters:

  • list (Array)

    Collection of jobs to save.



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+.

Parameters:

  • list (Array)

    Collection of outputs to save.



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.

Parameters:

  • list (Array)

    Collection of pages to save.



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_jobsObject

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_outputsObject

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_pagesObject

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_nameInteger?

Fake scraper name used by executor.

Returns:

  • (Integer, nil)


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