Module: Scruber::Core::Extensions::MongoOutput::CoreMethods

Defined in:
lib/scruber/core/extensions/mongo_output.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/scruber/core/extensions/mongo_output.rb', line 69

def self.included(base)
  Scruber::Core::Crawler.register_method_missing /\Amongo_out_(\w+)\Z/ do |meth, scan_results, args|
    suffix = scan_results.first.first.to_sym
    fields, options = args.first
    fields = {} if fields.nil?
    Scruber::Core::Crawler.class_eval do
      define_method "mongo_out_#{suffix}".to_sym do |fields, opts={}|
        Scruber::Core::Extensions::MongoOutput.mongo_out(self.scraper_name, suffix, fields, opts)
      end
    end
    Scruber::Core::Extensions::MongoOutput.mongo_out(self.scraper_name, suffix, fields, options)
  end
  Scruber::Core::Crawler.register_method_missing /\Amongo_find_(\w+)\Z/ do |meth, scan_results, args|
    suffix = scan_results.first.first.to_sym
    id = args.first
    Scruber::Core::Crawler.class_eval do
      define_method "mongo_find_#{suffix}".to_sym do |id|
        Scruber::Core::Extensions::MongoOutput.mongo_find(self.scraper_name, suffix, id)
      end
    end
    Scruber::Core::Extensions::MongoOutput.mongo_find(self.scraper_name, suffix, id)
  end
  Scruber::Core::Crawler.register_method_missing /\Amongo_(\w+)_collection\Z/ do |meth, scan_results, args|
    suffix = scan_results.first.first.to_sym
    Scruber::Core::Crawler.class_eval do
      define_method "mongo_#{suffix}_collection".to_sym do
        Scruber::Core::Extensions::MongoOutput.mongo_collection(self.scraper_name, suffix)
      end
    end
    Scruber::Core::Extensions::MongoOutput.mongo_collection(self.scraper_name, suffix)
  end
end

Instance Method Details

#mongo_collectionMongo::Collection

Direct access to mongo collection

Returns:

  • (Mongo::Collection)

    Mongo collection instance



65
66
67
# File 'lib/scruber/core/extensions/mongo_output.rb', line 65

def mongo_collection
  Scruber::Core::Extensions::MongoOutput.mongo_collection self.scraper_name, Scruber::Core::Extensions::MongoOutput.default_suffix_name
end

#mongo_find(id) ⇒ Hash

Find mongo document by id

Parameters:

  • id (Object)

    id of document

Returns:

  • (Hash)

    mongo document



57
58
59
# File 'lib/scruber/core/extensions/mongo_output.rb', line 57

def mongo_find(id)
  Scruber::Core::Extensions::MongoOutput.mongo_find self.scraper_name, Scruber::Core::Extensions::MongoOutput.default_suffix_name, id
end

#mongo_out(fields, options = {}) ⇒ Object

Mongo out default method. By default it uses suffix _records

Parameters:

Returns:

  • (Object)

    id of writed record



47
48
49
# File 'lib/scruber/core/extensions/mongo_output.rb', line 47

def mongo_out(fields, options={})
  Scruber::Core::Extensions::MongoOutput.mongo_out self.scraper_name, Scruber::Core::Extensions::MongoOutput.default_suffix_name, fields, options
end