Class: CrystalApi::WebhookRegistration

Inherits:
Object
  • Object
show all
Defined in:
lib/crystal_api/webhook_registration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ WebhookRegistration

Returns a new instance of WebhookRegistration.



5
6
7
# File 'lib/crystal_api/webhook_registration.rb', line 5

def initialize(endpoint)
  @endpoint = endpoint
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



3
4
5
# File 'lib/crystal_api/webhook_registration.rb', line 3

def endpoint
  @endpoint
end

Instance Method Details

#deregister(webhook_id) ⇒ Object



19
20
21
# File 'lib/crystal_api/webhook_registration.rb', line 19

def deregister(webhook_id)
  endpoint.delete("/webhooks/#{webhook_id}")
end

#register(webhook) ⇒ Object



9
10
11
# File 'lib/crystal_api/webhook_registration.rb', line 9

def register(webhook)
  endpoint.post('/webhooks', webhook.to_json(:except => [:id]))
end

#registered?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/crystal_api/webhook_registration.rb', line 13

def registered?(webhook)
  registered_webhooks.any? {|wh| wh.address == webhook.address &&
                                 wh.topic   == webhook.topic &&
                                 wh.resource_id == webhook.resource_id }
end

#registered_webhooksObject



23
24
25
# File 'lib/crystal_api/webhook_registration.rb', line 23

def registered_webhooks
  endpoint.get('/webhooks').parsed
end

#webhook_id(webhook) ⇒ Object



27
28
29
30
31
32
# File 'lib/crystal_api/webhook_registration.rb', line 27

def webhook_id(webhook)
  wh = registered_webhooks.detect {|wh| wh.address == webhook.address &&
                                   wh.topic   == webhook.topic &&
                                   wh.resource_id == webhook.resource_id }
  wh && wh.id
end