Skip to content
Merged
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
12 changes: 8 additions & 4 deletions lib/convertkit_v4/client/custom_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ def custom_fields
connection.get("custom_fields").body["custom_fields"]
end

def add_custom_field(options = {})
def add_custom_field(label)
connection.post("custom_fields") do |f|
f.params['label'] = options[:label]
f.body = JSON.generate({
label: label
})
end
end

def delete_custom_field(custom_field_id)
connection.delete("custom_fields/#{custom_field_id}")
end

def update_custom_field(custom_field_id, options = {})
def update_custom_field(custom_field_id, label)
connection.put("custom_fields/#{custom_field_id}") do |f|
f.params['label'] = options[:label]
f.body = JSON.generate({
label: label
})
end
end

Expand Down
36 changes: 26 additions & 10 deletions lib/convertkit_v4/client/subscribers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ConvertkitV4
class Client
module Subscribers

def subscribers(options = {})
connection.get("subscribers", options)
end
Expand All @@ -9,27 +10,42 @@ def subscriber(subscriber_id)
connection.get("subscribers/#{subscriber_id}")
end

def subscriber_tags(subscriber_id)
connection.get("subscribers/#{subscriber_id}/tags")
def add_subscriber(email_address, options = {})
response = connection.post("subscribers") do |f|
body = {
email_address: email_address
}

body[:fields] = options[:fields] if options[:fields]
body[:first_name] = options[:first_name] if options[:first_name]

f.body = JSON.generate(body)
end
response.body
end

def update_subscriber(subscriber_id, options = {})
response = connection.put("subscribers/#{subscriber_id}") do |f|
f.params["email_address"] = options[:email_address] if options[:email_address]
f.params["fields"] = options[:fields] if options[:fields]
f.params["first_name"] = options[:first_name] if options[:first_name]
body = {}
body[:email_address] = options[:email_address] if options[:email_address]
body[:fields] = options[:fields] if options[:fields]
body[:first_name] = options[:first_name] if options[:first_name]

f.body = JSON.generate(body)
end
response.body
end

def unsubscribe(email)
connection.put("unsubscribe") do |f|
f.params['email'] = email
def unsubscribe(subscriber_id)
connection.post("unsubscribe") do |f|
f.body = JSON.generate({
id: subscriber_id
})
end
end

def remove_tag_from_subscriber(subscriber_id, tag_id)
connection.delete("subscribers/#{subscriber_id}/tags/#{tag_id}")
def subscriber_tags(subscriber_id)
connection.get("subscribers/#{subscriber_id}/tags")
end
end
end
Expand Down
43 changes: 11 additions & 32 deletions lib/convertkit_v4/client/tags.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,29 @@
module ConvertkitV4
class Client
module Tags

def tags
connection.get("tags")
end

def add_subscriber_to_tag(tag_id, email, options = {})
connection.post("tags/#{tag_id}/subscribe") do |f|
f.params['email'] = email
f.params['first_name'] = options[:first_name]
f.params['fields'] = options[:fields]
f.params['tags'] = options[:tags]
end
end

def remove_tag_from_subscriber(tag_id, subscriber_id)
connection.delete("subscribers/#{subscriber_id}/tags/#{tag_id}")
end

def remove_tag_from_subscriber_by_email(tag_id, email)
connection.post("tags/#{tag_id}/unsubscribe") do |f|
f.params['email'] = email
end
end

def create_tag(tag_name)
response = connection.post("tags") do |request|
request.params["tag"] = { name: tag_name }
response = connection.post("tags") do |f|
f.body = JSON.generate({
name: tag_name
})
end
response.body
end

def create_tags(tag_names)
response = connection.post("tags") do |request|
request.params["tag"] = tag_names.map { |tag_name| { name: tag_name } }
def add_subscriber_to_tag(tag_id, options = {})
connection.post("tags/#{tag_id}/subscribers") do |f|
f.body = JSON.generate({
email: options[:email],
id: options[:id]
})
end
response.body
end

def subscriptions_to_tag(tag_id, options = {})
connection.get("tags/#{tag_id}/subscriptions", options) do |f|
f.params["page"] = options[:page] if options[:page]
f.params["sort_order"] = options[:sort_order] if options[:sort_order]
f.params["subscriber_state"] = options[:subscriber_state] if options[:subscriber_state]
end
end
end
end
end
16 changes: 8 additions & 8 deletions lib/convertkit_v4/client/webhooks.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
module ConvertkitV4
class Client
module Webhooks

def webhooks
connection.get("automations/hooks/").body
connection.get("webhooks").body
end

def create_webhook(url, events)
response = connection.post("automations/hooks") do |f|
f.params['target_url'] = url
f.params['event'] = events
def create_webhook(url, event)
response = connection.post("webhooks") do |f|
f.body = JSON.generate({
target_url: url,
event: event
})
end
response.body
end

def remove_webhook(rule_id)
connection.delete("automations/hooks/#{rule_id}").body
end
end
end
end
4 changes: 2 additions & 2 deletions lib/convertkit_v4/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ConvertkitV4
VERSION = "1.0.0"
end
VERSION = "1.0.5"
end