berkeley_library-util 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b313922bbe999c373d0094a01734c05aa0e31606aeef894bb0503293a8c78e65
4
- data.tar.gz: f7e67788b3b88035ff9feac00386d45cf5e35b92c5a25a40d311fe8df16e3056
3
+ metadata.gz: c52542412eba63da568f64ede565f97e941321d108f42e89920341df02a268b2
4
+ data.tar.gz: 3a56f6a5dd154c44d3d94e601f3093a8c4aca81e05fb3f7e18cd70ca6389f930
5
5
  SHA512:
6
- metadata.gz: 711daf87bdb397f89302d6ee5c9f00464863947e2d7bd5d24736de48acbe987096b59d814638d4ad2e897f5bf48fc2d1e5037b753c0405c597c6feda39ba1a81
7
- data.tar.gz: dfb544c71430fbda524e3ca4b8ca32d5387fb55683cd6a2eefebaceb6152416afb5d72dae03dc5a2636b3dde4f4be8a5e49695a358a4a4348ff16dc38f3cadf9
6
+ metadata.gz: 0ff8d42319d6faa217bb540d7276a7dd48499a9660242a3c875496594105a16a6bd63190ea234781b8950d269548d4400cabcf87292a161eb16cf8f5a3d205b8
7
+ data.tar.gz: ebfbe371422bbbb59587d583409200968742511013aa49df1f2922df5c9880102c3014ebe7622a7be663cd327a5a742f69c622e8369a912929edad1cc4a0b4fe
data/CHANGES.md CHANGED
@@ -1,10 +1,15 @@
1
+ # 0.1.3 (2022-05-11)
2
+
3
+ - Adds `URIs#get_response`, which returns a complete `RestClient::Response` rather than just
4
+ the response body.
5
+
1
6
  # 0.1.2 (2022-05-05)
2
7
 
3
8
  - Adds `BerkeleyLibrary::Files`, which contains file and IO utility methods
4
9
 
5
10
  # 0.1.1 (2021-09-23)
6
11
 
7
- - `URIs.append` now handles bare URLs with empty paths, with the caveat that
12
+ - `URIs#append` now handles bare URLs with empty paths, with the caveat that
8
13
  a root path `/` will always be added, even if there are no path elements to append.
9
14
 
10
15
  # 0.1.0 (2021-09-23)
@@ -7,7 +7,7 @@ module BerkeleyLibrary
7
7
  SUMMARY = 'Miscellaneous Ruby utilities for the UC Berkeley Library'.freeze
8
8
  DESCRIPTION = 'A collection of miscellaneous Ruby routines for the UC Berkeley Library.'.freeze
9
9
  LICENSE = 'MIT'.freeze
10
- VERSION = '0.1.2'.freeze
10
+ VERSION = '0.1.3'.freeze
11
11
  HOMEPAGE = 'https://github.com/BerkeleyLibrary/util'.freeze
12
12
  end
13
13
  end
@@ -10,7 +10,7 @@ module BerkeleyLibrary
10
10
  class << self
11
11
  include BerkeleyLibrary::Logging
12
12
 
13
- # Performs a GET request.
13
+ # Performs a GET request and returns the response body as a string.
14
14
  #
15
15
  # @param uri [URI, String] the URI to GET
16
16
  # @param params [Hash] the query parameters to add to the URI. (Note that the URI may already include query parameters.)
@@ -18,13 +18,29 @@ module BerkeleyLibrary
18
18
  # @return [String] the body as a string.
19
19
  # @raise [RestClient::Exception] in the event of an error.
20
20
  def get(uri, params: {}, headers: {})
21
- url_str = url_str_with_params(uri, params)
22
- resp = get_or_raise(url_str, headers)
21
+ resp = make_get_request(uri, params, headers)
23
22
  resp.body
24
23
  end
25
24
 
25
+ # Performs a GET request and returns the response.
26
+ #
27
+ # @param uri [URI, String] the URI to GET
28
+ # @param params [Hash] the query parameters to add to the URI. (Note that the URI may already include query parameters.)
29
+ # @param headers [Hash] the request headers.
30
+ # @return [RestClient::Response] the body as a string.
31
+ def get_response(uri, params: {}, headers: {})
32
+ make_get_request(uri, params, headers)
33
+ rescue RestClient::Exception => e
34
+ e.response
35
+ end
36
+
26
37
  private
27
38
 
