service_client 0.1.1 → 0.1.2
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/Gemfile.lock +1 -3
- data/README.md +17 -15
- data/lib/service_client/base.rb +65 -0
- data/lib/service_client/version.rb +3 -1
- data/lib/service_client.rb +3 -63
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 521a1eeca83e5e229c94fcd20c94e9173e30ef27036e961c1d5dcfc02294d7e1
|
4
|
+
data.tar.gz: 46b385eb2d4619a8eafe55af1379ec8753d3fd8e314cff577694e972ed5545ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39065c89a4675a343e87bf748eb910bd2d9b132655653e72e3e9459e8e834fc129d716b0cf5837d0c6a1f8c154304c289e060b02c3ac689d388fdd1cd42b06fc
|
7
|
+
data.tar.gz: 1811584d2b959c7db852620e711ce1946bff3434960681695113e8779adf946e48c81ee037f28928c2b468a7fc86bb6a1ce47dcf34fad711e9bc43c27f5a4823
|
data/Gemfile.lock
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
service_client (0.1.
|
4
|
+
service_client (0.1.2)
|
5
5
|
httparty (~> 0.18.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
byebug (11.1.3)
|
11
10
|
diff-lcs (1.5.0)
|
12
11
|
httparty (0.18.1)
|
13
12
|
mime-types (~> 3.0)
|
@@ -37,7 +36,6 @@ PLATFORMS
|
|
37
36
|
|
38
37
|
DEPENDENCIES
|
39
38
|
bundler (= 2.1.4)
|
40
|
-
byebug
|
41
39
|
httparty (~> 0.18.0)
|
42
40
|
rack (~> 2.2.3)
|
43
41
|
rake (~> 12.0)
|
data/README.md
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/service_client`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
4
|
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
7
5
|
## Installation
|
8
6
|
|
9
7
|
Add this line to your application's Gemfile:
|
@@ -14,19 +12,23 @@ gem 'service_client'
|
|
14
12
|
|
15
13
|
And then execute:
|
16
14
|
|
17
|
-
|
15
|
+
```bash
|
16
|
+
bundle install
|
17
|
+
```
|
18
18
|
|
19
19
|
Or install it yourself as:
|
20
20
|
|
21
|
-
|
21
|
+
```bash
|
22
|
+
gem install service_client
|
23
|
+
```
|
22
24
|
|
23
25
|
## Usage
|
24
26
|
|
25
27
|
Basic usage example:
|
26
28
|
|
27
29
|
```ruby
|
28
|
-
class
|
29
|
-
def
|
30
|
+
class CustomerClient < ServiceClient::Base
|
31
|
+
def find(id)
|
30
32
|
response = get("https://api.com/customers/#{id}")
|
31
33
|
|
32
34
|
response.data
|
@@ -41,7 +43,7 @@ class Request < ServiceClient::Call
|
|
41
43
|
# All errors of HTTP status code
|
42
44
|
end
|
43
45
|
|
44
|
-
def
|
46
|
+
def all
|
45
47
|
get(nil)
|
46
48
|
rescue ServiceClient::ParamsRequired => e
|
47
49
|
puts e.message
|
@@ -50,26 +52,26 @@ end
|
|
50
52
|
|
51
53
|
```
|
52
54
|
|
53
|
-
|
55
|
+
## Methods of class
|
54
56
|
|
55
57
|
```ruby
|
56
58
|
# GET
|
57
|
-
ServiceClient::
|
59
|
+
ServiceClient::Base.get(url, headers:)
|
58
60
|
|
59
61
|
# POST
|
60
|
-
ServiceClient::
|
62
|
+
ServiceClient::Base.post(url, headers:, body:)
|
61
63
|
|
62
64
|
# PUT
|
63
|
-
ServiceClient::
|
65
|
+
ServiceClient::Base.put(url, headers:, body:)
|
64
66
|
|
65
67
|
# DELETE
|
66
|
-
ServiceClient::
|
68
|
+
ServiceClient::Base.delete(url, headers:, body:)
|
67
69
|
```
|
68
70
|
|
69
|
-
|
71
|
+
## Classes of errors
|
70
72
|
|
71
73
|
```ruby
|
72
|
-
# 400 – Bad Request
|
74
|
+
# 400 – Bad Request
|
73
75
|
ServiceClient::Errors::BadRequestError
|
74
76
|
# 401 – Unauthorized
|
75
77
|
ServiceClient::Errors::UnauthorizedError
|
@@ -167,7 +169,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
167
169
|
|
168
170
|
## Contributing
|
169
171
|
|
170
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
172
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nemuba/service_client.
|
171
173
|
|
172
174
|
## License
|
173
175
|
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'httparty'
|
4
|
+
require_relative 'response'
|
5
|
+
|
6
|
+
module ServiceClient
|
7
|
+
# Base class
|
8
|
+
class ParamsRequired < StandardError; end
|
9
|
+
|
10
|
+
class Base
|
11
|
+
# ParamsRequired error
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def post(url = nil, headers: nil, body: nil)
|
15
|
+
raise_params_required(url: url, headers: headers, body: body)
|
16
|
+
|
17
|
+
request = HTTParty.post(url, headers: headers, body: body)
|
18
|
+
|
19
|
+
make_response(request)
|
20
|
+
end
|
21
|
+
|
22
|
+
def get(url = nil, headers: nil)
|
23
|
+
raise_params_required(url: url)
|
24
|
+
|
25
|
+
request = HTTParty.get(url, headers: headers)
|
26
|
+
|
27
|
+
make_response(request)
|
28
|
+
end
|
29
|
+
|
30
|
+
def put(url = nil, headers: nil, body: nil)
|
31
|
+
raise_params_required(url: url, body: body)
|
32
|
+
|
33
|
+
request = HTTParty.put(url, headers: headers, body: body)
|
34
|
+
|
35
|
+
make_response(request)
|
36
|
+
end
|
37
|
+
|
38
|
+
def delete(url = nil, headers: nil)
|
39
|
+
raise_params_required(url: url)
|
40
|
+
|
41
|
+
request = HTTParty.delete(url, headers: headers)
|
42
|
+
|
43
|
+
make_response(request)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def make_response(response)
|
49
|
+
Response.call(response)
|
50
|
+
end
|
51
|
+
|
52
|
+
def raise_params_required(params)
|
53
|
+
raise ParamsRequired, "Params: #{nil_params(params)} are required" if params_nil?(params)
|
54
|
+
end
|
55
|
+
|
56
|
+
def nil_params(params)
|
57
|
+
params.select { |_, value| value.nil? }.keys
|
58
|
+
end
|
59
|
+
|
60
|
+
def params_nil?(params)
|
61
|
+
params.values.any?(&:nil?)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/service_client.rb
CHANGED
@@ -1,67 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'httparty'
|
2
4
|
require_relative 'service_client/version'
|
3
5
|
require_relative 'service_client/response'
|
6
|
+
require_relative 'service_client/base'
|
4
7
|
require_relative 'service_client/errors'
|
5
|
-
require 'httparty'
|
6
|
-
require 'rack/utils'
|
7
|
-
|
8
|
-
# Service Client
|
9
|
-
module ServiceClient
|
10
|
-
# ParamsRequired error
|
11
|
-
class ParamsRequired < StandardError; end
|
12
|
-
|
13
|
-
# Call class
|
14
|
-
class Call
|
15
|
-
def self.post(url = nil, headers: nil, body: nil)
|
16
|
-
raise_params_required(url: url, headers: headers, body: body)
|
17
|
-
|
18
|
-
request = HTTParty.post(url, headers: headers, body: body)
|
19
|
-
|
20
|
-
make_response(request)
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.get(url = nil, headers: nil)
|
24
|
-
raise_params_required(url: url)
|
25
|
-
|
26
|
-
request = HTTParty.get(url, headers: headers)
|
27
|
-
|
28
|
-
make_response(request)
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.put(url = nil, headers: nil, body: nil)
|
32
|
-
raise_params_required(url: url, body: body)
|
33
|
-
|
34
|
-
request = HTTParty.put(url, headers: headers, body: body)
|
35
|
-
|
36
|
-
make_response(request)
|
37
|
-
end
|
38
|
-
|
39
|
-
def self.delete(url = nil, headers: nil)
|
40
|
-
raise_params_required(url: url)
|
41
|
-
|
42
|
-
request = HTTParty.delete(url, headers: headers)
|
43
|
-
|
44
|
-
make_response(request)
|
45
|
-
end
|
46
|
-
|
47
|
-
class << self
|
48
|
-
private
|
49
|
-
|
50
|
-
def make_response(response)
|
51
|
-
Response.call(response)
|
52
|
-
end
|
53
|
-
|
54
|
-
def raise_params_required(params)
|
55
|
-
raise ParamsRequired, "Params: #{nil_params(params)} are required" if params_nil?(params)
|
56
|
-
end
|
57
|
-
|
58
|
-
def nil_params(params)
|
59
|
-
params.select { |_, value| value.nil? }.keys
|
60
|
-
end
|
61
|
-
|
62
|
-
def params_nil?(params)
|
63
|
-
params.values.any?(&:nil?)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: service_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alef Ojeda de Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-02-
|
11
|
+
date: 2023-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -54,6 +54,7 @@ files:
|
|
54
54
|
- bin/console
|
55
55
|
- bin/setup
|
56
56
|
- lib/service_client.rb
|
57
|
+
- lib/service_client/base.rb
|
57
58
|
- lib/service_client/errors.rb
|
58
59
|
- lib/service_client/response.rb
|
59
60
|
- lib/service_client/version.rb
|