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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: baed86642c0c37f151d41d58a12e0bf73cc8be4441e42082dec217cbb921d957
4
- data.tar.gz: a506ed62ea0a49d24e5a10c2945b56a7e298114df36730d99f3fb3e3fb48d299
3
+ metadata.gz: abce07b88fce37bc8244e708a8b564499673c4de5391822387b5eb85219803a1
4
+ data.tar.gz: 5afd7519510296c17262f61bebe61731fbba16dbfee484a7cfbbbc2a97c92ab5
5
5
  SHA512:
6
- metadata.gz: 3ad4dd9cd51bae9cc9626acc59fd1e114e4a666f367914b16af3135797842cf9f890a801fcc6f2509f01751c20368f15a139c53c49ab2f38b8840851146f30a0
7
- data.tar.gz: 3cad6750ed114c2e4abe01ca74ec5dc708f1ed298eab7694597dfd5f7fe2e19fae35667f5c3660059b42b438b0bc39257e5e1d4a3c8abbd0bc45c56abf66ba96
6
+ metadata.gz: efaadb1850294c22ce5cc28693017ba5302b44ec1ee944f12612cb4b55672fd96d2ef97ff30fba56c0653e8bd179c863b53aee88370110958a72eab7b7d75fee
7
+ data.tar.gz: d6d1ce0b137b599c0a3964c2d13fdf8065ecd16446135402ebb1d30a65add4864a246bde27f9b1bbf5ca4af6d20af00169adb696d7094cb0049fa63bbe9df7e0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prest (0.1.6)
4
+ prest (0.1.7)
5
5
  httparty (~> 0.20.0)
6
6
 
7
7
  GEM
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::Error` when the HTTP request is not successful, use the bang methods (`get!`, `post!`, `put!`, `patch!` and `delete!`).
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::Error with the response as a json in the message.
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::Error => e
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 ::Prest::Error, e.message
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
- ::Kernel.raise ::Prest::Error, res.body.to_json unless res.successful?
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Prest
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.7'
5
5
  end
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.6
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-02 00:00:00.000000000 Z
11
+ date: 2022-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty