Class: SalsaLabs::ApiClient
- Inherits:
-
Object
- Object
- SalsaLabs::ApiClient
- 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
-
#authentication_cookie ⇒ Object
readonly
Returns the value of attribute authentication_cookie.
Instance Method Summary collapse
- #authenticate ⇒ Object
- #authenticated? ⇒ Boolean
- #fetch(endpoint, params) ⇒ Object
-
#initialize(credentials = {}) ⇒ ApiClient
constructor
A new instance of ApiClient.
Constructor Details
#initialize(credentials = {}) ⇒ ApiClient
Returns a new instance of ApiClient.
10 11 12 13 14 15 16 |
# 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'] @host = credentials[:host] || 'hq-salsa.wiredforchange.com' @authenticated = false end |
Instance Attribute Details
#authentication_cookie ⇒ Object (readonly)
Returns the value of attribute authentication_cookie.
8 9 10 |
# File 'lib/salsa_labs/api_client.rb', line 8 def @authentication_cookie end |
Instance Method Details
#authenticate ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/salsa_labs/api_client.rb', line 18 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
27 28 29 |
# File 'lib/salsa_labs/api_client.rb', line 27 def authenticated? @authenticated end |
#fetch(endpoint, params) ⇒ Object
31 32 33 34 35 |
# File 'lib/salsa_labs/api_client.rb', line 31 def fetch(endpoint, params) authenticate unless authenticated? perform_get_request(endpoint, params).body end |