alma_rest_api 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/alma_rest_api.rb +47 -16
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 03ab118590c4736f9b951740be856a4fd159f5af
4
- data.tar.gz: e3ffca9161f15c71891d519158045f2804d587a3
3
+ metadata.gz: de85edc1bceed958e81d0ef5c116903719ac0ba8
4
+ data.tar.gz: 03c0660e3762d8327ca1ed7c0fc2030a6d20a939
5
5
  SHA512:
6
- metadata.gz: 2349e7d150592d9cb9b4082bd5801a13e2b615b02ae4bb14f22f65d5faca1ecc1f0539ae62055b26870c290b0ecd024c830a2ac70f92ba960702dfa3e004136b
7
- data.tar.gz: 3082871dbdc36f6f6b67f214ecf0f8a6492e9503b5fc2919bc78729404798a3c5519f1b0740a1623a12ec5cc89e5cf43c57bc648d05a610a8a5d0997d21f370b
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 ' + AlmaRestApi.configuration.api_key
23
+ authorization: 'apikey ' + configuration.api_key
24
24
  return JSON.parse(response.body)
25
25
  rescue => e
26
- raise parse_error e.response
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 ' + AlmaRestApi.configuration.api_key,
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 e.response
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 ' + AlmaRestApi.configuration.api_key,
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 e.response
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 ' + AlmaRestApi.configuration.api_key
64
+ authorization: 'apikey ' + configuration.api_key
62
65
  rescue => e
63
- raise parse_error e.response
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
- error = JSON.parse(err)
78
- if error["web_service_result"] #500
79
- return error["web_service_result"]["errorList"]["error"]["errorMessage"]
80
- else #400
81
- return error["errorList"]["error"][0]["errorMessage"]
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 JSON::ParserError
84
- return "Unknown error from Alma"
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alma_rest_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Weisman