prest 0.1.5 → 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: aef2d0096f535fe7a735571b6d3a556aed2ec746e50378b13e2b4218a788dc29
4
- data.tar.gz: 7ae1c9c4fcc036adcc7484453caeda0fd31e78c5318666a2b20a9ad516de2cbe
3
+ metadata.gz: abce07b88fce37bc8244e708a8b564499673c4de5391822387b5eb85219803a1
4
+ data.tar.gz: 5afd7519510296c17262f61bebe61731fbba16dbfee484a7cfbbbc2a97c92ab5
5
5
  SHA512:
6
- metadata.gz: 5ed398def4e6713c62bcab45447ed7f466c517b6786e9bf3ea3f419cab97d8e48c5ca336e6e2b0995e4f4a6ace68476b3aa31d753d6fef061f630e6062b07a73
7
- data.tar.gz: 7f748b2cf758c6d41bb8d907d631f31167970dd54a3b9b9751c73d98f56c960525ae10990e17ad391c5b3d24d0fb80435f18232d7c8e5cf668e6bdad80142826
6
+ metadata.gz: efaadb1850294c22ce5cc28693017ba5302b44ec1ee944f12612cb4b55672fd96d2ef97ff30fba56c0653e8bd179c863b53aee88370110958a72eab7b7d75fee
7
+ data.tar.gz: d6d1ce0b137b599c0a3964c2d13fdf8065ecd16446135402ebb1d30a65add4864a246bde27f9b1bbf5ca4af6d20af00169adb696d7094cb0049fa63bbe9df7e0
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prest (0.1.5)
4
+ prest (0.1.7)
5
5
  httparty (~> 0.20.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -54,21 +54,37 @@ Prest::Client.new('https://example.com/api', { headers: { 'Authorization' => 'Be
54
54
  .post(body: { username: 'juan-apa' })
55
55
  ```
56
56
 
57
+ ### Automatically adding the json headers
58
+
59
+ Because some API's need a `Content-Type: application/json` and/or `Accept: application/json` headers, there's a built in option that can be passed to the client to add those for you:
60
+
61
+ ```ruby
62
+ Prest::Client.new('https://example.com/api', { json: true })
63
+ .users
64
+ .get
65
+ # Makes a GET https://example.com/api/users with headers Content-Type: application/json and Accept: application/json
66
+ ```
67
+
68
+ Note: The option will merge any other header you pass to the initializer.
69
+
57
70
  ### Raising exceptions on failed HTTP requests
58
71
 
59
72
  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!` ).
73
+ To automatically raise a `Prest::RequestError` when the HTTP request is not successful, use the bang methods (`get!`, `post!`, `put!`, `patch!` and `delete!`).
61
74
 
62
75
  ```ruby
63
76
  # 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.
77
+ # This call will raise a ::Prest::RequestError with the response as a json in the message.
65
78
 
66
79
  begin
67
80
  Prest::Client.new('https://example.com/api', { headers: { 'Authorization' => 'Bearer Token xxxyyyzzz' } })
68
81
  .users
69
82
  .get!
70
- rescue Prest::Error => e
83
+ rescue Prest::RequestError => e
71
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' }
72
88
  end
73
89
  ```
74
90
 
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Prest
4
+ # Main wrapper class for Prest. To use the gem, call:
5
+ # Prest::Client.new('https://base_uri.com', { headers: { 'Authorization' => 'Bearer token' }})
6
+ class Client < BasicObject
7
+ SUPPORTED_HTTP_VERBS = %i[get post put patch delete get! post! put! patch! delete!].freeze
8
+
9
+ def initialize(base_uri, options = {})
10
+ @base_uri = base_uri
11
+ @options = options
12
+ @fragments = []
13
+ @query_params = {}
14
+ end
15
+
16
+ def method_missing(method, *args, **kwargs)
17
+ if SUPPORTED_HTTP_VERBS.include?(method)
18
+ if method.to_s.end_with?('!')
19
+ execute_query!(method[0..-2], **kwargs)
20
+ else
21
+ execute_query(method, **kwargs)
22
+ end
23
+ else
24
+ chain_fragment(method.to_s, *args, **kwargs)
25
+ self
26
+ end
27
+ end
28
+
29
+ def respond_to_missing?(_, _)
30
+ true
31
+ end
32
+
33
+ private
34
+
35
+ def execute_query(http_method, body: {})
36
+ res = ::HTTParty.send(http_method, build_url, headers: headers, body: body)
37
+ ::Prest::Response.new(res.code, res.parsed_response, res.headers)
38
+ rescue ::HTTParty::ResponseError => e
39
+ ::Kernel.raise(::Prest::RequestError.new(status: res.status,
40
+ body: res.parsed_response,
41
+ headers: res.headers), e.message)
42
+ end
43
+
44
+ def execute_query!(*args, **kwargs)
45
+ res = execute_query(*args, **kwargs)
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
52
+
53
+ res
54
+ end
55
+
56
+ def chain_fragment(fragment_name, *args, **kwargs)
57
+ arguments = args.join('/')
58
+ parsed_args = arguments.empty? ? '' : "/#{arguments}"
59
+ @query_params.merge!(kwargs)
60
+ @fragments << "#{fragment_name.gsub("__", "-")}#{parsed_args}"
61
+ end
62
+
63
+ def headers
64
+ tmp_headers = @options[:headers] || {}
65
+ tmp_headers.merge!('Content-Type' => 'application/json', 'Accept' => 'application/json') if @options[:json]
66
+ tmp_headers
67
+ end
68
+
69
+ def build_url
70
+ path = @fragments.join('/')
71
+
72
+ stringified_params = ''
73
+ @query_params.to_a.each do |key_val|
74
+ stringified_params += "#{key_val[0]}=#{key_val[1]}&"
75
+ end
76
+
77
+ stringified_params = stringified_params.empty? ? '' : "?#{stringified_params[0..-2]}"
78
+ "#{@base_uri}/#{path}#{stringified_params}"
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Prest
4
+ # Base Service Object from which to extend to integrate with api's using the Prest gem.
5
+ class Service < BasicObject
6
+ def method_missing(method, *args, **kwargs)
7
+ client.__send__(method, *args, **kwargs)
8
+ end
9
+
10
+ def self.method_missing(method, *args, **kwargs)
11
+ new.__send__(:client).__send__(method, *args, **kwargs)
12
+ end
13
+
14
+ def self.respond_to_missing?(_, _)
15
+ true
16
+ end
17
+
18
+ def respond_to_missing?(_, _)
19
+ true
20
+ end
21
+
22
+ protected
23
+
24
+ def client
25
+ @client ||= ::Prest::Client.new(base_uri, options)
26
+ end
27
+
28
+ def base_uri
29
+ ::Kernel.raise(Error, 'Implement in subclass')
30
+ end
31
+
32
+ def options
33
+ {}
34
+ end
35
+ end
36
+ 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.5'
4
+ VERSION = '0.1.7'
5
5
  end
data/lib/prest.rb CHANGED
@@ -1,111 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'httparty'
4
+ require_relative 'prest/client'
5
+ require_relative 'prest/service'
4
6
  require_relative 'prest/version'
5
7
  require_relative 'prest/response'
6
8
 
7
9
  module Prest
8
10
  class Error < StandardError; end
9
11
 
10
- # Main wrapper class for Prest. To use the gem, call:
11
- # Prest::Client.new('https://base_uri.com', { headers: { 'Authorization' => 'Bearer token' }})
12
- class Client < BasicObject
13
- SUPPORTED_HTTP_VERBS = %i[get post put patch delete get! post! put! patch! delete!].freeze
12
+ # Error for when a request is unsuccessful.
13
+ class RequestError < StandardError
14
+ attr_reader :status, :body, :headers
14
15
 
15
- def initialize(base_uri, options = {})
16
- @base_uri = base_uri
17
- @options = options
18
- @fragments = []
19
- @query_params = {}
20
- end
21
-
22
- def method_missing(method, *args, **kwargs)
23
- if SUPPORTED_HTTP_VERBS.include?(method)
24
- if method.to_s.end_with?('!')
25
- execute_query!(method[0..-2], **kwargs)
26
- else
27
- execute_query(method, **kwargs)
28
- end
29
- else
30
- chain_fragment(method.to_s, *args, **kwargs)
31
- self
32
- end
33
- end
34
-
35
- def respond_to_missing?(_, _)
36
- true
37
- end
38
-
39
- private
40
-
41
- def execute_query(http_method, body: {})
42
- res = ::HTTParty.send(http_method, build_url, headers: headers, body: body)
43
- ::Prest::Response.new(res.code, res.parsed_response, res.headers)
44
- rescue ::HTTParty::ResponseError => e
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
53
- end
54
-
55
- def chain_fragment(fragment_name, *args, **kwargs)
56
- arguments = args.join('/')
57
- parsed_args = arguments.empty? ? '' : "/#{arguments}"
58
- @query_params.merge!(kwargs)
59
- @fragments << "#{fragment_name.gsub("__", "-")}#{parsed_args}"
60
- end
61
-
62
- def headers
63
- @options[:headers] || {}
64
- end
65
-
66
- def build_url
67
- path = @fragments.join('/')
68
-
69
- stringified_params = ''
70
- @query_params.to_a.each do |key_val|
71
- stringified_params += "#{key_val[0]}=#{key_val[1]}&"
72
- end
73
-
74
- stringified_params = stringified_params.empty? ? '' : "?#{stringified_params[0..-2]}"
75
- "#{@base_uri}/#{path}#{stringified_params}"
76
- end
77
- end
78
-
79
- # Base Service Object from which to extend to integrate with api's using the Prest gem.
80
- class Service < BasicObject
81
- def method_missing(method, *args, **kwargs)
82
- client.__send__(method, *args, **kwargs)
83
- end
84
-
85
- def self.method_missing(method, *args, **kwargs)
86
- new.__send__(:client).__send__(method, *args, **kwargs)
87
- end
88
-
89
- def self.respond_to_missing?(_, _)
90
- true
91
- end
92
-
93
- def respond_to_missing?(_, _)
94
- true
95
- end
96
-
97
- protected
98
-
99
- def client
100
- @client ||= ::Prest::Client.new(base_uri, options)
101
- end
102
-
103
- def base_uri
104
- ::Kernel.raise(Error, 'Implement in subclass')
105
- end
106
-
107
- def options
108
- {}
16
+ def initialize(status:, body: '', headers: {})
17
+ super()
18
+ @status = status
19
+ @body = body
20
+ @headers = headers
109
21
  end
110
22
  end
111
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.5
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-08-10 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
@@ -33,6 +33,7 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - ".rspec"
35
35
  - ".rubocop.yml"
36
+ - ".ruby-version"
36
37
  - CHANGELOG.md
37
38
  - CODE_OF_CONDUCT.md
38
39
  - Gemfile
@@ -41,7 +42,9 @@ files:
41
42
  - README.md
42
43
  - Rakefile
43
44
  - lib/prest.rb
45
+ - lib/prest/client.rb
44
46
  - lib/prest/response.rb
47
+ - lib/prest/service.rb
45
48
  - lib/prest/version.rb
46
49
  - prest.gemspec
47
50
  homepage: https://gogrow.dev