diff --git a/lib/convertkit_v4/client/custom_fields.rb b/lib/convertkit_v4/client/custom_fields.rb index f7a5498..84ca5ac 100644 --- a/lib/convertkit_v4/client/custom_fields.rb +++ b/lib/convertkit_v4/client/custom_fields.rb @@ -6,9 +6,11 @@ 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 @@ -16,9 +18,11 @@ 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 diff --git a/lib/convertkit_v4/client/subscribers.rb b/lib/convertkit_v4/client/subscribers.rb index 64f66db..e96dbe1 100644 --- a/lib/convertkit_v4/client/subscribers.rb +++ b/lib/convertkit_v4/client/subscribers.rb @@ -1,6 +1,7 @@ module ConvertkitV4 class Client module Subscribers + def subscribers(options = {}) connection.get("subscribers", options) end @@ -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 diff --git a/lib/convertkit_v4/client/tags.rb b/lib/convertkit_v4/client/tags.rb index 4bb322b..c797647 100644 --- a/lib/convertkit_v4/client/tags.rb +++ b/lib/convertkit_v4/client/tags.rb @@ -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 diff --git a/lib/convertkit_v4/client/webhooks.rb b/lib/convertkit_v4/client/webhooks.rb index 09a718d..4ebea41 100644 --- a/lib/convertkit_v4/client/webhooks.rb +++ b/lib/convertkit_v4/client/webhooks.rb @@ -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 diff --git a/lib/convertkit_v4/version.rb b/lib/convertkit_v4/version.rb index ce4adf1..4e29082 100644 --- a/lib/convertkit_v4/version.rb +++ b/lib/convertkit_v4/version.rb @@ -1,3 +1,3 @@ module ConvertkitV4 - VERSION = "1.0.0" -end \ No newline at end of file + VERSION = "1.0.5" +end