Sha256: 862330efbcfad97cc403f11a31c2286b4b4477ee93e24398ff5dfff85699f1e3

Contents?: true

Size: 949 Bytes

Versions: 2

Compression:

Stored size: 949 Bytes

Contents

module CrystalApi
  class WebhookRegistration
    attr_reader :endpoint

    def initialize(endpoint)
      @endpoint = endpoint
    end

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

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

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

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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
crystal_api-0.1.0 lib/crystal_api/webhook_registration.rb
crystal_api-0.0.1 lib/crystal_api/webhook_registration.rb