Class: PusherPlatform::BaseClient
- Inherits:
-
Object
- Object
- PusherPlatform::BaseClient
- Defined in:
- lib/pusher-platform/base_client.rb
Instance Method Summary collapse
-
#initialize(options) ⇒ BaseClient
constructor
A new instance of BaseClient.
- #request(options) ⇒ Object
Constructor Details
#initialize(options) ⇒ BaseClient
Returns a new instance of BaseClient.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/pusher-platform/base_client.rb', line 8 def initialize() raise PusherPlatform::Error.new("Unspecified host") if [:host].nil? port_string = [:port] || '' host_string = "https://#{[:host]}#{port_string}" @connection = Excon.new(host_string) @instance_id = [:instance_id] @service_name = [:service_name] @service_version = [:service_version] end |
Instance Method Details
#request(options) ⇒ Object
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 59 60 61 62 63 64 65 66 67 |
# File 'lib/pusher-platform/base_client.rb', line 19 def request() raise PusherPlatform::Error.new("Unspecified request method") if [:method].nil? raise PusherPlatform::Error.new("Unspecified request path") if [:path].nil? headers = if [:headers] [:headers].dup else {} end if [:jwt] headers["Authorization"] = "Bearer #{[:jwt]}" end path = "services/#{@service_name}/#{@service_version}/#{@instance_id}/#{[:path]}" body = unless [:body].nil? [:body].to_json end response = @connection.request( method: [:method], path: sanitise_path(path), headers: headers, body: body, query: [:query] ) if response.status >= 200 && response.status <= 299 return response elsif response.status >= 300 && response.status <= 399 raise PusherPlatform::Error.new("Unsupported redirect response: #{response.status}") elsif response.status >= 400 && response.status <= 599 error = begin JSON.parse(response.body) rescue response.body end error_res_opts = { status: response.status, headers: response.headers, error: error["error"], error_description: error["error_description"], error_uri: error["error_uri"] } raise ErrorResponse.new(error_res_opts) else raise PusherPlatform::Error.new("Unsupported response code: #{response.status}") end end |