prest 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +6 -3
- data/lib/prest/client.rb +9 -2
- data/lib/prest/version.rb +1 -1
- data/lib/prest.rb +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abce07b88fce37bc8244e708a8b564499673c4de5391822387b5eb85219803a1
|
4
|
+
data.tar.gz: 5afd7519510296c17262f61bebe61731fbba16dbfee484a7cfbbbc2a97c92ab5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efaadb1850294c22ce5cc28693017ba5302b44ec1ee944f12612cb4b55672fd96d2ef97ff30fba56c0653e8bd179c863b53aee88370110958a72eab7b7d75fee
|
7
|
+
data.tar.gz: d6d1ce0b137b599c0a3964c2d13fdf8065ecd16446135402ebb1d30a65add4864a246bde27f9b1bbf5ca4af6d20af00169adb696d7094cb0049fa63bbe9df7e0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -70,18 +70,21 @@ Note: The option will merge any other header you pass to the initializer.
|
|
70
70
|
### Raising exceptions on failed HTTP requests
|
71
71
|
|
72
72
|
An HTTP request is considered as failed when the status code is not between `100` and `299`.
|
73
|
-
To automatically raise a `Prest::
|
73
|
+
To automatically raise a `Prest::RequestError` when the HTTP request is not successful, use the bang methods (`get!`, `post!`, `put!`, `patch!` and `delete!`).
|
74
74
|
|
75
75
|
```ruby
|
76
76
|
# If for example the authorization headers are invalid, it will return an 401 status code.
|
77
|
-
# This call will raise a ::Prest::
|
77
|
+
# This call will raise a ::Prest::RequestError with the response as a json in the message.
|
78
78
|
|
79
79
|
begin
|
80
80
|
Prest::Client.new('https://example.com/api', { headers: { 'Authorization' => 'Bearer Token xxxyyyzzz' } })
|
81
81
|
.users
|
82
82
|
.get!
|
83
|
-
rescue Prest::
|
83
|
+
rescue Prest::RequestError => e
|
84
84
|
puts e.message # "{ error: \"Invalid auth credentials\" }"
|
85
|
+
puts e.status # 403
|
86
|
+
puts e.body # "{ error: \"Invalid auth credentials\" }"
|
87
|
+
puts e.headers # { 'ContentType' => 'application/json' }
|
85
88
|
end
|
86
89
|
```
|
87
90
|
|
data/lib/prest/client.rb
CHANGED
@@ -36,12 +36,19 @@ module Prest
|
|
36
36
|
res = ::HTTParty.send(http_method, build_url, headers: headers, body: body)
|
37
37
|
::Prest::Response.new(res.code, res.parsed_response, res.headers)
|
38
38
|
rescue ::HTTParty::ResponseError => e
|
39
|
-
::Kernel.raise
|
39
|
+
::Kernel.raise(::Prest::RequestError.new(status: res.status,
|
40
|
+
body: res.parsed_response,
|
41
|
+
headers: res.headers), e.message)
|
40
42
|
end
|
41
43
|
|
42
44
|
def execute_query!(*args, **kwargs)
|
43
45
|
res = execute_query(*args, **kwargs)
|
44
|
-
|
46
|
+
|
47
|
+
unless res.successful?
|
48
|
+
::Kernel.raise(::Prest::RequestError.new(status: res.status,
|
49
|
+
body: res.body.to_json,
|
50
|
+
headers: res.headers), res.body.to_json)
|
51
|
+
end
|
45
52
|
|
46
53
|
res
|
47
54
|
end
|
data/lib/prest/version.rb
CHANGED
data/lib/prest.rb
CHANGED
@@ -8,4 +8,16 @@ require_relative 'prest/response'
|
|
8
8
|
|
9
9
|
module Prest
|
10
10
|
class Error < StandardError; end
|
11
|
+
|
12
|
+
# Error for when a request is unsuccessful.
|
13
|
+
class RequestError < StandardError
|
14
|
+
attr_reader :status, :body, :headers
|
15
|
+
|
16
|
+
def initialize(status:, body: '', headers: {})
|
17
|
+
super()
|
18
|
+
@status = status
|
19
|
+
@body = body
|
20
|
+
@headers = headers
|
21
|
+
end
|
22
|
+
end
|
11
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Aparicio
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-09-
|
11
|
+
date: 2022-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|