Class: Pocus::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/pocus/session.rb

Overview

Sends authenticated JSON requests to remote service using HTTPS.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: 'api.invoc.us', app_id:, username:, password:, logger: Logger.new('/dev/null')) ⇒ Session

Returns a new instance of Session.



23
24
25
26
27
28
29
30
31
# File 'lib/pocus/session.rb', line 23

def initialize(host: 'api.invoc.us', app_id:, username:, password:, logger: Logger.new('/dev/null'))
  @base_url = "https://#{host}/icp".freeze
  @credentials = {
    'API-AppId'    => app_id,
    'API-Username' => username,
    'API-Password' => password
  }
  @logger = logger
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



19
20
21
# File 'lib/pocus/session.rb', line 19

def base_url
  @base_url
end

#credentialsObject (readonly)

Returns the value of attribute credentials.



20
21
22
# File 'lib/pocus/session.rb', line 20

def credentials
  @credentials
end

#loggerObject

Returns the value of attribute logger.



21
22
23
# File 'lib/pocus/session.rb', line 21

def logger
  @logger
end

Instance Method Details

#pro?Boolean

‘Pro’ is legacy as moniker dropped.

Returns:

  • (Boolean)


34
35
36
# File 'lib/pocus/session.rb', line 34

def pro?
  base_url =~ /invoc.us/
end

#send_request(method, path, fields = {}) ⇒ Object

Accepts hash of fields to send. Returns parsed response body, always a hash.



40
41
42
43
44
# File 'lib/pocus/session.rb', line 40

def send_request(method, path, fields = {})
  response = send_logged_request(URI(base_url + path), method, request_data(fields))
  fail UnexpectedHttpResponse, response unless response.is_a? Net::HTTPSuccess
  JSON.parse(response.body)
end