json_api_toolbox 0.13.0 → 0.14.0
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 -1
- data/CHANGELOG.md +7 -2
- data/lib/json_api_toolbox/version.rb +1 -1
- data/lib/service.rb +41 -25
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a3598f87e949855f9f2bc6fcb64caa44f9e170d
|
4
|
+
data.tar.gz: 428ca57f018b06a414c5289e6ac5e9c2d299411b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d431f1d4f666505381239fa826efdb911bb432ac21ab9853d0534dce4f3436d3f96467dd79b0c51ba0ddc16749681286861e1a40a6709029541c4d82a1db1ff
|
7
|
+
data.tar.gz: aac705c5d9689f2e58371e1ee9f36769abf72054985a16083ccd79fd6e8045bdbea560b3b27bb1deb7310270e922ed6b1660345bf4dcaefa2438a6ce084617c3
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.5.0
|
data/CHANGELOG.md
CHANGED
@@ -6,13 +6,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
-
## [0.
|
9
|
+
## [0.14.0] - 2018-05-02
|
10
10
|
### Changed
|
11
|
+
- Removing 'includes' attributes when calling without any 'includes' parameter
|
12
|
+
- Using payload as headers on GET's so its passed by query string
|
13
|
+
|
14
|
+
## [0.13.0] - 2018-05-02
|
15
|
+
### Added
|
11
16
|
- Added error details on exception renderizable
|
12
17
|
- Added exception log
|
13
18
|
|
14
19
|
## [0.12.1] - 2018-04-04
|
15
|
-
###
|
20
|
+
### Fixed
|
16
21
|
- Bug fix proxy at Service class
|
17
22
|
|
18
23
|
## [0.12.0] - 2018-04-03
|
data/lib/service.rb
CHANGED
@@ -6,46 +6,62 @@ require 'json-api-vanilla'
|
|
6
6
|
|
7
7
|
module JsonApiToolbox
|
8
8
|
class Service
|
9
|
+
attr_reader :method, :url, :body
|
10
|
+
|
11
|
+
def initialize(method, url, body)
|
12
|
+
@method = method
|
13
|
+
@url = url
|
14
|
+
@body = body
|
15
|
+
end
|
16
|
+
|
17
|
+
def execute
|
18
|
+
response = request
|
19
|
+
return if response.code == 404
|
20
|
+
Service.parse_response(response)
|
21
|
+
end
|
22
|
+
|
23
|
+
def request
|
24
|
+
RestClient::Request.execute(
|
25
|
+
method: method,
|
26
|
+
url: url,
|
27
|
+
payload: build_body,
|
28
|
+
headers: build_header
|
29
|
+
)
|
30
|
+
rescue RestClient::ExceptionWithResponse => e
|
31
|
+
e.response
|
32
|
+
end
|
33
|
+
|
34
|
+
def build_header
|
35
|
+
headers = { Authorization: RequestStore.store[:token] }
|
36
|
+
headers[:params] = body if method == :get
|
37
|
+
headers
|
38
|
+
end
|
39
|
+
|
40
|
+
def build_body
|
41
|
+
method == :post ? body : nil
|
42
|
+
end
|
43
|
+
|
9
44
|
class << self
|
10
45
|
def get(url: nil, includes: nil, query_string: nil)
|
11
|
-
body =
|
12
|
-
|
13
|
-
return if response.code == 404
|
14
|
-
parse_response(response)
|
46
|
+
body = build_query_string(includes, query_string)
|
47
|
+
new(:get, url, body).execute
|
15
48
|
end
|
16
49
|
|
17
50
|
def post(url: nil, body: nil)
|
18
|
-
|
19
|
-
parse_response(response)
|
51
|
+
new(:post, url, body).execute
|
20
52
|
end
|
21
53
|
|
22
|
-
private
|
23
|
-
|
24
54
|
def parse_response(response)
|
25
55
|
response_parsed = JSON::Api::Vanilla.parse(response.body)
|
26
56
|
response_parsed.data || response_parsed.errors
|
27
57
|
end
|
28
58
|
|
29
|
-
def
|
30
|
-
|
31
|
-
|
32
|
-
url: url,
|
33
|
-
payload: payload,
|
34
|
-
headers: build_header
|
35
|
-
)
|
36
|
-
rescue RestClient::ExceptionWithResponse => e
|
37
|
-
e.response
|
38
|
-
end
|
39
|
-
|
40
|
-
def build_body(includes, query_string)
|
41
|
-
params = { includes: includes }
|
59
|
+
def build_query_string(includes, query_string)
|
60
|
+
params = {}
|
61
|
+
params[:includes] = includes if includes
|
42
62
|
params.merge!(query_string) if query_string
|
43
63
|
params
|
44
64
|
end
|
45
|
-
|
46
|
-
def build_header
|
47
|
-
{ Authorization: RequestStore.store[:token] }
|
48
|
-
end
|
49
65
|
end
|
50
66
|
end
|
51
67
|
end
|