From 5d8356f0d5fb65a2eb7b18149d3351a635282052 Mon Sep 17 00:00:00 2001 From: Yoshi Gillaspie Date: Tue, 18 Apr 2017 12:39:05 -0700 Subject: [PATCH] Format Params as they're added to the resource For all values other than email and phone, the required format for the keys is very strange. I thought that this could be one way to improve this, or at least put more of this weirdness under the hood. Happy to update or get shot down if this isn't going to work, just an idea. --- README.md | 2 +- lib/active_campaign/contact.rb | 4 ---- lib/active_campaign/resource.rb | 10 +++++++++- test/active_campaign/resource_test.rb | 11 ++++++++++- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1f283dc..f34994f 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ would do this: ```ruby client = ActiveCampaign.client -contact = ActiveCampaign::Contact.new(email: "hi@example.com") +contact = ActiveCampaign::Contact.new(params: {email: "hi@example.com", first_name: "Tester"}) client.create!(contact) ``` diff --git a/lib/active_campaign/contact.rb b/lib/active_campaign/contact.rb index 946e7a0..33e1f4e 100644 --- a/lib/active_campaign/contact.rb +++ b/lib/active_campaign/contact.rb @@ -2,10 +2,6 @@ module ActiveCampaign class Contact < Resource - def initialize(email:, **params) - @params = params.merge(email: email) - end - action find: :contact_view_email action create: :contact_add action update: :contact_edit diff --git a/lib/active_campaign/resource.rb b/lib/active_campaign/resource.rb index 8b9957d..d501a8f 100644 --- a/lib/active_campaign/resource.rb +++ b/lib/active_campaign/resource.rb @@ -1,7 +1,15 @@ module ActiveCampaign class Resource def initialize(**params) - @params = params + @params = format_params(params) + end + + def format_params(params) + raise "no email attribute included in params" if params[:email].nil? + + params.map do |k, v| + [:email, :phone].include?(k) ? [k, v] : ["%#{k.upcase}%, 0".to_sym, v] + end.to_h end attr_reader :params diff --git a/test/active_campaign/resource_test.rb b/test/active_campaign/resource_test.rb index faa3785..3206659 100644 --- a/test/active_campaign/resource_test.rb +++ b/test/active_campaign/resource_test.rb @@ -11,7 +11,7 @@ class ActiveCampaign::ResourceTest < Minitest::Test action action_name => action_mapping end end - let(:params) { {} } + let(:params) { { email: "testing@email.com" } } subject { resource.new(params) } describe "actions" do @@ -28,5 +28,14 @@ class ActiveCampaign::ResourceTest < Minitest::Test end end end + + describe "when no email param" do + let(:params) { {} } + it "raises an error" do + assert_raises RuntimeError do + subject.action(action_name) + end + end + end end end