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
# File 'lib/helper/DAO.rb', line 24

def initialize (type = 'twitter')

	@type = type
	
	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



58
59
60
61
62
63
# File 'lib/helper/DAO.rb', line 58

def clean
	
	@db.del 'twitter'
	@db.del 'rpg'

end

#connect_databaseObject

Connects to a redis database



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/helper/DAO.rb', line 33

def connect_database

	@db = Redis.new

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

#get_statusString

Retrieves the next status that were saved in the database

Returns:

  • (String)

    the status from the Adapter



49
50
51
52
53
54
# File 'lib/helper/DAO.rb', line 49

def get_status 
	
	status = @db.rpop @type
	status

end

#publish(usmf) ⇒ Object

Publish a message in the websocket server

Parameters:

  • usmf (USMF)

    a message to publish



84
85
86
# File 'lib/helper/DAO.rb', line 84

def publish usmf
	@db.publish 'ws', usmf
end

#save_status(status) ⇒ Object

Persists the status into the database

Parameters:

  • status (String)

    the status to persist it



68
69
70
71
72
# File 'lib/helper/DAO.rb', line 68

def save_status status

	@db.rpush @type, status

end

#sizeInteger

Returns the size of the database

Returns:

  • (Integer)

    the size (items) of the database



77
78
79
# File 'lib/helper/DAO.rb', line 77

def size
	@db.llen @type
end