Class: MongoRequestLogger::Adapters::Mongo

Inherits:
Base
  • Object
show all
Defined in:
lib/mongo_request_logger/adapters/mongo.rb

Instance Attribute Summary

Attributes inherited from Base

#authenticated, #collection, #configuration, #connection, #connection_type

Instance Method Summary collapse

Methods inherited from Base

#authenticated?, #check_for_collection, #collection_name, #collection_stats_hash, #reset_collection

Constructor Details

#initialize(options = {}) ⇒ Mongo

Returns a new instance of Mongo.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mongo_request_logger/adapters/mongo.rb', line 9

def initialize(options = {})
  @authenticated = false
  @configuration = options.with_indifferent_access
  @configuration['host'] ||= '127.0.0.1'
  @configuration['port'] ||= '27017'
  @configuration['collection'] ||= 'server_log'

  connect

  check_for_collection
end

Instance Method Details

#clear!Object

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/mongo_request_logger/adapters/mongo.rb', line 71

def clear!
  raise NotImplementedError
end

#collection_statsObject



55
56
57
# File 'lib/mongo_request_logger/adapters/mongo.rb', line 55

def collection_stats
  collection_stats_hash(@collection.stats)
end

#connectObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongo_request_logger/adapters/mongo.rb', line 21

def connect
  if @configuration['url']
    uri = URI.parse(@configuration['url'])
    @configuration['database'] = uri.path.gsub(/^\//, '')
    @connection ||= mongo_connection_object.db(@configuration['database'])
    @authenticated = true
  else
    @connection ||= mongo_connection_object.db(@configuration['database'])
    if @configuration['username'] && @configuration['password']
      # the driver stores credentials in case reconnection is required
      @authenticated = @connection.authenticate(@configuration['username'],
          @configuration['password'])
    end
  end
end

#create_collectionObject



41
42
43
44
# File 'lib/mongo_request_logger/adapters/mongo.rb', line 41

def create_collection
  @connection.create_collection(collection_name,
      {:capped => true, :size => @configuration['capsize'].to_i*1024*1024})
end

#create_index(field) ⇒ Object



46
47
48
# File 'lib/mongo_request_logger/adapters/mongo.rb', line 46

def create_index field
  @collection.create_index(field)
end

#find_by_id(id) ⇒ Object



67
68
69
# File 'lib/mongo_request_logger/adapters/mongo.rb', line 67

def find_by_id(id)
  @collection.find_one(::BSON::ObjectId(id))
end

#insert_log_record(record) ⇒ Object



50
51
52
53
# File 'lib/mongo_request_logger/adapters/mongo.rb', line 50

def insert_log_record(record)
  # TODO: we should make this non-safe?
  @collection.insert(record)
end

#query(criteria, options = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/mongo_request_logger/adapters/mongo.rb', line 59

def query(criteria, options={})
  q = @collection.find(criteria).sort('timestamp', -1)
  if options[:limit]
    q = q.limit(options[:limit])
  end
  q
end

#reconnectObject



37
38
39
# File 'lib/mongo_request_logger/adapters/mongo.rb', line 37

def reconnect
  connect
end