Class: Envoy::Hooks

Inherits:
Object
  • Object
show all
Defined in:
lib/envoy/hooks.rb,
lib/envoy/hooks/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.11.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jwtToken:, baseUrl:, adapter: Faraday.default_adapter, stubs: nil, timeout: 25) ⇒ Hooks

Returns a new instance of Hooks.



13
14
15
16
17
18
19
# File 'lib/envoy/hooks.rb', line 13

def initialize(jwtToken:, baseUrl:, adapter: Faraday.default_adapter, stubs: nil, timeout: 25)
  @jwtToken = jwtToken
  @baseUrl = baseUrl
  @adapter = adapter
  @stubs = stubs
  @timeout = timeout
end

Instance Attribute Details

#adapter ⇒ Object (readonly)

Returns the value of attribute adapter.



11
12
13
# File 'lib/envoy/hooks.rb', line 11

def adapter
  @adapter
end

#baseUrl ⇒ Object (readonly)

Returns the value of attribute baseUrl.



11
12
13
# File 'lib/envoy/hooks.rb', line 11

def baseUrl
  @baseUrl
end

#jwtToken ⇒ Object (readonly)

Returns the value of attribute jwtToken.



11
12
13
# File 'lib/envoy/hooks.rb', line 11

def jwtToken
  @jwtToken
end

Instance Method Details

#faraday ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/envoy/hooks.rb', line 21

def faraday
  @faraday ||= Faraday.new do |conn|
    conn.url_prefix = @baseUrl
    conn.request :authorization, 'Bearer', @jwtToken
    conn.request :json
    conn.response :json, content_type: 'application/json'
    conn.adapter @adapter, @stubs
    conn.options.timeout = @timeout
    conn.proxy ENV.fetch('http_proxy', nil)
  end
end

#subscribedUIHooks(locationId: nil, locationIds: [], triggerName: nil, triggerNames: [], includeHidden: false) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/envoy/hooks.rb', line 105

def subscribedUIHooks(locationId: nil, locationIds: [], triggerName: nil, triggerNames: [], includeHidden: false)
  faraday.get('/rest/v1/locations/hooks/ui', {
                locationId: locationId,
                locationIds: locationIds,
                triggerName: triggerName,
                triggerNames: triggerNames,
                includeHidden: includeHidden
              })
end

#subscribedZoneUIHooks(zoneIds: [], triggerNames: [], includeHidden: false) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/envoy/hooks.rb', line 115

def subscribedZoneUIHooks(zoneIds: [], triggerNames: [], includeHidden: false)
  faraday.get('/rest/v1/zones/hooks/ui', {
                zoneIds: zoneIds,
                triggerNames: triggerNames,
                includeHidden: includeHidden
              })
end

#syncClientConfig(clientId:, clientSecret:, clientName:, clientType:, clientIcon:, version:, isDisabled:, eventHooks: [], featureHooks: [], uiHooks: [], subscriptionEnabledInvocationURL: nil, subscriptionDisabledInvocationURL: nil, externalId: nil) ⇒ Object



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
# File 'lib/envoy/hooks.rb', line 136

def syncClientConfig(
  clientId:,
  clientSecret:,
  clientName:,
  clientType:,
  clientIcon:,
  version:,
  isDisabled:,
  eventHooks: [],
  featureHooks: [],
  uiHooks: [],
  subscriptionEnabledInvocationURL: nil,
  subscriptionDisabledInvocationURL: nil,
  externalId: nil
)
  validateEnum('clientType', clientType, %w[APP PRODUCT])
  faraday.post("/rest/v1/clients/#{clientId}/config", {
                 clientId: clientId,
                 clientSecret: clientSecret,
                 clientName: clientName,
                 clientType: clientType,
                 clientIcon: clientIcon,
                 version: version,
                 isDisabled: isDisabled,
                 eventHooks: eventHooks,
                 featureHooks: featureHooks,
                 uiHooks: uiHooks,
                 subscriptionEnabledInvocationURL: subscriptionEnabledInvocationURL,
                 subscriptionDisabledInvocationURL: subscriptionDisabledInvocationURL,
                 externalId: externalId
               })
end

#triggerEventHooks(locationId:, userEmail: nil, userEmails: [], triggerName: nil, triggerNames: [], resourceId: nil, eventHookIds: []) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/envoy/hooks.rb', line 123

