Class: Pebblebed::GenericClient
Instance Method Summary
collapse
#delete, #get, #post, #put
Constructor Details
#initialize(session_key, root_url) ⇒ GenericClient
Returns a new instance of GenericClient.
6
7
8
9
10
|
# File 'lib/pebblebed/clients/generic_client.rb', line 6
def initialize(session_key, root_url)
@root_url = root_url
@root_url = URI(@root_url) unless @root_url.is_a?(URI::HTTP)
@session_key = session_key
end
|
Instance Method Details
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/pebblebed/clients/generic_client.rb', line 12
def perform(method, url = '', params = {}, &block)
begin
result = Pebblebed::Http.send(method, service_url(url), service_params(params), &block)
return DeepStruct.wrap(JSON.parse(result.body))
rescue JSON::ParserError => e
if e.message =~ /^lexical error: invalid bytes in UTF8 string/
raise
else
return result.body
end
end
end
|
#service_params(params) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/pebblebed/clients/generic_client.rb', line 67
def service_params(params)
if (key = @session_key) and (params.nil? or not params[:session])
if params
params = params.dup else
params = {}
end
params['session'] = key
end
params
end
|
#service_url(url, params = nil) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/pebblebed/clients/generic_client.rb', line 52
def service_url(url, params = nil)
result = @root_url.dup
result.path = result.path.sub(/\/+$/, "") + url
if params
result.query << '&' if result.query
result.query ||= ''
result.query << if params.is_a?(Hash)
params.entries.map { |k, v| CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s) }.join('&')
else
params
end
end
result
end
|
#stream(method, url = '', params = {}, on_data:, accept: nil) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/pebblebed/clients/generic_client.rb', line 26
def stream(method, url = '', params = {}, on_data:, accept: nil)
method_name = "stream_#{method.to_s.downcase}"
raise "Method not supported for streaming" unless Pebblebed::Http.respond_to?(method_name)
= {}
if accept
['Accept'] = accept
end
if accept == 'application/x-ndjson'
buffer = NdjsonBuffer.new(on_data)
response = Pebblebed::Http.send(method_name,
service_url(url), service_params(params),
headers: ,
on_data: ->(data) {
buffer << data
})
buffer.check_end!
return response
end
return Pebblebed::Http.send(method_name, service_url(url), service_params(params),
headers: ,
on_data: on_data)
end
|