Class: DAO

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/helper/DAO.rb

Overview

Data Access Object to manage the persistence layer of the app

Author:

  • Daniel Machado Fernandez

Version:

  • 1.0

Constant Summary

Constants included from Logging

Logging::KermitPFC

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initialize(type = 'twitter') ⇒ DAO

Config the DAO defining the Data Stream

Parameters:

  • type (String) (defaults to: 'twitter')

    type of the Data Stream to choose the correct database



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/helper/DAO.rb', line 24

def initialize (type = 'twitter')

	@type = type

	logger.debug('Starting DAO...')
	
	logger.info(type + " DAO in " + type.to_s + " type")

	logger.info("config loaded OK")
	connect_database

end

Instance Attribute Details

#dbRedis

Returns An instance from a Redis DB with the redis gem.

Returns:

  • (Redis)

    An instance from a Redis DB with the redis gem

See Also:



19
20
21
# File 'lib/helper/DAO.rb', line 19

def db
  @db
end

#typeString

Returns Type of the stream to identify the database.

Returns:

  • (String)

    Type of the stream to identify the database



14
15
16
# File 'lib/helper/DAO.rb', line 14

def type
  @type
end

Instance Method Details

#cleanObject

Note:

if the user develops more Streams, add the new keys like the existing ones

Deletes the keys in the database to starts the FW empty



64
65
66
67
68
# File 'lib/helper/DAO.rb', line 64

def clean
	logger.debug("cleaning the db")
	@db.del 'twitter'
	@db.del 'rpg'
end

#connect_databaseObject

Connects to a redis database



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/helper/DAO.rb', line 38

def connect_database
	@db = Redis.new

	logger.debug("New instance for Redis")

	@db = Redis.connect(
		:db   => "#{Settings.redis.db}",
		:host => "#{Settings.redis.host}",
		:port => Settings.redis.port,
		:password => Settings.redis.password
	)
	
	logger.info("Redis connect OK")
end

#get_statusString

Retrieves the next status that were saved in the database

Returns:

  • (String)

    the status from the Adapter



56
57
58
59
60
# File 'lib/helper/DAO.rb', line 56

def get_status 
	logger.debug("getting status")
	status = @db.rpop @type
	status
end

#publish(usmf) ⇒ Object

Publish a message in the websocket server

Parameters:

  • usmf (USMF)

    a message to publish



89
90
91
92
# File 'lib/helper/DAO.rb', line 89

def publish usmf
	logger.info("Publishing")
	@db.publish 'ws', usmf
end

#save_status(status) ⇒ Object

Persists the status into the database

Parameters:

  • status (String)

    the status to persist it



73
74
75
76
# File 'lib/helper/DAO.rb', line 73

def save_status status
	@db.rpush @type, status
	logger.info("Status saved")
end

#sizeInteger

Returns the size of the database

Returns:

  • (Integer)

    the size (items) of the database



81
82
83
84
# File 'lib/helper/DAO.rb', line 81

def size
	logger.debug("getting size")
	@db.llen @type
end