class NewsCrawler::Storage::YAMLStor::MongoStorage

YAML storage implement using MongoDB

Constants

NAME

Public Class Methods

new(*opts) click to toggle source
# File lib/news_crawler/storage/yaml_stor/mongo_storage.rb, line 38
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.yaml]
  # @coll.ensure_index({:key => Mongo::ASCENDING}, {:unique => true})
end

Public Instance Methods

add(module_name, key, value) click to toggle source

Add entry to yaml collection, overwrite old data @param [ String ] module_name @param [ String ] key @param [ Object ] value YAML string

# File lib/news_crawler/storage/yaml_stor/mongo_storage.rb, line 50
def add(module_name, key, value)
  yaml_str = value.to_yaml
  yaml_str.encode!('utf-8', :invalid => :replace, :undef => :replace)
  @coll.update({:key   => key,
                 :m_name => module_name},
               {:$set  => {:value => yaml_str}},
               {:upsert => true})
end
clear() click to toggle source
# File lib/news_crawler/storage/yaml_stor/mongo_storage.rb, line 78
def clear
  @coll.remove
end
count() click to toggle source

Get number of raw data entries

# File lib/news_crawler/storage/yaml_stor/mongo_storage.rb, line 74
def count
  @coll.count
end
get(module_name, key) click to toggle source

Find document with correspond key @param [ String ] module_name @param [ String ] key @return [ Object, nil ]

# File lib/news_crawler/storage/yaml_stor/mongo_storage.rb, line 63
def get(module_name, key)
  result = @coll.find_one({:key => key,
                            :m_name => module_name})
  if (!result.nil?)
    YAML.load(result['value'])
  else
    nil
  end
end