module KayakoClient::HTTP::ClassMethods

Public Instance Methods

client() click to toggle source
# File lib/kayako_client/http/http.rb, line 169
def client
    @@client ||= nil
    unless @@client
        @@client = http_backend.new(proxy.merge(:logger => logger))
    end
    @@client
end
delete_request(options = {}) click to toggle source
# File lib/kayako_client/http/http.rb, line 210
def delete_request(options = {})
    random = salt
    params = options.dup

    id     = params.delete(:id)
    e      = params.delete(:e) || "#{path}/#{id}"
    http   = params.delete(:client) || client
    url    = params.delete(:api_url) || api_url
    key    = params.delete(:api_key) || api_key
    secret = params.delete(:secret_key) || secret_key

    raise ArgumentError, "missing ID" unless id
    http.delete(url, :e         => e,
                     :apikey    => key,
                     :salt      => random,
                     :signature => signature(random, secret))
end
get_request(options = {}) click to toggle source
# File lib/kayako_client/http/http.rb, line 177
def get_request(options = {})
    random = salt
    params = options.dup

    id     = params.delete(:id)
    e      = params.delete(:e) || (id.nil? ? path : "#{path}/#{id}")
    http   = params.delete(:client) || client
    url    = params.delete(:api_url) || api_url
    key    = params.delete(:api_key) || api_key
    secret = params.delete(:secret_key) || secret_key

    http.get(url, params.merge(:e         => e,
                               :apikey    => key,
                               :salt      => random,
                               :signature => signature(random, secret)))
end
http_backend() click to toggle source
# File lib/kayako_client/http/http.rb, line 159
def http_backend
    begin
        require 'kayako_client/http/http_client'
        @@http_backend ||= KayakoClient::HTTPClient
    rescue LoadError
        require 'kayako_client/http/net_http'
        @@http_backend ||= KayakoClient::NetHTTP
    end
end
http_backend=(backend) click to toggle source
# File lib/kayako_client/http/http.rb, line 135
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(options = {}) click to toggle source
# File lib/kayako_client/http/http.rb, line 194
def post_request(options = {})
    random = salt
    params = options.dup

    e      = params.delete(:e) || path
    http   = params.delete(:client) || client
    url    = params.delete(:api_url) || api_url
    key    = params.delete(:api_key) || api_key
    secret = params.delete(:secret_key) || secret_key

    http.post(url, params.merge(:e         => e,
                                :apikey    => key,
                                :salt      => random,
                                :signature => signature(random, secret)))
end
proxy() click to toggle source
# File lib/kayako_client/http/http.rb, line 131
def proxy
    @@proxy ||= {}
end
proxy=(options = {}) click to toggle source
# File lib/kayako_client/http/http.rb, line 120
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