Ruby script for connecting Piwik Database in rails
One of my client I have two different application/Framework 1.Piwik: Which is used as a Analytic framework for tracking all the activites of a web sites and you can store your analytic data in your server basic need for using piwik it's free and easy to integrate with any application and it is provide very good support if you are a paid customer but it's available free also .
So why we need to integrate that PIWIK database in Rails ! Actually we had a requirement to show Analytic dashboard of piwik for some of the Account holder so our Admin maually create the client app on Piwik and we populating that piwik token and other stuff via connecting the Piwik database .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mysql' | |
module Piwik | |
#--------------query for piwik database for getting token of user----------- | |
def piwik_tokens | |
!connection_piwik.nil? ? @rs = connection_piwik.query('select login, token_auth from enbake_piwik_user;') : "#{["Connect-Piwik-Sever"]}" | |
!@rs.nil? ? result : ["Connect-Piwik-Sever"] | |
end | |
#--------------------query for site_ids for clients dashboard---------------- | |
def piwik_site_ids | |
!connection_piwik.nil? ? @rs = connection_piwik.query("select name, idsite from enbake_piwik_site;") : "#{["Connect-Piwik-Sever"]}" | |
!@rs.nil? ? result : ["Connect-Piwik-Sever"] | |
end | |
#----------------creating connection for piwik database with mysql adapter------- | |
private | |
def connection_piwik | |
Mysql.new('localhost', 'root', 'a', 'enbake') | |
rescue Mysql::Error => e | |
# Print the error. | |
puts "ERROR #{e.errno} (#{e.sqlstate}): #{e.error}" | |
puts "Can't connect to the MySQL database specified." | |
# Signal an error | |
ensure | |
end | |
#---------------------------data for dropdown----------------------- | |
def result | |
@data = Array.new | |
@rs.each do |f| | |
f[0] = "#{f[0]}-#{f[1]}" | |
@data = @data << f | |
end | |
connection_piwik.close | |
@data | |
end | |
end |