Class: Pinch::WebhookController
- Inherits:
-
Object
- Object
- Pinch::WebhookController
- Defined in:
- lib/pinch/controllers/webhook_controller.rb
Constant Summary collapse
- @@instance =
WebhookController.new
Class Method Summary collapse
-
.instance ⇒ Object
Singleton instance of the controller class.
Instance Method Summary collapse
-
#create(webhook) ⇒ Object
TODO: type endpoint description here.
-
#destroy(webhook_id) ⇒ Object
TODO: type endpoint description here.
-
#get(id) ⇒ Object
Get a specific webhook by its id.
-
#list ⇒ Object
List the webhooks of the current user.
-
#update(webhook_id, webhook = nil) ⇒ Object
TODO: type endpoint description here.
Class Method Details
.instance ⇒ Object
Singleton instance of the controller class
7 8 9 |
# File 'lib/pinch/controllers/webhook_controller.rb', line 7 def self.instance @@instance end |
Instance Method Details
#create(webhook) ⇒ Object
TODO: type endpoint description here
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/pinch/controllers/webhook_controller.rb', line 65 def create(webhook) # Validate required parameters if webhook == nil raise ArgumentError.new "Required parameter 'webhook' cannot be nil." end # the base uri for api requests _query_builder = Configuration.base_uri.dup # prepare query string for API call _query_builder << '/webhooks' # validate and preprocess url _query_url = APIHelper.clean_url _query_builder # prepare headers _headers = { 'user-agent' => 'APIMATIC 2.0', 'accept' => 'application/json', 'content-type' => 'application/json; charset=utf-8', 'X-API-TOKEN' => Configuration.x_api_token, 'X-API-EMAIL' => Configuration.x_api_email } # append custom auth authorization CustomAuthUtility.append_custom_auth_params _headers # invoke the API call request to fetch the response _response = Unirest.post _query_url, headers: _headers, parameters: webhook.to_json # Error handling using HTTP status codes if _response.code == 401 raise APIException.new 'Your API key is incorrect', 401, _response.body elsif _response.code == 400 raise APIException.new 'There is an error in the parameters you send', 400, _response.body elsif _response.code == 404 raise APIException.new 'Cannot find the resource specified', 404, _response.body elsif !_response.code.between?(200, 206) # [200,206] = HTTP OK raise APIException.new 'HTTP Response Not OK', _response.code, _response.body end # Try to cast response to desired type if _response.body.instance_of? Hash begin Webhook.from_hash(_response.body) rescue Exception raise APIException.new "Invalid JSON returned.", _response.code, _response.body end end end |
#destroy(webhook_id) ⇒ Object
TODO: type endpoint description here
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/pinch/controllers/webhook_controller.rb', line 182 def destroy(webhook_id) # Validate required parameters if webhook_id == nil raise ArgumentError.new "Required parameter 'webhook_id' cannot be nil." end # the base uri for api requests _query_builder = Configuration.base_uri.dup # prepare query string for API call _query_builder << '/webhooks/{webhook_id}' # process optional query parameters _query_builder = APIHelper.append_url_with_template_parameters _query_builder, { 'webhook_id' => webhook_id } # validate and preprocess url _query_url = APIHelper.clean_url _query_builder # prepare headers _headers = { 'user-agent' => 'APIMATIC 2.0', 'X-API-TOKEN' => Configuration.x_api_token, 'X-API-EMAIL' => Configuration.x_api_email } # append custom auth authorization CustomAuthUtility.append_custom_auth_params _headers # invoke the API call request to fetch the response _response = Unirest.delete _query_url, headers: _headers # Error handling using HTTP status codes if _response.code == 401 raise APIException.new 'Your API key is incorrect', 401, _response.body elsif _response.code == 400 raise APIException.new 'There is an error in the parameters you send', 400, _response.body elsif _response.code == 404 raise APIException.new 'Cannot find the resource specified', 404, _response.body elsif !_response.code.between?(200, 206) # [200,206] = HTTP OK raise APIException.new 'HTTP Response Not OK', _response.code, _response.body end # Return appropriate type _response.body end |
#get(id) ⇒ Object
Get a specific webhook by its id
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/pinch/controllers/webhook_controller.rb', line 234 def get(id) # Validate required parameters if id == nil raise ArgumentError.new "Required parameter 'id' cannot be nil." end # the base uri for api requests _query_builder = Configuration.base_uri.dup # prepare query string for API call _query_builder << '/webhooks/{id}' # process optional query parameters _query_builder = APIHelper.append_url_with_template_parameters _query_builder, { 'id' => id } # validate and preprocess url _query_url = APIHelper.clean_url _query_builder # prepare headers _headers = { 'user-agent' => 'APIMATIC 2.0', 'accept' => 'application/json', 'X-API-TOKEN' => Configuration.x_api_token, 'X-API-EMAIL' => Configuration.x_api_email } # append custom auth authorization CustomAuthUtility.append_custom_auth_params _headers # invoke the API call request to fetch the response _response = Unirest.get _query_url, headers: _headers # Error handling using HTTP status codes if _response.code == 401 raise APIException.new 'Your API key is incorrect', 401, _response.body elsif _response.code == 400 raise APIException.new 'There is an error in the parameters you send', 400, _response.body elsif _response.code == 404 raise APIException.new 'Cannot find the resource specified', 404, _response.body elsif !_response.code.between?(200, 206) # [200,206] = HTTP OK raise APIException.new 'HTTP Response Not OK', _response.code, _response.body end # Try to cast response to desired type if _response.body.instance_of? Hash begin Webhook.from_hash(_response.body) rescue Exception raise APIException.new "Invalid JSON returned.", _response.code, _response.body end end end |
#list ⇒ Object
List the webhooks of the current user
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pinch/controllers/webhook_controller.rb', line 13 def list # the base uri for api requests _query_builder = Configuration.base_uri.dup # prepare query string for API call _query_builder << '/webhooks' # validate and preprocess url _query_url = APIHelper.clean_url _query_builder # prepare headers _headers = { 'user-agent' => 'APIMATIC 2.0', 'accept' => 'application/json', 'X-API-TOKEN' => Configuration.x_api_token, 'X-API-EMAIL' => Configuration.x_api_email } # append custom auth authorization CustomAuthUtility.append_custom_auth_params _headers # invoke the API call request to fetch the response _response = Unirest.get _query_url, headers: _headers # Error handling using HTTP status codes if _response.code == 401 raise APIException.new 'Your API key is incorrect', 401, _response.body elsif _response.code == 400 raise APIException.new 'There is an error in the parameters you send', 400, _response.body elsif _response.code == 404 raise APIException.new 'Cannot find the resource specified', 404, _response.body elsif !_response.code.between?(200, 206) # [200,206] = HTTP OK raise APIException.new 'HTTP Response Not OK', _response.code, _response.body end # Try to cast response to list of desired type if _response.body.instance_of? Array value = Array.new _response.body.each do |item| begin value << (Webhook.from_hash(item)) rescue Exception raise APIException.new "Invalid JSON returned.", _response.code, _response.body end end value end end |
#update(webhook_id, webhook = nil) ⇒ Object
TODO: type endpoint description here
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/pinch/controllers/webhook_controller.rb', line 121 def update(webhook_id, webhook = nil) # Validate required parameters if webhook_id == nil raise ArgumentError.new "Required parameter 'webhook_id' cannot be nil." end # the base uri for api requests _query_builder = Configuration.base_uri.dup # prepare query string for API call _query_builder << '/webhooks/{webhook_id}' # process optional query parameters _query_builder = APIHelper.append_url_with_template_parameters _query_builder, { 'webhook_id' => webhook_id } # validate and preprocess url _query_url = APIHelper.clean_url _query_builder # prepare headers _headers = { 'user-agent' => 'APIMATIC 2.0', 'accept' => 'application/json', 'content-type' => 'application/json; charset=utf-8', 'X-API-TOKEN' => Configuration.x_api_token, 'X-API-EMAIL' => Configuration.x_api_email } # append custom auth authorization CustomAuthUtility.append_custom_auth_params _headers # invoke the API call request to fetch the response _response = Unirest.put _query_url, headers: _headers, parameters: webhook.to_json # Error handling using HTTP status codes if _response.code == 401 raise APIException.new 'Your API key is incorrect', 401, _response.body elsif _response.code == 400 raise APIException.new 'There is an error in the parameters you send', 400, _response.body elsif _response.code == 404 raise APIException.new 'Cannot find the resource specified', 404, _response.body elsif !_response.code.between?(200, 206) # [200,206] = HTTP OK raise APIException.new 'HTTP Response Not OK', _response.code, _response.body end # Try to cast response to desired type if _response.body.instance_of? Hash begin Webhook.from_hash(_response.body) rescue Exception raise APIException.new "Invalid JSON returned.", _response.code, _response.body end end end |