Class: PusherPlatform::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pusher-platform/base_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ BaseClient

Returns a new instance of BaseClient.



6
7
8
9
10
11
12
13
14
15
# File 'lib/pusher-platform/base_client.rb', line 6

def initialize(options)
  raise "Unspecified host" if options[:host].nil?
  port_string = options[:port] || ''
  host_string = "https://#{options[:host]}#{port_string}"
  @connection = Excon.new(host_string)

  @instance_id = options[:instance_id]
  @service_name = options[:service_name]
  @service_version = options[:service_version]
end

Instance Method Details

#request(options) ⇒ Object



17
18
19
20
21
22
23
24
25
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
51
52
53
54
55
56
57
58
# File 'lib/pusher-platform/base_client.rb', line 17

def request(options)
  raise "Unspecified request method" if options[:method].nil?
  raise "Unspecified request path" if options[:path].nil?

  headers = if options[:headers]
    options[:headers].dup
  else
    {}
  end

  if options[:jwt]
    headers["Authorization"] = "Bearer #{options[:jwt]}"
  end

  path = "services/#{@service_name}/#{@service_version}/#{@instance_id}/#{options[:path]}"
  body = unless options[:body].nil?
    options[:body].to_json
  end

  response = @connection.request(
    method: options[:method],
    path: sanitise_path(path),
    headers: headers,
    body: body,
    query: options[:query]
  )

  if response.status >= 200 && response.status <= 299
    return response
  elsif response.status >= 300 && response.status <= 399
    raise "unsupported redirect response: #{response.status}"
  elsif response.status >= 400 && response.status <= 599
    error_description = begin
      JSON.parse(response.body)
    rescue
      response.body
    end
    raise ErrorResponse.new(response.status, response.headers, error_description)
  else
    raise "unsupported response code: #{response.status}"
  end
end