def triggerEventHooks(locationId:, userEmail: nil, userEmails: [], triggerName: nil,
                      triggerNames: [], resourceId: nil, eventHookIds: [])
  faraday.post("/rest/v1/locations/#{locationId}/hooks/event/trigger", {
                 locationId: locationId,
                 userEmail: userEmail,
                 userEmails: userEmails,
                 triggerName: triggerName,
                 triggerNames: triggerNames,
                 resourceId: resourceId,
                 eventHookIds: eventHookIds
               })
end

#triggerUIHooks(locationId:, responseType:, triggerName: nil, triggerNames: [], resourceId: nil, accentColor: nil, uiHooks: [], previewMode: false, uiHookIds: [], resourceType: nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/envoy/hooks.rb', line 65

def triggerUIHooks(locationId:, responseType:, triggerName: nil, triggerNames: [], resourceId: nil,
                   accentColor: nil, uiHooks: [], previewMode: false, uiHookIds: [], resourceType: nil)

  validateEnum('responseType', responseType, %w[HTML JSON])

  validateEnum('resourceType', resourceType, %w[INVITE EMPLOYEE ENTRY]) unless resourceType.nil?

  faraday.post("/rest/v1/locations/#{locationId}/hooks/ui/trigger", {
                 locationId: locationId,
                 triggerName: triggerName,
                 triggerNames: triggerNames,
                 responseType: responseType,
                 resourceId: resourceId,
                 resourceType: resourceType,
                 accentColor: accentColor,
                 previewMode: previewMode,
                 uiHookIds: uiHookIds || uiHooks
               })
end

#triggerZoneUIHooks(responseType:, zoneIds:, triggerNames: [], resourceId: nil, resourceIds: [], resourceType: nil, accentColor: nil, previewMode: false, uiHookIds: []) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/envoy/hooks.rb', line 85

def triggerZoneUIHooks(responseType:, zoneIds:, triggerNames: [], resourceId: nil, resourceIds: [],
                       resourceType: nil, accentColor: nil, previewMode: false, uiHookIds: [])

  validateEnum('responseType', responseType, %w[HTML JSON])

  validateEnum('resourceType', resourceType, %w[INVITE EMPLOYEE ENTRY]) unless resourceType.nil?

  resource_ids = Array(resourceId || resourceIds)
  faraday.post('/rest/v1/zones/hooks/ui/trigger', {
                 zoneIds: zoneIds,
                 triggerNames: triggerNames,
                 responseType: responseType,
                 resourceIds: resource_ids,
                 resourceType: resourceType,
                 accentColor: accentColor,
                 previewMode: previewMode,
                 uiHookIds: uiHookIds
               })
end

#upsertSubscription(subscriptionScope:, clientId:, status:, config: {}, externalId: nil, creatorUserId: nil, authorizedScopes: []) ⇒ Object

Should we require all these, or can we get some from the environment?



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
61
62
63
# File 'lib/envoy/hooks.rb', line 34

def upsertSubscription(subscriptionScope:, clientId:, status:, config: {}, externalId: nil, creatorUserId: nil,
                       authorizedScopes: [])
  # NOTE: - should we be doing these checks in here, or let the API handle it?
  if !subscriptionScope || !subscriptionScope[:type]
    raise(Error, 'Must fully define a subscriptionScope for `upsertSubscription`')
  end

  # Validate that id is present for non-GLOBAL subscriptions
  if subscriptionScope[:type] != 'GLOBAL' && !subscriptionScope[:id]
    raise(Error, 'subscriptionScope.id is required for non-GLOBAL subscriptions')
  end

  # Validate that id is nil/null for GLOBAL subscriptions
  if subscriptionScope[:type] == 'GLOBAL' && subscriptionScope[:id]
    raise(Error, 'subscriptionScope.id must be nil for GLOBAL subscriptions')
  end

  validateEnum('subscriptionScope.type', subscriptionScope[:type], %w[LOCATION COMPANY USER ZONE GLOBAL])
  validateEnum('status', status, %w[ENABLED DISABLED PENDING])

  faraday.post('/rest/v1/subscriptions', {
                 subscriptionScope: subscriptionScope,
                 clientId: clientId,
                 status: status,
                 config: config,
                 externalId: externalId,
                 creatorUserId: creatorUserId,
                 authorizedScopes: authorizedScopes
               })
end

#validateEnum(optionName, testOption, validOptions) ⇒ Object

Raises:



169
170
171
172
173
174
# File 'lib/envoy/hooks.rb', line 169

def validateEnum(optionName, testOption, validOptions)
  return if validOptions.include?(testOption)

  raise(Error,
        "#{optionName} must be one of the allowed options: #{validOptions.join(', ')}")
end