Class: Piwik::ApiModule

Inherits:
Base
  • Object
show all
Includes:
DataMethods
Defined in:
lib/piwik/api_module.rb

Instance Attribute Summary

Attributes inherited from Base

#attributes

Class Method Summary collapse

Methods included from DataMethods

included

Methods inherited from Base

call, #call, collection, #collection, #config, #created_at, #delete, #id, #id_attr, #initialize, load, load_config_from_file, #method_missing, #new?, parse_xml, #parse_xml, #save

Methods included from ApiScope

included

Methods included from Typecast

included

Constructor Details

This class inherits a constructor from Piwik::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Piwik::Base

Class Method Details

.api_call_to_const(string, full = false) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/piwik/api_module.rb', line 44

def self.api_call_to_const string, full = false  # We can get rid of the get prefix

  string = case string
  when /[A-Z]{1}[a-z]*\.[get|add|delete|save]$/,'get','add','delete','save'
    string.camelize
  else
    string.gsub(/get|save|add|delete/, '')
  end
  string = string.split('.').map {|s| s.camelize }.join('::')
  full ? "Piwik::#{string}" : string
end

.available_methods(method_array) ⇒ Object


34
35
36
37
38
39
40
41
42
# File 'lib/piwik/api_module.rb', line 34

def self.available_methods method_array
  @available_methods = method_array
  @available_methods.each do |method|
    class_eval %{
      class #{self.api_call_to_const(method)} < Piwik::ApiResponse
      end
    }, __FILE__, __LINE__
  end
end

.defaultsObject

returns default API params, used all over the place, especially in scoped_methods


6
7
8
# File 'lib/piwik/api_module.rb', line 6

def self.defaults
  {:period => :day, :date => Date.today, @obj.id_attr => @obj.id}
end

.method_missing(method, *args, &block) ⇒ Object

Catch incoming method calls and try to format them and send them over to the api


11
12
13
14
15
16
17
18
19
20
# File 'lib/piwik/api_module.rb', line 11

def self.method_missing(method, *args, &block)
  formatted_method = method.to_s.camelize(:lower)
  formatted_method = formatted_method.gsub(/ip$/i,'IP').gsub(/os/i,'OS') # Lame
  # connect to API if this is a valid-looking method in the current class context
  if @available_methods.include?(formatted_method)
    handle_api_call(formatted_method, args.first)
  else
    super
  end
end

.scoped_methods(&block) ⇒ Object

allows the addition of scoped methods. It's basically a class << self wrapper mostly added to make ApiModule code more self-explanatory the @obj instance variable is set in the api_scope call. This is not very clean or anything, and I am still researching a better way to do it, but the Piwik::Site API is certainly much better to work with due to this


27
28
29
30
31
32
# File 'lib/piwik/api_module.rb', line 27

def self.scoped_methods &block
  if block_given?
    extension =  Module.new(&Proc.new)
    self.extend(extension)
  end
end