class Solarium::Database

Public: Implements logic for interacting with the Solarium database.

Attributes

error[R]

 Public: Either an exception that was thrown during database connection or nil.

Public Class Methods

new(url) click to toggle source

Public: Initializes a new instance of the Solarium::Database class.

url - The URL for connecting to the SQL database.

# File lib/solarium/database.rb, line 14
def initialize url
        connect url
rescue ::Exception => error
        @error = error
end

Public Instance Methods

insert(collector) click to toggle source

Public: Stores the information provided by a Solarium::Collector in the database.

collector - An instance of the Solarium::Collector class containing information to store.

# File lib/solarium/database.rb, line 23
def insert collector
        row = {
                time: DateTime.now,
                now: collector.now,
                today: collector.today,
                week: collector.week,
                lifetime: collector.lifetime
        }
        @connection[:solarium].insert row
end
select_days(days) click to toggle source

Public: Reads the specified number of days worth of data from the database.

days - The number of days worth of data to read.

# File lib/solarium/database.rb, line 37
def select_days days
        data = @connection[:solarium].where do
                time > DateTime.now - days
        end
        return data.to_a
end

Private Instance Methods

connect(url) click to toggle source

Internal: Connects to the database and creates the table if required.

url - The URL for connecting to the SQL database.

# File lib/solarium/database.rb, line 47
        def connect url
        @connection = Sequel.connect url
        @connection.create_table? :solarium do
                column :time, DateTime, primary_key: true
                column :now, Integer, null: false
                column :today, Integer, null: false
                column :week, Integer, null: false
                column :lifetime, Integer, null: false
        end
end