class Magenthor::Base

Public Class Methods

setup(params) click to toggle source

Initialize the constants that will be used to make the connection to Magento

@param params [Hash] contains the paramters needed for connection @return [Magenthor::Base]

# File lib/magenthor/base.rb, line 15
def self.setup params
    if params.class != Hash
        puts "Parameters must be in an Hash."
        return false
    end

    if !params.key? :host or !params.key? :api_user or !params.key? :api_key
        puts "Mandatory parameter missing. Check if :host, :api_user and :api_key are there."
        return false
    end
    
    @@api_user = params[:api_user]
    @@api_key = params[:api_key]
    url = "http://#{params[:host]}:#{params[:port]}/api/xmlrpc"
    
    @@client = XMLRPC::Client.new2(url)
    @@client.http_header_extra = { "accept-encoding" => "identity" }

    return true
end

Private Class Methods

commit(resource_path, params) click to toggle source

Call the Magento Api resource passing parameters

@param resource_path [String] the Magento Api resource path to call @param params [Hash, Array] the paramters needed for the call @return [Array, FalseClass] the result set if the call is successful or false

# File lib/magenthor/base.rb, line 64
def self.commit resource_path, params
    if params.class == Hash
        params = [params]   # Magento wants an Array, always!
    end
    
    if login
        begin
            @@client.call('call', @@session_id, resource_path, params)
        rescue => e
            if e.class == XMLRPC::FaultException
                puts "Magento says: #{e.message}"
            end
            return false
        ensure
            logout
        end
    else
        return false
    end
end
login() click to toggle source

Login to Magento using the parameters setted on initialize

@return [TrueClass, FalseClass] true if login successful or false

# File lib/magenthor/base.rb, line 41
def self.login
    begin
        @@session_id = @@client.call('login', @@api_user, @@api_key)
        return true
    rescue => e
        if e.class == NoMethodError
            puts 'You must first set the connection parameters using Magenthor::Base.setup'
            return false
        end
    end
end
logout() click to toggle source

End the current Magento api session

# File lib/magenthor/base.rb, line 54
def self.logout
    response = @@client.call('endSession', @@session_id)
    @@session_id = nil
end