prest 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +14 -1
- data/lib/prest/client.rb +74 -0
- data/lib/prest/service.rb +36 -0
- data/lib/prest/version.rb +1 -1
- data/lib/prest.rb +2 -102
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: baed86642c0c37f151d41d58a12e0bf73cc8be4441e42082dec217cbb921d957
|
4
|
+
data.tar.gz: a506ed62ea0a49d24e5a10c2945b56a7e298114df36730d99f3fb3e3fb48d299
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ad4dd9cd51bae9cc9626acc59fd1e114e4a666f367914b16af3135797842cf9f890a801fcc6f2509f01751c20368f15a139c53c49ab2f38b8840851146f30a0
|
7
|
+
data.tar.gz: 3cad6750ed114c2e4abe01ca74ec5dc708f1ed298eab7694597dfd5f7fe2e19fae35667f5c3660059b42b438b0bc39257e5e1d4a3c8abbd0bc45c56abf66ba96
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.1.1
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -54,10 +54,23 @@ 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::Error` 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.
|
data/lib/prest/client.rb
ADDED
@@ -0,0 +1,74 @@
|
|
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::Error, e.message
|
40
|
+
end
|
41
|
+
|
42
|
+
def execute_query!(*args, **kwargs)
|
43
|
+
res = execute_query(*args, **kwargs)
|
44
|
+
::Kernel.raise ::Prest::Error, res.body.to_json unless res.successful?
|
45
|
+
|
46
|
+
res
|
47
|
+
end
|
48
|
+
|
49
|
+
def chain_fragment(fragment_name, *args, **kwargs)
|
50
|
+
arguments = args.join('/')
|
51
|
+
parsed_args = arguments.empty? ? '' : "/#{arguments}"
|
52
|
+
@query_params.merge!(kwargs)
|
53
|
+
@fragments << "#{fragment_name.gsub("__", "-")}#{parsed_args}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def headers
|
57
|
+
tmp_headers = @options[:headers] || {}
|
58
|
+
tmp_headers.merge!('Content-Type' => 'application/json', 'Accept' => 'application/json') if @options[:json]
|
59
|
+
tmp_headers
|
60
|
+
end
|
61
|
+
|
62
|
+
def build_url
|
63
|
+
path = @fragments.join('/')
|
64
|
+
|
65
|
+
stringified_params = ''
|
66
|
+
@query_params.to_a.each do |key_val|
|
67
|
+
stringified_params += "#{key_val[0]}=#{key_val[1]}&"
|
68
|
+
end
|
69
|
+
|
70
|
+
stringified_params = stringified_params.empty? ? '' : "?#{stringified_params[0..-2]}"
|
71
|
+
"#{@base_uri}/#{path}#{stringified_params}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
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
data/lib/prest.rb
CHANGED
@@ -1,111 +1,11 @@
|
|
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
|
-
|
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
|
14
|
-
|
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
|
-
{}
|
109
|
-
end
|
110
|
-
end
|
111
11
|
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.6
|
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-
|
11
|
+
date: 2022-09-02 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
|