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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73b91ef1e5dadc1e481b783fe817242c60c340d9
4
- data.tar.gz: 6ab51aa214846f0d6100ba92529bce53abfcfa3a
3
+ metadata.gz: 1a3598f87e949855f9f2bc6fcb64caa44f9e170d
4
+ data.tar.gz: 428ca57f018b06a414c5289e6ac5e9c2d299411b
5
5
  SHA512:
6
- metadata.gz: 50155433bc66dd3e51a60c59e1210a7e84a83514c9cf2730c537ce00c394b4c08ba9846ec9ff194e8b9df1d224a8cac0c5c68a4ece3cae1507a55e4ddf98b838
7
- data.tar.gz: 0a9510a7c553e0ac321e160b1510e93c7d41e0ade07e710fe64f305d576496fb1788a097a31dfb152668c2a729620683112648552e9d1e8f65caec3cdda0ad21
6
+ metadata.gz: 0d431f1d4f666505381239fa826efdb911bb432ac21ab9853d0534dce4f3436d3f96467dd79b0c51ba0ddc16749681286861e1a40a6709029541c4d82a1db1ff
7
+ data.tar.gz: aac705c5d9689f2e58371e1ee9f36769abf72054985a16083ccd79fd6e8045bdbea560b3b27bb1deb7310270e922ed6b1660345bf4dcaefa2438a6ce084617c3
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.4.1
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.12.2] - 2018-05-02
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
- ### Changed
20
+ ### Fixed
16
21
  - Bug fix proxy at Service class
17
22
 
18
23
  ## [0.12.0] - 2018-04-03
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonApiToolbox
4
- VERSION = '0.13.0'
4
+ VERSION = '0.14.0'
5
5
  end
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 = build_body(includes, query_string)
12
- response = request(url, body, :get)
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
- response = request(url, body, :post)
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 request(url, payload, method)
30
- RestClient::Request.execute(
31
- method: method,
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adriano Bacha