Class: Scruber::QueueAdapters::Mongo::Page

Inherits:
AbstractAdapter::Page
  • Object
show all
Defined in:
lib/scruber/queue_adapters/mongo.rb

Instance Method Summary collapse

Instance Method Details

#attrsHash

Generating hash with mongo doc attributes

Returns:

  • (Hash)

    hash with page attributes



52
53
54
# File 'lib/scruber/queue_adapters/mongo.rb', line 52

def attrs
  @options.with_indifferent_access.except('id', '_id').merge(id.present? ? {_id: id} : {}).merge (instance_variables.select{|ivar| !(ivar.to_s =~ /\@_/) }-[:@options, :@queue]).inject({}){|acc,ivar| acc[ivar[1..-1]] = instance_variable_get(ivar);acc }.with_indifferent_access
end

#deletevoid

This method returns an undefined value.

Delete record from Mongo collection



60
61
62
# File 'lib/scruber/queue_adapters/mongo.rb', line 60

def delete
  @queue.collection.find({"_id" => self.id }).delete_one if self.id.present?
end

#idObject



6
7
8
# File 'lib/scruber/queue_adapters/mongo.rb', line 6

def id
  @options[:_id] || @id
end

#processed!void

This method returns an undefined value.

Mark page as processed by parser and save it



40
41
42
43
44
45
46
# File 'lib/scruber/queue_adapters/mongo.rb', line 40

def processed!
  # Monkey patch for processing error pages.
  if @fetched_at == 0
    @fetched_at = -1
  end
  super
end

#save(options = {}, save_options = {}) ⇒ type

Saving page to queue

Parameters:

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

    saving options

  • save_options={}
    type
    description

Returns:

  • (type)
    description


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/scruber/queue_adapters/mongo.rb', line 16

def save(options={}, save_options={})
  if id.blank?
    @queue.collection.insert_one(attrs)
  else
    if options[:new]
      @queue.collection.find_one_and_update(
        {"_id" => self.id },
        {'$setOnInsert' => attrs },
        {return_document: :after, upsert: true, projection: {_id: 1}}.merge(options)
      )
    else
      @queue.collection.find_one_and_update(
        {"_id" => self.id },
        {'$set' => attrs },
        {return_document: :after, upsert: true, projection: {_id: 1}}.merge(options)
      )
    end
  end
end