1
+ # frozen_string_literal: true
2
+
1
3
module Knock
2
4
# A Net::HTTP based API client for interacting with the Knock API
3
5
module Client
@@ -18,9 +20,7 @@ def execute_request(request:)
18
20
http_status = response . code . to_i
19
21
handle_error_response ( response : response ) if http_status >= 400
20
22
21
- if response . body and response . body != "" do
22
- JSON . parse ( response . body )
23
- end
23
+ JSON . parse ( response . body ) if response . body && ( response . body != '' )
24
24
end
25
25
26
26
def get_request ( path :, auth : false , params : { } , access_token : nil )
@@ -29,19 +29,19 @@ def get_request(path:, auth: false, params: {}, access_token: nil)
29
29
30
30
request = Net ::HTTP ::Get . new (
31
31
uri . to_s ,
32
- " Content-Type" => " application/json"
32
+ ' Content-Type' => ' application/json'
33
33
)
34
34
35
- request [ " Authorization" ] = "Bearer #{ access_token || Knock . key! } " if auth
36
- request [ " User-Agent" ] = user_agent
35
+ request [ ' Authorization' ] = "Bearer #{ access_token || Knock . key! } " if auth
36
+ request [ ' User-Agent' ] = user_agent
37
37
request
38
38
end
39
39
40
- def post_request ( path :, auth : false , idempotency_key : nil , body : nil )
41
- request = Net ::HTTP ::Post . new ( path , " Content-Type" => " application/json" )
40
+ def post_request ( path :, auth : false , _idempotency_key : nil , body : nil )
41
+ request = Net ::HTTP ::Post . new ( path , ' Content-Type' => ' application/json' )
42
42
request . body = body . to_json if body
43
- request [ " Authorization" ] = "Bearer #{ Knock . key! } " if auth
44
- request [ " User-Agent" ] = user_agent
43
+ request [ ' Authorization' ] = "Bearer #{ Knock . key! } " if auth
44
+ request [ ' User-Agent' ] = user_agent
45
45
request
46
46
end
47
47
@@ -51,68 +51,72 @@ def delete_request(path:, auth: false, params: {})
51
51
52
52
request = Net ::HTTP ::Delete . new (
53
53
uri . to_s ,
54
- " Content-Type" => " application/json"
54
+ ' Content-Type' => ' application/json'
55
55
)
56
56
57
- request [ " Authorization" ] = "Bearer #{ Knock . key! } " if auth
58
- request [ " User-Agent" ] = user_agent
57
+ request [ ' Authorization' ] = "Bearer #{ Knock . key! } " if auth
58
+ request [ ' User-Agent' ] = user_agent
59
59
request
60
60
end
61
61
62
- def put_request ( path :, auth : false , idempotency_key : nil , body : nil )
63
- request = Net ::HTTP ::Put . new ( path , " Content-Type" => " application/json" )
62
+ def put_request ( path :, auth : false , _idempotency_key : nil , body : nil )
63
+ request = Net ::HTTP ::Put . new ( path , ' Content-Type' => ' application/json' )
64
64
request . body = body . to_json if body
65
- request [ " Authorization" ] = "Bearer #{ Knock . key! } " if auth
66
- request [ " User-Agent" ] = user_agent
65
+ request [ ' Authorization' ] = "Bearer #{ Knock . key! } " if auth
66
+ request [ ' User-Agent' ] = user_agent
67
67
request
68
68
end
69
69
70
70
def user_agent
71
71
"Knock Ruby - v#{ Knock ::VERSION } "
72
72
end
73
73
74
+ # rubocop:disable Metrics/AbcSize
75
+
74
76
def handle_error_response ( response :)
75
77
http_status = response . code . to_i
76
78
json = JSON . parse ( response . body )
77
79
78
80
case http_status
79
81
when 400
80
82
raise InvalidRequestError . new (
81
- message : json [ " message" ] ,
83
+ message : json [ ' message' ] ,
82
84
http_status : http_status ,
83
- request_id : response [ " x-request-id" ]
85
+ request_id : response [ ' x-request-id' ]
84
86
)
85
87
when 401
86
88
raise AuthenticationError . new (
87
- message : json [ " message" ] ,
89
+ message : json [ ' message' ] ,
88
90
http_status : http_status ,
89
- request_id : response [ " x-request-id" ]
91
+ request_id : response [ ' x-request-id' ]
90
92
)
91
93
when 404
92
94
raise APIError . new (
93
- message : json [ " message" ] ,
95
+ message : json [ ' message' ] ,
94
96
http_status : http_status ,
95
- request_id : response [ " x-request-id" ]
97
+ request_id : response [ ' x-request-id' ]
96
98
)
97
99
when 422
98
- message = json [ " message" ]
99
- errors = extract_error ( json [ " errors" ] ) if json [ " errors" ]
100
+ message = json [ ' message' ]
101
+ errors = extract_error ( json [ ' errors' ] ) if json [ ' errors' ]
100
102
message += " (#{ errors } )" if errors
101
103
102
104
raise InvalidRequestError . new (
103
105
message : message ,
104
106
http_status : http_status ,
105
- request_id : response [ " x-request-id" ]
107
+ request_id : response [ ' x-request-id' ]
106
108
)
107
109
end
108
110
end
109
111
112
+ # rubocop:enable Metrics/AbcSize
113
+
110
114
private
111
115
112
116
def extract_error ( errors )
113
117
errors . map do |error |
114
- "#{ error [ " field" ] } : #{ error [ " message" ] } (#{ error [ " type" ] } )"
115
- end . join ( "; " )
118
+ "#{ error [ ' field' ] } : #{ error [ ' message' ] } (#{ error [ ' type' ] } )"
119
+ end . join ( '; ' )
116
120
end
117
121
end
118
122
end
0 commit comments