module KayakoClient::HTTP

Public Class Methods

included(base) click to toggle source
# File lib/kayako_client/http/http.rb, line 18
def self.included(base)
    base.extend(ClassMethods)
end

Public Instance Methods

client() click to toggle source
# File lib/kayako_client/http/http.rb, line 65
def client
    @client ||= nil
    unless @client
        if !instance_of?(KayakoClient::Base) && http_backend == self.class.http_backend && self.class.client
            @client = self.class.client
        else
            @client = http_backend.new(proxy.merge(:logger => logger))
        end
    end
    @client
end
delete_request(params = {}) click to toggle source
# File lib/kayako_client/http/http.rb, line 108
def delete_request(params = {})
    raise RuntimeError, "undefined ID" unless id
    random = self.class.salt
    e = params.delete(:e) || "#{self.class.path}/#{id}"
    client.delete(api_url, :e         => e,
                           :apikey    => api_key,
                           :salt      => random,
                           :signature => self.class.signature(random, secret_key))
end
http_backend() click to toggle source
# File lib/kayako_client/http/http.rb, line 61
def http_backend
    @http_backend ||= self.class.http_backend
end
http_backend=(backend) click to toggle source
# File lib/kayako_client/http/http.rb, line 37
def http_backend=(backend)
    if backend.is_a?(String)
        raise ArgumentError, "invalid HTTP backend: #{backend}" unless backend =~ /^[A-Za-z_]+$/
        file = backend.gsub(/([a-z])([A-Z])/, '\1_\2').gsub(/([A-Z])([A-Z][a-z])/, '\1_\2').downcase
        require "kayako_client/http/#{file}"
        backend = KayakoClient.const_get(backend)
    end
    if backend.is_a?(Class)
        if backend.included_modules.include?(KayakoClient::HTTPBackend)
            @http_backend = backend
            @client = nil
        else
            raise ArgumentError, "invalid HTTP backend: #{backend.name}"
        end
    else
        if backend.class.included_modules.include?(KayakoClient::HTTPBackend)
            @http_backend = backend.class
            @client = backend
        else
            raise ArgumentError, "invalid HTTP backend: #{backend.class.name}"
        end
    end
end
post_request(params = {}) click to toggle source
# File lib/kayako_client/http/http.rb, line 93
def post_request(params = {})
    @errors ||= {}
    random = self.class.salt
    e = params.delete(:e) || self.class.path
    data = self.class.validate(params.merge(:errors => @errors))
    if @errors.empty?
        client.post(api_url, data.merge(:e         => e,
                                        :apikey    => api_key,
                                        :salt      => random,
                                        :signature => self.class.signature(random, secret_key)))
    else
        @errors
    end
end
proxy() click to toggle source
# File lib/kayako_client/http/http.rb, line 33
def proxy
    @proxy ||= self.class.proxy
end
proxy=(options = {}) click to toggle source
# File lib/kayako_client/http/http.rb, line 22
def proxy=(options = {})
    @proxy = {}
    options.each do |key, value|
        if [ :host, :port, :user, :pass ].include?(key)
            @proxy[key] = value
        else
            raise ArgumentError, "unsupported option: #{key}"
        end
    end
end
put_request(params = {}) click to toggle source
# File lib/kayako_client/http/http.rb, line 77
def put_request(params = {})
    @errors ||= {}
    raise RuntimeError, "undefined ID" unless id
    random = self.class.salt
    e = params.delete(:e) || "#{self.class.path}/#{id}"
    data = self.class.validate(params.merge(:errors => @errors))
    if @errors.empty?
        client.put(api_url, data.merge(:e         => e,
                                       :apikey    => api_key,
                                       :salt      => random,
                                       :signature => self.class.signature(random, secret_key)))
    else
        @errors
    end
end