Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,47 @@ dynamic_template.delete

```

### Manage Webhooks
Can manage(create, update, find and delete) the Webhook. by using the following commands.

```ruby
require 'Paubox'
require 'json'

#These two arguments are necessary
target_url = "Target url"
events = "Event name"

# For create the new Webhook
Paubox::Webhook.create(target_url: 'https://webhook.site/77' signing_key: '11', api_key: '23', active: 'true', events: ["api_mail"])
=> { "RestClient::Response"=>"201",
"data" => {"id"=>1, "target_url"=>"https://webhook.site/39", "events"=>["api_mail_log_delivered"], "active"=>true, "signing_key"=>"test-1", "api_key"=>"1221", "api_customer_id"=>11 }
"message"=>"Webhook created!" }

# For getting the list of all Webhook of your organization
Paubox::Webhook.list
=>[ {"id"=>1, "target_url"=>"https://webhook.site/39", "events"=>["api_mail_log_delivered"], "active"=>true, "signing_key"=>"test-1",
"api_key"=>"1221",
"api_customer_id"=>11},
{"id"=>2, "target_url"=>"https://webhook.site/3984", "events"=>["api_mail_log_opened"], "active"=>true, "signing_key"=>"test-3",
"api_key"=>"1231",
"api_customer_id"=>11}]


# For update the existing Webhook
webhook = Paubox::Webhook.find(id)
webhook.update(target_url: 'https://webhook.site/377' signing_key: 'test', api_key: '32', active: 'true', events: ["api_mail_log_delivered"])
=> { "RestClient::Response"=>"200",
"data" => {"id"=>1, "target_url"=>"https://webhook.site/39", "events"=>["api_mail_log_delivered"], "active"=>true, "signing_key"=>"test-2", "api_key"=>"1241", "api_customer_id"=>11 }
"message"=>"Webhook updated!" }

# For delete the existing Webhook
webhook = Paubox::Webhook.find(template_id)
webhook.delete
=> {"RestClient::Response"=>"200", "message"=>"Webhook deleted!"}
```


### Send Messages using Dynamic Templates
Using above[dynamic templates](https://docs.paubox.com/docs/paubox_email_api/dynamic_templates/) is similar to sending a regular message. Just create a `Paubox::TemplatedMessage` object and pass a `template` object with the name of the template and variables:

Expand Down
1 change: 1 addition & 0 deletions lib/paubox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'paubox/message'
require 'paubox/templated_message'
require 'paubox/email_disposition'
require 'paubox/webhook'
require 'mail/paubox'

module Paubox
Expand Down
2 changes: 1 addition & 1 deletion lib/paubox/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Paubox
VERSION = '0.3.2'
VERSION = '0.3.3'
end
63 changes: 63 additions & 0 deletions lib/paubox/webhook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

module Paubox
class Webhook

attr_accessor :id, :target_url, :signing_key, :api_key, :active, :events

def initialize(args = {})
@id = args['id']
@target_url = args['target_url']
@signing_key = args['signing_key']
@api_key = args['api_key']
@active = args['active']
@events = args['events']
end

def update(target_url: nil, signing_key: nil, api_key: nil, active: nil, events: nil)
path = "webhook_endpoints/#{id}"
payload = {
target_url: target_url,
signing_key: signing_key,
api_key: api_key,
active: active,
events: events
}

Paubox::Client.new.send_request(method: :patch, payload: payload, path: path)
end

def delete
path = "webhook_endpoints/#{id}"
Paubox::Client.new.send_request(method: :delete, path: path)
end

class << self
def create(target_url: nil, signing_key: nil, api_key: nil, active: nil, events: nil)
path = "webhook_endpoints"
payload = {
target_url: target_url,
signing_key: signing_key,
api_key: api_key,
active: active,
events: events
}

Paubox::Client.new.send_request(method: :post, payload: payload, path: path)
end

def find(id)
path = "webhook_endpoints/#{id}"
response = Paubox::Client.new.send_request(method: :get, path: path)
data = JSON.parse(response.body)
Paubox::Webhook.new(data)
end

def list
path = "webhook_endpoints"
response = Paubox::Client.new.send_request(method: :get, path: path)
response
end
end
end
end
46 changes: 46 additions & 0 deletions spec/paubox/webhook_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Paubox::Webhook do
before do
Paubox.configure do |config|
config.api_key = 'test_key'
config.api_user = 'test_user'
end
end

describe '#create' do
it 'checks the webhook created' do
stub_request(:post, "https://api.paubox.net/v1/test_user/webhook_endpoints").to_return(status: 201)
response = Paubox::Webhook.create(target_url: 'https://webhook.site/39848377', signing_key: 'test', api_key: 'api_key', active: 'true', events: ["api_mail_log_delivered"])
expect(response.code).to eq 201
end
end


describe '#update' do
it 'checks the webhook updated' do
stub_request(:patch, "https://api.paubox.net/v1/test_user/webhook_endpoints/")
response = Paubox::Webhook.new.update(target_url: 'https://webhook.site/39848377', signing_key: 'test', api_key: 'api_key', active: 'false', events: ["api_mail_log_delivered"])
expect(response.code).to eq 200
end
end


describe '#delete' do
it 'checks the webhook deleted' do
stub_request(:delete, "https://api.paubox.net/v1/test_user/webhook_endpoints/")
response = Paubox::Webhook.new.delete
expect(response.code).to eq 200
end
end

describe '#list' do
it 'checks the webhook lists' do
stub_request(:get, "https://api.paubox.net/v1/test_user/webhook_endpoints")
response = Paubox::Webhook.list
expect(response.code).to eq 200
end
end
end