Class: CampaignMonitor::Client

Inherits:
Base
  • Object
show all
Includes:
Helpers
Defined in:
lib/campaign_monitor/client.rb

Overview

The Client class aims to impliment the full functionality of the CampaignMonitor Clients API as detailed at: www.campaignmonitor.com/api/

Attributes

Attriutes can be read and set as if Client were a Hash

@client["CompanyName"]="Road Running, Inc."
@client["ContactName"] => "Wiley Coyote"

Convenience attribute readers are provided for name and id

@client.id == @client["ClientID"]
@client.name == @client["CompanyName"]

API calls supported

  • Client.Create

  • Client.Delete

  • Client.GetCampaigns

  • Client.GetDetail

  • Client.GetLists

  • Client.UpdateAccessAndBilling

  • Client.UpdateBasics

Not yet supported

  • Client.GetSegments - TODO

  • Client.GetSuppressionList - TODO

Constant Summary collapse

BASIC_ATTRIBUTES =

we will assume if something isn’t a basic attribute that it’s a AccessAndBilling attribute

%w{CompanyName ContactName EmailAddress Country Timezone}

Instance Attribute Summary

Attributes inherited from Base

#attributes, #cm_client, #result

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#formatted_timestamp, #handle_response, #timestamp_format

Methods inherited from Base

#[], #[]=, client, client=, #id, #id=, #name

Constructor Details

#initialize(attrs = {}) ⇒ Client

Creates a new client that you can later create (or load) The prefered way to load a client is using Client#[] however

Example

@client = Client.new(attributes)
@client.Create

@client = Client.new("ClientID" => 12345)
@client.GetDetails


61
62
63
64
# File 'lib/campaign_monitor/client.rb', line 61

def initialize(attrs={})
  super
  @attributes=attrs
end

Class Method Details

.[](id) ⇒ Object

Calls Client.GetDetails to load a specific client Client#result will have the result of the API call

Example

@client=Client[12345]
puts @client.name if @client.result.success?


109
110
111
112
113
# File 'lib/campaign_monitor/client.rb', line 109

def self.[](id)
  client=self.new("ClientID" => id)
  client.GetDetail(true)
  client.result.code == 102 ? nil : client
end

Instance Method Details

#CreateObject

Calls Client.Create It will return true if successful and false if not. Client#result will have the result of the API call

Example

@client=CampaignMonitor::Client.new
@client["CompanyName"]="Ben's Widgets"
@client["ContactName"]="Ben Winters"
@client["Country"]=@cm.countries.first
@client["Timezone"]=@cm.timezones.first
...
@client.Create


199
200
201
202
203
# File 'lib/campaign_monitor/client.rb', line 199

def Create
  @result=Result.new(cm_client.Client_Create(@attributes))
  self.id = @result.content if @result.success?
  @result.success?
end

#DeleteObject

Calls Client.Delete. It will return true if successful and false if not. Client#result will have the result of the API call

Example

@client=@cm.clients.first
@client.Delete


212
213
214
215
# File 'lib/campaign_monitor/client.rb', line 212

def Delete
  @result=Result.new(cm_client.Client_Delete("ClientID" => id))
  @result.success?
end

#GetCampaignsObject Also known as: campaigns

Calls Client.GetCampaigns and returns a collection of CM::List objects

Example

@client = @cm.clients.first
@campaigns = @client.campaigns

for campaign in @campaigns
  puts campaign.subject
end


91
92
93
# File 'lib/campaign_monitor/client.rb', line 91

def GetCampaigns
  cm_client.campaigns(self.id)
end

#GetDetail(overwrite = false) ⇒ Object

Calls Client.GetDetails This is needed because often if you’re working with a list of clients you really only have their company name when what you want is the full record. It will return true if successful and false if not. Client#result will have the result of the API call

Example

@client=@cm.clients.first
@client["CompanyName"]="Ben's Widgets"
@client["ContactName"] => nil
@client.GetDetail
@client["ContactName"] => "Ben Wilder"


128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/campaign_monitor/client.rb', line 128

def GetDetail(overwrite=false)
  @result=Result.new(cm_client.Client_GetDetail("ClientID" => id))
  return false if @result.failed?
  @flatten={}
  @flatten.merge!(@result.raw["BasicDetails"])
  @flatten.merge!(@result.raw["AccessAndBilling"])
  # TODO - look into
  # map {} to nil - some weird XML converstion issue?
  @flatten=@flatten.inject({}) { |sum,a| sum[a[0]]=a[1]=={} ? nil : a[1]; sum }
  @attributes=@flatten.merge(@attributes)
  @attributes.merge!(@flatten) if overwrite
  @fully_baked=true if @result.success?
  @result.success?
end

#GetListsObject Also known as: lists

Calls Client.GetLists and returns a collection of CM::Campaign objects

Example

@client = @cm.clients.first
@new_list = @client.lists.build
@lists = @client.lists

for list in @lists
  puts list.name # a shortcut for list["Title"]
end


76
77
78
# File 'lib/campaign_monitor/client.rb', line 76

def GetLists
  ClientLists.new(cm_client.lists(self.id), self)
end

#new_campaign(attrs = {}) ⇒ Object



97
98
99
# File 'lib/campaign_monitor/client.rb', line 97

def new_campaign(attrs={})
  Campaign.new(attrs.merge("ClientID" => self.id))
end

#updateObject

This is just a convenience method that calls both Client.UpdateBasics and Client.UpdateAccessAndBilling. It will return true if successful and false if not. Client#result will have the result of the API call

Example

@client=@cm.clients.first
@client["CompanyName"]="Ben's Widgets"
@client.update


151
152
153
154
155
# File 'lib/campaign_monitor/client.rb', line 151

def update
  self.UpdateBasics
  self.UpdateAccessAndBilling if result.success?
  @result.success?
end

#UpdateAccessAndBillingObject

Calls Client.UpdateAccessAndBilling This will also call GetDetails first to prepoluate any empty fields the API call needs It will return true if successful and false if not. Client#result will have the result of the API call

Example

@client=@cm.clients.first
@client["Currency"]="USD"
@client.UpdateAccessAndBilling


166
167
168
169
170
# File 'lib/campaign_monitor/client.rb', line 166

def UpdateAccessAndBilling
  fully_bake
  @result=Result.new(cm_client.Client_UpdateAccessAndBilling(@attributes))
  @result.success? 
end

#UpdateBasicsObject

Calls Client.UpdateBasics This will also call GetDetails first to prepoluate any empty fields the API call needs It will return true if successful and false if not. Client#result will have the result of the API call

Example

@client=@cm.clients.first
@client["CompanyName"]="Ben's Widgets"
@client.UpdateBasics


181
182
183
184
185
# File 'lib/campaign_monitor/client.rb', line 181

def UpdateBasics
  fully_bake
  @result=Result.new(cm_client.Client_UpdateBasics(@attributes))
  @result.success? 
end