prest 0.1.3 → 0.1.4

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: 70f03f1a6b0308bf6289c7b7c1158c17622999fd54fe788cf7f5c970d8100ed4
4
- data.tar.gz: dabc32ac8bbaf16b4a44519cfa6206ee93121a5c9c666dd6e760b4bd720cf06b
3
+ metadata.gz: 64b97c654e18ed1cd49daaa3923f1dc3eb227216311e051977db5e5cdffa0999
4
+ data.tar.gz: f8e546e1f5feacdd22a8cea434840957d455d61ca70bf80ef627a3ecda2e08e6
5
5
  SHA512:
6
- metadata.gz: 61695f52e98eb93f4c9a321771ca0061ba203640a1bdf971c390767d78b08a0e2f11e3ad734213d04778bf2ae303427e183d1e957be6d4864927a92906c97390
7
- data.tar.gz: 8121063ee97d2cfad9a68e68ae16b8ad12248423cd17286feae94c826fd6e6f05e2edfd88a3890c4bfecf7f2eafdf55ae3189dac2d1e097886253877096d96ed
6
+ metadata.gz: f6c29682362e53853dc48d4f5b26bce57a2b33814b91418aa15afec036a942f31cdb8c58628cc21da64488d46d3d9c28793578c2efb7cc62472524c413cd3453
7
+ data.tar.gz: 1be14eb778ccb8c8f9218abe9db5c29089234b290154a98c167359bad911de9a130775a669239b48edd6695b5eb75a8c7c1c5602b6a5089cbda9183b4eab5496
data/CHANGELOG.md CHANGED
@@ -6,4 +6,12 @@
6
6
 
7
7
  ## [0.1.1] - 2022-07-22
8
8
 
9
- - Wraps endpoints response in a `Prest::Response` object for easier access to the response data
9
+ - Wraps endpoints response in a `Prest::Response` object for easier access to the response data
10
+
11
+ ## [0.1.2] - 2022-08-05
12
+
13
+ - Change how urls are built. Underscore in the fragment building stays as an underscore. Dashes are made with `__`
14
+
15
+ ## [0.1.3] - 2022-08-05
16
+
17
+ - Add `Prest::Service` object for easier rails integration with service-object pattern
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prest (0.1.3)
4
+ prest (0.1.4)
5
5
  httparty (~> 0.20.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -54,6 +54,24 @@ Prest::Client.new('https://example.com/api', { headers: { 'Authorization' => 'Be
54
54
  .post(body: { username: 'juan-apa' })
55
55
  ```
56
56
 
57
+ ### Raising exceptions on failed HTTP requests
58
+
59
+ An HTTP request is considered as failed when the status code is not between `100` and `299`.
60
+ To automatically raise a `Prest::Error` when the HTTP request is not successful, use the bang methods (`get!`, `post!`, `put!`, `patch!` and `delete!` ).
61
+
62
+ ```ruby
63
+ # If for example the authorization headers are invalid, it will return an 401 status code.
64
+ # This call will raise a ::Prest::Error with the response as a json in the message.
65
+
66
+ begin
67
+ Prest::Client.new('https://example.com/api', { headers: { 'Authorization' => 'Bearer Token xxxyyyzzz' } })
68
+ .users
69
+ .get!
70
+ rescue Prest::Error => e
71
+ puts e.message # "{ error: \"Invalid auth credentials\" }"
72
+ end
73
+ ```
74
+
57
75
  ### Accessing the response
58
76
 
59
77
  ```ruby
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.3'
4
+ VERSION = '0.1.4'
5
5
  end
data/lib/prest.rb CHANGED
@@ -10,7 +10,7 @@ module Prest
10
10
  # Main wrapper class for Prest. To use the gem, call:
11
11
  # Prest::Client.new('https://base_uri.com', { headers: { 'Authorization' => 'Bearer token' }})
12
12
  class Client < BasicObject
13
- SUPPORTED_HTTP_VERBS = %i[get post put patch delete].freeze
13
+ SUPPORTED_HTTP_VERBS = %i[get post put patch delete get! post! put! patch! delete!].freeze
14
14
 
15
15
  def initialize(base_uri, options = {})
16
16
  @base_uri = base_uri
@@ -21,7 +21,11 @@ module Prest
21
21
 
22
22
  def method_missing(method, *args, **kwargs)
23
23
  if SUPPORTED_HTTP_VERBS.include?(method)
24
- execute_query(method, **kwargs)
24
+ if method.to_s.end_with?('!')
25
+ execute_query!(method[0..-2], **kwargs)
26
+ else
27
+ execute_query(method, **kwargs)
28
+ end
25
29
  else
26
30
  chain_fragment(method.to_s, *args, **kwargs)
27
31
  self
@@ -38,7 +42,14 @@ module Prest
38
42
  res = ::HTTParty.send(http_method, build_url, headers: headers, body: body)
39
43
  ::Prest::Response.new(res.code, res.parsed_response, res.headers)
40
44
  rescue ::HTTParty::ResponseError => e
41
- raise Error, e.message
45
+ ::Kernel.raise ::Prest::Error, e.message
46
+ end
47
+
48
+ def execute_query!(*args, **kwargs)
49
+ res = execute_query(*args, **kwargs)
50
+ ::Kernel.raise ::Prest::Error, res.body.to_json unless res.successful?
51
+
52
+ res
42
53
  end
43
54
 
44
55
  def chain_fragment(fragment_name, *args, **kwargs)
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.3
4
+ version: 0.1.4
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-08-05 00:00:00.000000000 Z
11
+ date: 2022-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty