alma_rest_api 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/alma_rest_api.rb +47 -16
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de85edc1bceed958e81d0ef5c116903719ac0ba8
|
4
|
+
data.tar.gz: 03c0660e3762d8327ca1ed7c0fc2030a6d20a939
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b547ba6dd80c5931d200e525653e7df602c643ae08ca6a395297c666f87445bb93628af78cddf307e5cffaf11ffb9c72b7a72cf730c44552bd76112779e02f2
|
7
|
+
data.tar.gz: ea85f51c9c5065a6808dce7d5ecb90e470ac1ef7184e3fbf26901300ae655018f96cd8f12712b99bb1a899bcb940a149ee3ec808e22fcb30310867f3147c0837
|
data/lib/alma_rest_api.rb
CHANGED
@@ -11,56 +11,59 @@ module AlmaRestApi
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def configure
|
14
|
-
self.configuration ||= Configuration.new
|
15
14
|
yield(configuration)
|
16
15
|
end
|
17
16
|
|
18
17
|
def get(uri)
|
18
|
+
check_config
|
19
19
|
begin
|
20
20
|
response =
|
21
21
|
RestClient.get uri(uri),
|
22
22
|
accept: :json,
|
23
|
-
authorization: 'apikey ' +
|
23
|
+
authorization: 'apikey ' + configuration.api_key
|
24
24
|
return JSON.parse(response.body)
|
25
25
|
rescue => e
|
26
|
-
raise parse_error
|
26
|
+
raise AlmaApiError, parse_error(e.response)
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
def put(uri, data)
|
31
|
+
check_config
|
31
32
|
begin
|
32
33
|
response =
|
33
34
|
RestClient.put uri(uri),
|
34
35
|
data.to_json,
|
35
36
|
accept: :json,
|
36
|
-
authorization: 'apikey ' +
|
37
|
+
authorization: 'apikey ' + configuration.api_key,
|
37
38
|
content_type: :json
|
38
39
|
return JSON.parse(response.body)
|
39
40
|
rescue => e
|
40
|
-
raise parse_error
|
41
|
+
raise AlmaApiError, parse_error(e.response)
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
44
45
|
def post(uri, data)
|
46
|
+
check_config
|
45
47
|
begin
|
46
48
|
response =
|
47
49
|
RestClient.post uri(uri),
|
48
50
|
data.to_json,
|
49
51
|
accept: :json,
|
50
|
-
authorization: 'apikey ' +
|
52
|
+
authorization: 'apikey ' + configuration.api_key,
|
51
53
|
content_type: :json
|
52
54
|
return JSON.parse(response.body)
|
53
55
|
rescue => e
|
54
|
-
raise parse_error
|
56
|
+
raise AlmaApiError, parse_error(e.response)
|
55
57
|
end
|
56
58
|
end
|
57
59
|
|
58
60
|
def delete(uri)
|
61
|
+
check_config
|
59
62
|
begin
|
60
63
|
RestClient.delete uri(uri),
|
61
|
-
authorization: 'apikey ' +
|
64
|
+
authorization: 'apikey ' + configuration.api_key
|
62
65
|
rescue => e
|
63
|
-
raise parse_error
|
66
|
+
raise AlmaApiError, parse_error(e.response)
|
64
67
|
end
|
65
68
|
end
|
66
69
|
|
@@ -72,16 +75,31 @@ module AlmaRestApi
|
|
72
75
|
end
|
73
76
|
end
|
74
77
|
|
78
|
+
def check_config
|
79
|
+
raise NoApiKeyError if configuration.api_key.nil? || configuration.api_key.empty?
|
80
|
+
end
|
81
|
+
|
75
82
|
def parse_error(err)
|
76
83
|
begin
|
77
|
-
|
78
|
-
|
79
|
-
return
|
80
|
-
|
81
|
-
|
84
|
+
if err[0] == '<'
|
85
|
+
msg = err.match(/<errorMessage>(.*)<\/errorMessage>/)
|
86
|
+
return msg ? msg[1] : ''
|
87
|
+
elsif err[0] == '{'
|
88
|
+
begin
|
89
|
+
error = JSON.parse(err)
|
90
|
+
if error["web_service_result"] #500
|
91
|
+
return error["web_service_result"]["errorList"]["error"]["errorMessage"]
|
92
|
+
else #400
|
93
|
+
return error["errorList"]["error"][0]["errorMessage"]
|
94
|
+
end
|
95
|
+
rescue JSON::ParserError
|
96
|
+
return "Unknown error from Alma"
|
97
|
+
end
|
98
|
+
else
|
99
|
+
return err
|
82
100
|
end
|
83
|
-
rescue
|
84
|
-
return
|
101
|
+
rescue
|
102
|
+
return err
|
85
103
|
end
|
86
104
|
end
|
87
105
|
end
|
@@ -96,3 +114,16 @@ class Configuration
|
|
96
114
|
@api_path = ENV['ALMA_API_PATH'] || "https://api-na.hosted.exlibrisgroup.com/almaws/v1"
|
97
115
|
end
|
98
116
|
end
|
117
|
+
|
118
|
+
class NoApiKeyError < StandardError
|
119
|
+
def initialize(msg="No API key defined")
|
120
|
+
super
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
class AlmaApiError < StandardError
|
125
|
+
def initialize(msg)
|
126
|
+
msg = "Unknown error from Alma" if msg.empty?
|
127
|
+
super
|
128
|
+
end
|
129
|
+
end
|