Class: SalsaLabs::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/salsa_labs/api_client.rb

Overview

Used to request information from Salsa Labs. Handles cookie-based authentication, and raises an exception when the API returns an error.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials = {}) ⇒ ApiClient

Returns a new instance of ApiClient.



10
11
12
13
14
15
# File 'lib/salsa_labs/api_client.rb', line 10

def initialize(credentials = {})
  @email = credentials[:email] || ENV['SALSA_LABS_API_EMAIL']
  @password = credentials[:password] || ENV['SALSA_LABS_API_PASSWORD']

  @authenticated = false
end

Instance Attribute Details

Returns the value of attribute authentication_cookie.



8
9
10
# File 'lib/salsa_labs/api_client.rb', line 8

def authentication_cookie
  @authentication_cookie
end

Instance Method Details

#authenticateObject



17
18
19
20
21
22
23
24
# File 'lib/salsa_labs/api_client.rb', line 17

def authenticate
  return true if authenticated?

  response = authenticate!

  @authentication_cookie = response.env[:response_headers]["set-cookie"]
  @authenticated = Nokogiri::XML(response.body).css('error').empty?
end

#authenticated?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/salsa_labs/api_client.rb', line 26

def authenticated?
  @authenticated
end

#fetch(endpoint, params) ⇒ Object



30
31
32
33
34
# File 'lib/salsa_labs/api_client.rb', line 30

def fetch(endpoint, params)
  authenticate unless authenticated?

  perform_get_request(endpoint, params).body
end