Class: Pinch::WebhookTypeController

Inherits:
Object
  • Object
show all
Defined in:
lib/pinch/controllers/webhook_type_controller.rb

Constant Summary collapse

@@instance =
WebhookTypeController.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject

Singleton instance of the controller class



7
8
9
# File 'lib/pinch/controllers/webhook_type_controller.rb', line 7

def self.instance
  @@instance
end

Instance Method Details

#listObject

List webhook types

Returns:

  • List of WebhookType response from the API call



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_type_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 << '/webhook_types'

  # 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 << (WebhookType.from_hash(item))
      rescue Exception
        raise APIException.new "Invalid JSON returned.", _response.code, _response.body
      end
    end
    value
  end
end