Module: KayakoClient::HTTP::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#clientObject



169
170
171
172
173
174
175
# 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 = {}) ⇒ Object

Raises:

  • (ArgumentError)


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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 = {}) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# 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_backendObject



159
160
161
162
163
164
165
166
167
# 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) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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 = {}) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# 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

#proxyObject



131
132
133
# File 'lib/kayako_client/http/http.rb', line 131

def proxy
    @@proxy ||= {}
end

#proxy=(options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# 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