module AlmaRestApi

Public Class Methods

check_config() click to toggle source
# File lib/alma_rest_api.rb, line 90
def check_config
  raise NoApiKeyError if configuration.api_key.nil? || configuration.api_key.empty?
  raise InvalidApiFormatError unless [:json, :"application/xml"].include? configuration.format
end
configuration() click to toggle source
# File lib/alma_rest_api.rb, line 9
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/alma_rest_api.rb, line 13
def configure
  yield(configuration)
end
delete(uri) click to toggle source
# File lib/alma_rest_api.rb, line 72
def delete(uri)
  check_config
  begin
    RestClient.delete uri(uri),
      authorization: 'apikey ' + configuration.api_key
  rescue => e
   raise AlmaApiError, parse_error(e.response)
  end   
end
get(uri) click to toggle source
# File lib/alma_rest_api.rb, line 17
def get(uri)
  check_config
  begin
    response = 
     RestClient.get uri(uri),
        accept: configuration.format,
        authorization: 'apikey ' + configuration.api_key
    if configuration.format == :"application/xml"
      return Nokogiri::XML.parse(response.body)
    else
      return JSON.parse(response.body)
    end
  rescue => e
    raise AlmaApiError, parse_error(e.response)
  end 
end
parse_error(err) click to toggle source
# File lib/alma_rest_api.rb, line 95
def parse_error(err)
  begin
    if err[0] == '<'
      msg = err.match(/<errorMessage>(.*)<\/errorMessage>/)
      return msg ? msg[1] : ''
    elsif err[0] == '{'
      begin
        error = JSON.parse(err)
        if error["web_service_result"] #500
          return error["web_service_result"]["errorList"]["error"]["errorMessage"]
        else #400
          return error["errorList"]["error"][0]["errorMessage"]
        end
      rescue JSON::ParserError
        return "Unknown error from Alma"
      end            
    else
      return err          
    end
  rescue
    return err
  end
end
post(uri, data) click to toggle source
# File lib/alma_rest_api.rb, line 53
def post(uri, data)
  check_config
  begin
    response =
     RestClient.post uri(uri),
      configuration.format == :"application/xml" ? data.to_xml : data.to_json,
      accept: configuration.format,
      authorization: 'apikey ' + configuration.api_key,
      content_type: configuration.format
    if configuration.format == :"application/xml"
      return Nokogiri::XML.parse(response.body)
    else
      return JSON.parse(response.body)
    end
  rescue => e
    raise AlmaApiError, parse_error(e.response)
  end         
end
put(uri, data) click to toggle source
# File lib/alma_rest_api.rb, line 34
def put(uri, data)
  check_config
  begin
    response =
     RestClient.put uri(uri),
      configuration.format == :"application/xml" ? data.to_xml : data.to_json,
      accept: configuration.format,
      authorization: 'apikey ' + configuration.api_key,
      content_type: configuration.format
    if configuration.format == :"application/xml"
      return Nokogiri::XML.parse(response.body)
    else
      return JSON.parse(response.body)
    end
  rescue => e
    raise AlmaApiError, parse_error(e.response)
  end 
end
uri(uri) click to toggle source
# File lib/alma_rest_api.rb, line 82
def uri(uri)
  if uri.start_with? 'http'
    return uri
  else
    return AlmaRestApi.configuration.api_path + uri
  end
end