class SalsaLabs::Connection

Public Class Methods

new(email, password) click to toggle source

@param [String] email

from your Salsa Labs login

@param [String] password

from your Salsa Labs login
# File lib/salsa_labs/connection.rb, line 17
def initialize(email, password)
  @email, @password = email, password
  @authentication_headers = nil
end

Public Instance Methods

request(path, query={}, recursive_call = false, &block) click to toggle source

@param [String] path

the url fragment that follows "+sandbox.salsalabs.com/+"
in the endpoint uri you are making a request to

@param [Hash] query

a hash representing the url query string parameters

@param [Boolean] recursive_call

whether this method was called by itself

@yield

the code to execute if the response is successful

@yieldparam [String] response

the xml fragment contained in the +<data></data>+ element of the API response

@raise [AuthenticationLoopError]

if the request is caught in an infinite authentication loop

@raise [APIResponseError]

if the API response returned an error or was malformed
# File lib/salsa_labs/connection.rb, line 37
def request(path, query={}, recursive_call = false, &block)
  authenticate unless authenticated?

  execute_request(path, query) do |response|
    if response.successful?
      block.call(response.data)
    elsif response.needs_reauthentication?
      raise AuthenticationLoopError.new if recursive_call
      authenticate
      request(path, query, true, &block)
    else
      raise APIResponseError, response.error_message
    end
  end
end

Private Instance Methods

api() click to toggle source

@return [Faraday]

# File lib/salsa_labs/connection.rb, line 56
def api
  @api ||= ::Faraday.new url: 'https://sandbox.salsalabs.com/' do |conn|
    conn.use :salsa
    conn.adapter :net_http
  end
end
authenticate() click to toggle source

Makes an authentication call to the Salsa Labs API using the credentials set in the {SalsaLabs.configure} method.

@raise [AuthenticationError] if the response returned an error or is malformed

# File lib/salsa_labs/connection.rb, line 72
def authenticate
  email, password = CGI.escape(@email), CGI.escape(@password)
  auth_path = "api/authenticate.sjs?email=#{email}&password=#{password}"
  response = AuthenticationResponse.new(api.get(auth_path))
  if response.successful?
    @authentication_headers = { 'Cookie' => response.session_cookies }
  else
    @authentication_headers = nil
    raise AuthenticationError, response.error_message
  end
end
authenticated?() click to toggle source

Returns true if the session cookie has been set.

@return [Boolean]

# File lib/salsa_labs/connection.rb, line 87
def authenticated?
  !!@authentication_headers
end
execute_request(path, query={}) { |api_response(get)| ... } click to toggle source

@yieldparam [ApiResponse] response

# File lib/salsa_labs/connection.rb, line 64
def execute_request(path, query={})
  yield ApiResponse.new(api.get(path, query, @authentication_headers))
end