prest 0.1.3 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/CHANGELOG.md +9 -1
- data/Gemfile.lock +1 -1
- data/README.md +31 -0
- 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 -91
- 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/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
data/README.md
CHANGED
@@ -54,6 +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
|
+
|
70
|
+
### Raising exceptions on failed HTTP requests
|
71
|
+
|
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!`).
|
74
|
+
|
75
|
+
```ruby
|
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.
|
78
|
+
|
79
|
+
begin
|
80
|
+
Prest::Client.new('https://example.com/api', { headers: { 'Authorization' => 'Bearer Token xxxyyyzzz' } })
|
81
|
+
.users
|
82
|
+
.get!
|
83
|
+
rescue Prest::Error => e
|
84
|
+
puts e.message # "{ error: \"Invalid auth credentials\" }"
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
57
88
|
### Accessing the response
|
58
89
|
|
59
90
|
```ruby
|
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,100 +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].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
|
-
execute_query(method, **kwargs)
|
25
|
-
else
|
26
|
-
chain_fragment(method.to_s, *args, **kwargs)
|
27
|
-
self
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def respond_to_missing?(_, _)
|
32
|
-
true
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def execute_query(http_method, body: {})
|
38
|
-
res = ::HTTParty.send(http_method, build_url, headers: headers, body: body)
|
39
|
-
::Prest::Response.new(res.code, res.parsed_response, res.headers)
|
40
|
-
rescue ::HTTParty::ResponseError => e
|
41
|
-
raise Error, e.message
|
42
|
-
end
|
43
|
-
|
44
|
-
def chain_fragment(fragment_name, *args, **kwargs)
|
45
|
-
arguments = args.join('/')
|
46
|
-
parsed_args = arguments.empty? ? '' : "#{arguments}/"
|
47
|
-
@query_params.merge!(kwargs)
|
48
|
-
@fragments << "#{fragment_name.gsub("__", "-")}/#{parsed_args}"
|
49
|
-
end
|
50
|
-
|
51
|
-
def headers
|
52
|
-
@options[:headers] || {}
|
53
|
-
end
|
54
|
-
|
55
|
-
def build_url
|
56
|
-
path = @fragments.join('/')
|
57
|
-
|
58
|
-
stringified_params = ''
|
59
|
-
@query_params.to_a.each do |key_val|
|
60
|
-
stringified_params += "#{key_val[0]}=#{key_val[1]}&"
|
61
|
-
end
|
62
|
-
|
63
|
-
stringified_params = stringified_params.empty? ? '' : "?#{stringified_params[0..-2]}"
|
64
|
-
"#{@base_uri}/#{path}#{stringified_params}"
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
# Base Service Object from which to extend to integrate with api's using the Prest gem.
|
69
|
-
class Service < BasicObject
|
70
|
-
def method_missing(method, *args, **kwargs)
|
71
|
-
client.__send__(method, *args, **kwargs)
|
72
|
-
end
|
73
|
-
|
74
|
-
def self.method_missing(method, *args, **kwargs)
|
75
|
-
new.__send__(:client).__send__(method, *args, **kwargs)
|
76
|
-
end
|
77
|
-
|
78
|
-
def self.respond_to_missing?(_, _)
|
79
|
-
true
|
80
|
-
end
|
81
|
-
|
82
|
-
def respond_to_missing?(_, _)
|
83
|
-
true
|
84
|
-
end
|
85
|
-
|
86
|
-
protected
|
87
|
-
|
88
|
-
def client
|
89
|
-
@client ||= ::Prest::Client.new(base_uri, options)
|
90
|
-
end
|
91
|
-
|
92
|
-
def base_uri
|
93
|
-
::Kernel.raise(Error, 'Implement in subclass')
|
94
|
-
end
|
95
|
-
|
96
|
-
def options
|
97
|
-
{}
|
98
|
-
end
|
99
|
-
end
|
100
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
|