class Piwik::Base

Attributes

attributes[RW]

common constructor, using ostruct for attribute storage

Public Class Methods

call(method, params, piwik_url=nil, auth_token=nil) click to toggle source

Calls the supplied Piwik API method, with the supplied parameters.

Returns a string containing the XML reply from Piwik, or raises a Piwik::ApiError exception with the error message returned by Piwik in case it receives an error.

# File lib/piwik/base.rb, line 142
def call(method, params, piwik_url=nil, auth_token=nil)
  params ||= {}
  raise MissingConfiguration, "Please edit ~/.piwik to include your piwik url and auth_key" if piwik_url.nil? || auth_token.nil?
  url = "#{piwik_url}/index.php?"
  params.merge!({:module => 'API', :format => 'xml', :method => method})
  params.merge!({:token_auth => auth_token}) unless auth_token.nil?
  url << params.to_query
  verbose_obj_save = $VERBOSE
  $VERBOSE = nil # Suppress "warning: peer certificate won't be verified in this SSL session"
  xml = RestClient.get(url)
  $VERBOSE = verbose_obj_save
  if xml.is_a?(String) && xml.force_encoding('BINARY').is_binary_data?
    xml.force_encoding('BINARY')
  elsif xml =~ /error message=/
    result = XmlSimple.xml_in(xml, {'ForceArray' => false})
    raise ApiError, result['error']['message'] if result['error']
  else
    xml
  end
end
collection() click to toggle source
# File lib/piwik/base.rb, line 121
def collection
  "#{self.to_s.pluralize}".safe_constantize
end
load(id) click to toggle source
# File lib/piwik/base.rb, line 132
def load id
  collection.get(id_attr => id)
end
Also aliased as: reload
load_config_from_file() click to toggle source

Checks for the config, creates it if not found

# File lib/piwik/base.rb, line 164
def load_config_from_file
  # Useful for testing or embedding credentials - although as always
  # it is not recommended to embed any kind of credentials in source code for security reasons
  return { :piwik_url => PIWIK_URL, :auth_token => PIWIK_TOKEN } if PIWIK_URL.present? and PIWIK_TOKEN.present?
  config = {}
  if defined?(RAILS_ROOT) and RAILS_ROOT != nil
    home =  RAILS_ROOT
    filename = "config/piwik.yml"
  else
    home =  ENV['HOME'] || ENV['USERPROFILE'] || ENV['HOMEPATH'] || "."
    filename = ".piwik"
  end
  temp_config = if File.exists?(File.join(home,filename))
    YAML::load(open(File.join(home,filename)))
  else
    open(File.join(home,filename),'w') { |f| f.puts @@template }
    YAML::load(@@template)
  end
  temp_config.each { |k,v| config[k.to_sym] = v } if temp_config
  if config[:piwik_url] == nil || config[:auth_token] == nil
    if defined?(RAILS_ROOT) and RAILS_ROOT != nil
      raise MissingConfiguration, "Please edit ./config/piwik.yml to include your piwik url and auth_key"
    else
      raise MissingConfiguration, "Please edit ~/.piwik to include your piwik url and auth_key"
    end

  end
  config
end
new(params = {}) click to toggle source
# File lib/piwik/base.rb, line 32
def initialize params = {}
  @attributes = OpenStruct.new
  params.map do |k,v|
    @attributes.send(:"#{k}=",typecast(v))
  end
end
parse_xml(xml) click to toggle source

This is required to normalize the API responses when the Rails XmlSimple version is used

# File lib/piwik/base.rb, line 126
def parse_xml xml
  result = XmlSimple.xml_in(xml, {'ForceArray' => false})
  result = result['result'] if result['result']
  result
end
reload(id)
Alias for: load

Public Instance Methods

call(method, params={}) click to toggle source

Calls the supplied Piwik API method, with the supplied parameters.

Returns a string containing the XML reply from Piwik, or raises a Piwik::ApiError exception with the error message returned by Piwik in case it receives an error.

# File lib/piwik/base.rb, line 108
def call(method, params={})
  self.class.call(method, params, config[:piwik_url], config[:auth_token])
end
collection() click to toggle source
# File lib/piwik/base.rb, line 116
def collection
  self.class.collection
end
config() click to toggle source
# File lib/piwik/base.rb, line 112
def config
  @config ||= self.class.load_config_from_file
end
created_at() click to toggle source

created_at will try and return the value of the Piwik item id if it exists

# File lib/piwik/base.rb, line 88
def created_at
  attributes.send(:ts_created) rescue nil
end
delete() click to toggle source
# File lib/piwik/base.rb, line 55
def delete
  collection.delete(attributes)
end
Also aliased as: destroy
destroy()
Alias for: delete
id() click to toggle source

id will try and return the value of the Piwik item id if it exists

# File lib/piwik/base.rb, line 75
def id
  begin
    if self.class == Piwik::Site
      self.idsite
    else
      attributes.send(:"id#{self.class.to_s.gsub('Piwik::','')}")
    end
  rescue Exception => e
    $stderr.puts e
  end
end
id_attr() click to toggle source
# File lib/piwik/base.rb, line 39
def id_attr
  self.class.id_attr
end
method_missing(method,*args,&block) click to toggle source

delegate attribute calls to @attributes storage

Calls superclass method
# File lib/piwik/base.rb, line 93
def method_missing(method,*args,&block)
  if self.attributes.respond_to?(method)
    self.attributes.send(method,*args,&block)
  else
    super
  end
end
new?() click to toggle source

Returns true if the current site does not exists in the Piwik yet.

# File lib/piwik/base.rb, line 61
def new?
  begin
    if respond_to?(:id)
      id.nil? && created_at.blank?
    else
      created_at.blank?
    end
    
  rescue Exception => e
    nil
  end
end
parse_xml(xml;) click to toggle source
# File lib/piwik/base.rb, line 101
def parse_xml xml; self.class.parse_xml xml; end
save() click to toggle source
# File lib/piwik/base.rb, line 43
def save
  if new?
    resp = collection.add(attributes)
    attributes = resp.attributes
    true
  else
    collection.save(attributes)
  end
  
end
Also aliased as: update
update()
Alias for: save