Class: NewsCrawler::Storage::RawData::MongoStorage

Inherits:
RawDataEngine
  • Object
show all
Includes:
Mongo
Defined in:
lib/news_crawler/storage/raw_data/mongo_storage.rb

Overview

Raw data storage implement using MongoDB

Constant Summary collapse

NAME =
'mongo'

Instance Method Summary collapse

Methods inherited from RawDataEngine

get_engines, inherited

Constructor Details

#initialize(*opts) ⇒ MongoStorage

Returns a new instance of MongoStorage.



37
38
39
40
41
42
43
# File 'lib/news_crawler/storage/raw_data/mongo_storage.rb', line 37

def initialize(*opts)
  config = (SimpleConfig.for :application)
  client = MongoClient.new(config.mongodb.host, config.mongodb.port)
  db = client[config.mongodb.db_name]
  @coll = db[config.prefix + '_' + config.suffix.raw_data]
  @coll.ensure_index({:url => Mongo::ASCENDING}, {:unique => true})
end

Instance Method Details

#add(url, body) ⇒ Object

Add entry to raw data collection, overwrite old data

Parameters:

  • url (String)
  • body (String)


48
49
50
51
52
# File 'lib/news_crawler/storage/raw_data/mongo_storage.rb', line 48

def add(url, body)
  @coll.update({:url   => url},
               {:$set  => {:body => body}},
               {:upsert => true})
end

#clearObject



71
72
73
# File 'lib/news_crawler/storage/raw_data/mongo_storage.rb', line 71

def clear
  @coll.remove
end

#countObject

Get number of raw data entries



67
68
69
# File 'lib/news_crawler/storage/raw_data/mongo_storage.rb', line 67

def count
  @coll.count
end

#find_by_url(url) ⇒ String?

Find document with correspond url

Parameters:

  • url (String)

Returns:

  • (String, nil)


57
58
59
60
61
62
63
64
# File 'lib/news_crawler/storage/raw_data/mongo_storage.rb', line 57

def find_by_url(url)
  result = @coll.find_one({:url => url})
  if (!result.nil?)
    result['body']
  else
    nil
  end
end