module Emites
module Resources
# A wrapper to Emites webhooks API
#
# [API]
# Documentation: http://myfreecomm.github.io/emites/sandbox/v1/modules/webhooks.html
#
# @example Listing all webhooks:
# client = Emites.client("MY_SECRET_TOKEN")
# client.webhooks.list
#
# @example Creating an webhook:
# client = Emites.client("MY_SECRET_TOKEN")
# client.webhooks.create({name: "My Fake Web Hook", url: "http://web.hook")
#
# @see Emites.client
class Webhook < Base
# Lists all Webhooks related to the account
#
# [API]
# Method: GET /api/v1/webhooks
#
# Documentation: http://myfreecomm.github.io/emites/sandbox/v1/modules/webhooks.html#listagem
#
# @return [Array] an array of Webhook
def list
http.get("/webhooks") do |response|
respond_with_collection(response)
end
end
# Updates an Webhook identified by id
#
# [API]
# Method: PUT /api/v1/webhooks/:id
#
# Documentation: http://myfreecomm.github.io/emites/sandbox/v1/modules/webhooks.html#atualizacao
#
# @param id [Integer] the webhook id
# @param params [Hash] a hash with new data
# @return [Array] an array of Webhook
def update(id, params)
http.put("/webhooks/#{id}", { body: params }) do |response|
respond_with_entity(response)
end
end
# Deletes an Webhook by it's id. Returns true if deletion performed well, otherwise,
# returns false.
#
# [API]
# Method: DELETE /api/v1/webhooks/:id
#
# Documentation: http://myfreecomm.github.io/emites/sandbox/v1/modules/webhooks.html#remocao
#
# @param id [Integer] the Wehook id
# @return [Boolean] whether deletion was performed or not
def destroy(id)
http.delete("/webhooks/#{id}") do |response|
response.code == 204
end
end
# Creates an Webhook
#
# [API]
# Method: POST /api/v1/webhooks
#
# Documentation: http://myfreecomm.github.io/emites/sandbox/v1/modules/webhooks.html#criacao
#
# @param params [Hash] a hash with Webhook attributes
# @return [Emites::Entities::Webhook] the created Webhook
def create(params)
http.post("/webhooks", { body: params }) do |response|
respond_with_entity(response)
end
end
notify :create, :destroy, :update
end
end
end