39
+ def make_get_request(uri, params, headers)
40
+ url_str = url_str_with_params(uri, params)
41
+ get_or_raise(url_str, headers)
42
+ end
43
+
28
44
  def url_str_with_params(uri, params)
29
45
  raise ArgumentError, 'uri cannot be nil' unless (url_str = Validator.url_str_or_nil(uri))
30
46
 
@@ -40,6 +56,7 @@ module BerkeleyLibrary
40
56
  uri.to_s
41
57
  end
42
58
 
59
+ # @return [RestClient::Response]
43
60
  def get_or_raise(url_str, headers)
44
61
  resp = RestClient.get(url_str, headers)
45
62
  begin
@@ -47,6 +64,7 @@ module BerkeleyLibrary
47
64
 
48
65
  raise(exception_for(resp, status))
49
66
  ensure
67
+ # noinspection RubyMismatchedReturnType
50
68
  logger.info("GET #{url_str} returned #{status}")
51
69
  end
52
70
  end
@@ -32,6 +32,16 @@ module BerkeleyLibrary
32
32
  Requester.get(uri, params: params, headers: headers)
33
33
  end
34
34
 
35
+ # Performs a GET request and returns the response.
36
+ #
37
+ # @param uri [URI, String] the URI to GET
38
+ # @param params [Hash] the query parameters to add to the URI. (Note that the URI may already include query parameters.)
39
+ # @param headers [Hash] the request headers.
40
+ # @return [RestClient::Response] the body as a string.
41
+ def get_response(uri, params: {}, headers: {})
42
+ Requester.get_response(uri, params: params, headers: headers)
43
+ end
44
+
35
45
  # Returns the specified URL as a URI.
36
46
  # @param url [String, URI] the URL.
37
47
  # @return [URI] the URI.
@@ -138,18 +138,44 @@ module BerkeleyLibrary::Util
138
138
  end
139
139
  end
140
140
 
141
- describe :get do
142
- it 'makes a GET request' do
143
- url = 'https://example.org/'
144
- params = { p1: 1, p2: 2 }
145
- headers = { 'X-help' => 'I am trapped in a unit test' }
146
-
147
- url_with_query = "#{url}?#{URI.encode_www_form(params)}"
148
- expected_body = 'Help! I am trapped in a unit test'
149
- stub_request(:get, url_with_query).with(headers: headers).to_return(body: expected_body)
150
-
151
- result = URIs.get(url, params: params, headers: headers)
152
- expect(result).to eq(expected_body)
141
+ describe 'requests' do
142
+ let(:url) { 'https://example.org/' }
143
+ let(:params) { { p1: 1, p2: 2 } }
144
+ let(:headers) { { 'X-help' => 'I am trapped in a unit test' } }
145
+ let(:url_with_query) { "#{url}?#{URI.encode_www_form(params)}" }
146
+ let(:expected_body) { 'Help! I am trapped in a unit test' }
147
+
148
+ describe :get do
149
+ it 'makes a GET request' do
150
+ stub_request(:get, url_with_query).with(headers: headers).to_return(body: expected_body)
151
+
152
+ result = URIs.get(url, params: params, headers: headers)
153
+ expect(result).to eq(expected_body)
154
+ end
155
+
156
+ it 'raises an error in the event of a 404' do
157
+ stub_request(:get, url_with_query).with(headers: headers).to_return(status: 404, body: expected_body)
158
+
159
+ expect { URIs.get(url, params: params, headers: headers) }.to raise_error(RestClient::NotFound)
160
+ end
161
+ end
162
+
163
+ describe :get_response do
164
+ it 'makes a GET request' do
165
+ stub_request(:get, url_with_query).with(headers: headers).to_return(body: expected_body)
166
+
167
+ response = URIs.get_response(url, params: params, headers: headers)
168
+ expect(response.body).to eq(expected_body)
169
+ expect(response.code).to eq(200)
170
+ end
171
+
172
+ it 'returns the response even for errors' do
173
+ stub_request(:get, url_with_query).with(headers: headers).to_return(status: 404, body: expected_body)
174
+
175
+ response = URIs.get_response(url, params: params, headers: headers)
176
+ expect(response.body).to eq(expected_body)
177
+ expect(response.code).to eq(404)
178
+ end
153
179
  end
154
180
  end
155
181
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berkeley_library-util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Moles
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-05 00:00:00.000000000 Z
11
+ date: 2022-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: berkeley_library-logging