Module: KayakoClient::HTTP

Included in:
Base
Defined in:
lib/kayako_client/http/http.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



18
19
20
# File 'lib/kayako_client/http/http.rb', line 18

def self.included(base)
    base.extend(ClassMethods)
end

Instance Method Details

#clientObject



65
66
67
68
69
70
71
72
73
74
75
# 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 = {}) ⇒ Object

Raises:

  • (RuntimeError)


108
109
110
111
112
113
114
115
116
# 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_backendObject



61
62
63
# File 'lib/kayako_client/http/http.rb', line 61

def http_backend
    @http_backend ||= self.class.http_backend
end

#http_backend=(backend) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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 = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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

#proxyObject



33
34
35
# File 'lib/kayako_client/http/http.rb', line 33

def proxy
    @proxy ||= self.class.proxy
end

#proxy=(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# 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 = {}) ⇒ Object

Raises:

  • (RuntimeError)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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