service_client 0.1.1 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6b08c3ae1cd289404a362c7a57e3caafa43b0c5cb59cb3af1baeb9391dc8c59b
4
- data.tar.gz: 6b3a8b21aba11b9cf2213f88a8f16c8df35bac58611791dde035738195a26b72
3
+ metadata.gz: ef70f71d1ff5daf899d92ef3e56f0b342ab08265725f01dee7b56653a4f3686b
4
+ data.tar.gz: d91e2c02bae3a8252dc030aaaacf8f4699f63a1174c5e0addba893f1e1e4f375
5
5
  SHA512:
6
- metadata.gz: bc58261c84dd5f89c3f88ba70d92cd74ddd6223effbc3784d5a436f0303e5d17000fc038d224266561e0adde28eac729e7f53d4888b393b6fb70f52b4bf0aae9
7
- data.tar.gz: 85006dfbf928d9b63e34fc456bfa33a3556e9706cd4d9fc932eed424e4216846936b32bcddcd37422d5405e6e75e59a398afc9ec11c62f6db4b6caa5996836a2
6
+ metadata.gz: 644443a0c8e582ac6835aaf8a0842e7963ca751ea11f34c5e1908088b5dc46ec5086d6e2a960c4214454a104422e9a9a7277ef229e3a1baadd6f25d01c51f404
7
+ data.tar.gz: 8948d6757a730b924896a666fac5dedb4152a80b7d65f49ddbbeb25137ccfa1a1b722fc412e2925d33f8792346b0c59438b4b853ddb30469acf2e957b6eff6f1
data/Gemfile.lock CHANGED
@@ -1,13 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- service_client (0.1.0)
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
- $ bundle install
15
+ ```bash
16
+ bundle install
17
+ ```
18
18
 
19
19
  Or install it yourself as:
20
20
 
21
- $ gem install service_client
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 Request < ServiceClient::Call
29
- def customer(id)
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 customers
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
- # Methods of class
55
+ ## Methods of class
54
56
 
55
57
  ```ruby
56
58
  # GET
57
- ServiceClient::Call.get(url, headers:)
59
+ ServiceClient::Base.get(url, headers:)
58
60
 
59
61
  # POST
60
- ServiceClient::Call.post(url, headers:, body:)
62
+ ServiceClient::Base.post(url, headers:, body:)
61
63
 
62
64
  # PUT
63
- ServiceClient::Call.put(url, headers:, body:)
65
+ ServiceClient::Base.put(url, headers:, body:)
64
66
 
65
67
  # DELETE
66
- ServiceClient::Call.delete(url, headers:, body:)
68
+ ServiceClient::Base.delete(url, headers:, body:)
67
69
  ```
68
70
 
69
- # Classes of errors
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/[USERNAME]/service_client.
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,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'httparty'
4
+ require_relative 'response'
5
+
6
+ module ServiceClient
7
+ # ParamsRequired error
8
+ class ParamsRequired < StandardError; end
9
+
10
+ # Base class
11
+ class Base
12
+ class << self
13
+ def base_url(url = nil)
14
+ @base_url = url
15
+ end
16
+
17
+ def default_headers(headers = nil)
18
+ @default_headers = headers
19
+ end
20
+
21
+ def post(url = nil, headers: nil, body: nil)
22
+ raise_params_required(url: url, headers: headers, body: body)
23
+
24
+ request = HTTParty.post(build_url(url), headers: build_headers(headers), body: body)
25
+
26
+ make_response(request)
27
+ end
28
+
29
+ def get(url = nil, headers: nil)
30
+ raise_params_required(url: url)
31
+
32
+ request = HTTParty.get(build_url(url), headers: build_headers(headers))
33
+
34
+ make_response(request)
35
+ end
36
+
37
+ def put(url = nil, headers: nil, body: nil)
38
+ raise_params_required(url: url, body: body)
39
+
40
+ request = HTTParty.put(build_url(url), headers: build_headers(headers), body: body)
41
+
42
+ make_response(request)
43
+ end
44
+
45
+ def delete(url = nil, headers: nil)
46
+ raise_params_required(url: url)
47
+
48
+ request = HTTParty.delete(build_url(url), headers: build_headers(headers))
49
+
50
+ make_response(request)
51
+ end
52
+
53
+ private
54
+
55
+ def make_response(response)
56
+ Response.call(response)
57
+ end
58
+
59
+ def raise_params_required(params)
60
+ raise ParamsRequired, "Params: #{nil_params(params)} are required" if params_nil?(params)
61
+ end
62
+
63
+ def nil_params(params)
64
+ params.select { |_, value| value.nil? }.keys
65
+ end
66
+
67
+ def params_nil?(params)
68
+ params.values.any?(&:nil?)
69
+ end
70
+
71
+ def build_url(path)
72
+ base_url = instance_variable_get('@base_url')
73
+
74
+ return if base_url.nil? && path.nil?
75
+
76
+ [base_url, path].compact.join('/')
77
+ end
78
+
79
+ def build_headers(headers)
80
+ default_headers = instance_variable_get('@default_headers')
81
+ return default_headers if headers.nil?
82
+
83
+ return headers if default_headers.nil?
84
+
85
+ default_headers.merge(headers)
86
+ end
87
+ end
88
+ end
89
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ServiceClient
2
- VERSION = "0.1.1"
4
+ VERSION = '0.1.3'
3
5
  end
@@ -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.1
4
+ version: 0.1.3
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-12 00:00:00.000000000 Z
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