class SalsaLabs::ApiResponse

Public Class Methods

new(faraday_response) click to toggle source

@param [Faraday::Response] faraday_response

# File lib/salsa_labs/api_response.rb, line 5
def initialize(faraday_response)
  @response = faraday_response
end

Public Instance Methods

body() click to toggle source

@return [Nokogiri::XML] the full xml returned by the API,

parsed by +Nokogiri+ (memoized)
# File lib/salsa_labs/api_response.rb, line 24
def body
  @body ||= Nokogiri::XML(@response.body)
end
data() click to toggle source

@return [String] the xml fragment contained in the

+<data></data>+ element of the API response

@return [nil] if there was no data returned by the API

# File lib/salsa_labs/api_response.rb, line 43
def data
  data = body.xpath('//data')
  data.children.length == 0 ? nil : data.children.to_s
end
error() click to toggle source

@return [String] the error message returned by the API @return [nil] if there was no error message returned by the API

# File lib/salsa_labs/api_response.rb, line 50
def error
  err = body.xpath('//data/error').text
  err == '' ? nil : err
end
error_message() click to toggle source

@return [String] the error message returned by the API @return [nil] if there was no error message returned by the API

# File lib/salsa_labs/api_response.rb, line 30
def error_message
  if data.nil?
    'The response was not formatted as expected.'
  elsif error
    error
  else
    nil
  end
end
needs_reauthentication?() click to toggle source

@return [Boolean] true if an error message is returned that

indicates the current session has expired
# File lib/salsa_labs/api_response.rb, line 17
def needs_reauthentication?
  return false unless error
  error =~ /Please authenticate in order to continue./
end
successful?() click to toggle source

@return [Boolean] true if response was properly formed and

no error message was returned
# File lib/salsa_labs/api_response.rb, line 11
def successful?
  data && !error_message
end