Module: AlmaRestApi

Defined in:
lib/alma_rest_api.rb

Class Method Summary collapse

Class Method Details

.check_configObject

Raises:



90
91
92
93
# 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

.configurationObject



9
10
11
# File 'lib/alma_rest_api.rb', line 9

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



13
14
15
# File 'lib/alma_rest_api.rb', line 13

def configure
  yield(configuration)
end

.delete(uri) ⇒ Object



72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



82
83
84
85
86
87
88
# 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