Permalink
Browse files

implement person/administrator/primary contact api methods

  • Loading branch information...
1 parent 9bce25c commit 57001449ccab6af0db54d303f2ad19805acc8ff2 Paul Duran committed Jun 8, 2012
View
@@ -3,3 +3,5 @@ rdoc
pkg
*.gem
example.rb
+/.buildpath
+/.project
View
@@ -13,6 +13,8 @@
require 'createsend/segment'
require 'createsend/subscriber'
require 'createsend/template'
+require 'createsend/person'
+require 'createsend/administrator'
module CreateSend
@@ -110,6 +112,23 @@ def timezones
response = CreateSend.get('/timezones.json')
response.parsed_response
end
+
+ # Gets the administrators
+ def administrators
+ response = CreateSend.get('/admins.json')
+ response.map{|item| Hashie::Mash.new(item)}
+ end
+
+ def get_primary_contact
+ response = CreateSend.get('/primarycontact.json')
+ Hashie::Mash.new(response)
+ end
+
+ def set_primary_contact(email)
+ options = { :query => { :email => email } }
+ response = CreateSend.put("/primarycontact.json", options)
+ Hashie::Mash.new(response)
+ end
def self.get(*args); handle_response super end
def self.post(*args); handle_response super end
@@ -0,0 +1,49 @@
+require 'createsend'
+require 'json'
+
+module CreateSend
+ # Represents an administrator and associated functionality.
+ class Administrator
+ attr_reader :email_address
+
+ def initialize(email_address)
+ @email_address = email_address
+ end
+
+ # Gets an adminsitrator by email address.
+ def self.get(email_address)
+ options = { :query => { :email => email_address } }
+ response = CreateSend.get "/admins.json", options
+ Hashie::Mash.new(response)
+ end
+
+ # Adds an adminstrator to the account
+ def self.add(email_address, name)
+ options = { :body => {
+ :EmailAddress => email_address,
+ :Name => name
+ }.to_json }
+ response = CreateSend.post "/admins.json", options
+ Hashie::Mash.new(response)
+ end
+
+ # Updates the administator details
+ def update(new_email_address, name)
+ options = {
+ :query => { :email => @email_address },
+ :body => {
+ :EmailAddress => new_email_address,
+ :Name => name
+ }.to_json }
+ CreateSend.put '/admins.json', options
+ # Update @email_address, so this object can continue to be used reliably
+ @email_address = new_email_address
+ end
+
+ # deletes this administrator from the account
+ def delete
+ options = { :query => { :email => @email_address } }
+ CreateSend.delete '/admins.json', options
+ end
+ end
+end
View
@@ -56,6 +56,23 @@ def segments
response = get 'segments'
response.map{|item| Hashie::Mash.new(item)}
end
+
+ # Gets the people associated with this client
+ def people
+ response = get "people"
+ response.map{|item| Hashie::Mash.new(item)}
+ end
+
+ def get_primary_contact
+ response = get "primarycontact"
+ Hashie::Mash.new(response)
+ end
+
+ def set_primary_contact(email)
+ options = { :query => { :email => email } }
+ response = put "primarycontact", options
+ Hashie::Mash.new(response)
+ end
# Gets this client's suppression list.
def suppressionlist(page=1, page_size=1000, order_field="email", order_direction="asc")
View
@@ -0,0 +1,58 @@
+require 'createsend'
+require 'json'
+
+module CreateSend
+ # Represents a person and associated functionality.
+ class Person
+ attr_reader :client_id
+ attr_reader :email_address
+
+ def initialize(client_id, email_address)
+ @client_id = client_id
+ @email_address = email_address
+ end
+
+ # Gets a person by client ID and email address.
+ def self.get(client_id, email_address)
+ options = { :query => { :email => email_address } }
+ response = CreateSend.get "/clients/#{client_id}/people.json", options
+ Hashie::Mash.new(response)
+ end
+
+ # Adds a person to the client. password is optional. if ommitted, an
+ # email invitation will be sent to the person
+ def self.add(client_id, email_address, name, access_level, password)
+ options = { :body => {
+ :EmailAddress => email_address,
+ :Name => name,
+ :AccessLevel => access_level,
+ :Password => password }.to_json }
+ response = CreateSend.post "/clients/#{client_id}/people.json", options
+ Hashie::Mash.new(response)
+ end
+
+ # Updates the person details. password is optional and will only be updated if supplied
+ def update(new_email_address, name, access_level, password)
+ options = {
+ :query => { :email => @email_address },
+ :body => {
+ :EmailAddress => new_email_address,
+ :Name => name,
+ :AccessLevel => access_level,
+ :Password => password }.to_json }
+ CreateSend.put uri_for(client_id), options
+ # Update @email_address, so this object can continue to be used reliably
+ @email_address = new_email_address
+ end
+
+ # deletes this person from the client
+ def delete
+ options = { :query => { :email => @email_address } }
+ CreateSend.delete uri_for(client_id), options
+ end
+
+ def uri_for(client_id)
+ "/clients/#{client_id}/people.json"
+ end
+ end
+end
View
@@ -0,0 +1,39 @@
+require File.dirname(__FILE__) + '/helper'
+
+class AdministratorTest < Test::Unit::TestCase
+ context "when an api caller is authenticated" do
+ setup do
+ @api_key = '123123123123123123123'
+ CreateSend.api_key @api_key
+ @admin = CreateSend::Administrator.new "admin@example.com"
+ end
+
+ should "get a administrator by email address" do
+ email = "admin@example.com"
+ stub_get(@api_key, "admins.json?email=#{CGI.escape(email)}", "admin_details.json")
+ admin = CreateSend::Administrator.get email
+ admin.EmailAddress.should == email
+ admin.Name.should == "Admin One"
+ admin.Status.should == "Active"
+ end
+
+ should "add an administrator" do
+ stub_post(@api_key, "admins.json", "add_admin.json")
+ result = CreateSend::Administrator.add "admin@example.com", "Admin"
+ result.EmailAddress.should == "admin@example.com"
+ end
+
+ should "update an administrator" do
+ email = "admin@example.com"
+ new_email = "new_email_address@example.com"
+ stub_put(@api_key, "admins.json?email=#{CGI.escape(email)}", nil)
+ @admin.update new_email, "Admin Name"
+ @admin.email_address.should == new_email
+ end
+
+ should "delete an admin" do
+ stub_delete(@api_key, "admins.json?email=#{CGI.escape(@admin.email_address)}", nil)
+ @admin.delete
+ end
+ end
+end
View
@@ -94,6 +94,16 @@ class ClientTest < Test::Unit::TestCase
res.Results.first.Date.should == "2010-10-26 10:55:31"
res.Results.first.State.should == "Suppressed"
end
+
+ should "get all people" do
+ stub_get(@api_key, "clients/#{@client.client_id}/people.json", "people.json")
+ people = @client.people
+ people.size.should == 2
+ people.first.EmailAddress.should == "person1@blackhole.com"
+ people.first.Name.should == "Person One"
+ people.first.Status.should == "Active"
+ people.first.AccessLevel.should == 31
+ end
should "get all templates" do
stub_get(@api_key, "clients/#{@client.client_id}/templates.json", "templates.json")
@@ -103,6 +113,19 @@ class ClientTest < Test::Unit::TestCase
templates.first.Name.should == 'Template One'
end
+ should "set primary contact" do
+ email = 'person@blackhole.com'
+ stub_put(@api_key, "clients/#{@client.client_id}/primarycontact.json?email=#{CGI.escape(email)}", 'client_set_primary_contact.json')
+ result = @client.set_primary_contact email
+ result.EmailAddress.should == email
+ end
+
+ should "get primary contact" do
+ stub_get(@api_key, "clients/#{@client.client_id}/primarycontact.json", 'client_get_primary_contact.json')
+ result = @client.get_primary_contact
+ result.EmailAddress.should == 'person@blackhole.com'
+ end
+
should "set basics" do
stub_put(@api_key, "clients/#{@client.client_id}/setbasics.json", nil)
@client.set_basics "Client Company Name", "Client Contact Name", "client@example.com", "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia"
View
@@ -54,6 +54,29 @@ class CreateSendTest < Test::Unit::TestCase
timezones.size.should == 97
assert timezones.include? "(GMT+12:00) Fiji"
end
+
+ should "get all administrators" do
+ stub_get(@api_key, "admins.json", "administrators.json")
+ administrators = @cs.administrators
+ administrators.size.should == 2
+ administrators.first.EmailAddress.should == "admin1@blackhole.com"
+ administrators.first.Name.should == 'Admin One'
+ administrators.first.Status.should == 'Active'
+ end
+
+ should "set primary contact" do
+ email = 'admin@blackhole.com'
+ stub_put(@api_key, "primarycontact.json?email=#{CGI.escape(email)}", 'admin_set_primary_contact.json')
+ result = @cs.set_primary_contact email
+ result.EmailAddress.should == email
+ end
+
+ should "get primary contact" do
+ stub_get(@api_key, "primarycontact.json", 'admin_get_primary_contact.json')
+ result = @cs.get_primary_contact
+ result.EmailAddress.should == 'admin@blackhole.com'
+ end
+
end
context "when the CreateSend API responds with an error" do
@@ -0,0 +1,3 @@
+{
+ "EmailAddress": "admin@example.com"
+}
@@ -0,0 +1,3 @@
+{
+ "EmailAddress": "person@example.com"
+}
@@ -0,0 +1,5 @@
+{
+ "EmailAddress": "admin@example.com",
+ "Name": "Admin One",
+ "Status": "Active"
+}
@@ -0,0 +1,3 @@
+{
+ "EmailAddress": "admin@blackhole.com"
+}
@@ -0,0 +1,3 @@
+{
+ "EmailAddress": "admin@blackhole.com"
+}
@@ -0,0 +1,12 @@
+[
+ {
+ "EmailAddress": "admin1@blackhole.com",
+ "Name": "Admin One",
+ "Status": "Active"
+ },
+ {
+ "EmailAddress": "admin2@blackhole.com",
+ "Name": "Admin Two",
+ "Status": "Active"
+ }
+]
@@ -0,0 +1,3 @@
+{
+ "EmailAddress": "person@blackhole.com"
+}
@@ -0,0 +1,3 @@
+{
+ "EmailAddress": "person@blackhole.com"
+}
View
@@ -0,0 +1,14 @@
+[
+ {
+ "EmailAddress": "person1@blackhole.com",
+ "Name": "Person One",
+ "Status": "Active",
+ "AccessLevel": 31
+ },
+ {
+ "EmailAddress": "person2@blackhole.com",
+ "Name": "Person Two",
+ "Status": "Active",
+ "AccessLevel": 0
+ }
+]
@@ -0,0 +1,6 @@
+{
+ "EmailAddress": "person@example.com",
+ "Name": "Person One",
+ "AccessLevel": 1023,
+ "Status": "Active"
+}
View
@@ -0,0 +1,41 @@
+require File.dirname(__FILE__) + '/helper'
+
+class PersonTest < Test::Unit::TestCase
+ context "when an api caller is authenticated" do
+ setup do
+ @api_key = '123123123123123123123'
+ CreateSend.api_key @api_key
+ @client_id = "d98h2938d9283d982u3d98u88"
+ @person = CreateSend::Person.new @client_id, "person@example.com"
+ end
+
+ should "get a person by client id and email address" do
+ email = "person@example.com"
+ stub_get(@api_key, "clients/#{@client_id}/people.json?email=#{CGI.escape(email)}", "person_details.json")
+ person = CreateSend::Person.get @client_id, email
+ person.EmailAddress.should == email
+ person.Name.should == "Person One"
+ person.AccessLevel.should == 1023
+ person.Status.should == "Active"
+ end
+
+ should "add a person" do
+ stub_post(@api_key, "clients/#{@client_id}/people.json", "add_person.json")
+ result = CreateSend::Person.add @client_id, "person@example.com", "Person", 0, "Password"
+ result.EmailAddress.should == "person@example.com"
+ end
+
+ should "update a person" do
+ email = "person@example.com"
+ new_email = "new_email_address@example.com"
+ stub_put(@api_key, "clients/#{@client_id}/people.json?email=#{CGI.escape(email)}", nil)
+ @person.update new_email, "Person", 1023, "NewPassword"
+ @person.email_address.should == new_email
+ end
+
+ should "delete a person" do
+ stub_delete(@api_key, "clients/#{@person.client_id}/people.json?email=#{CGI.escape(@person.email_address)}", nil)
+ @person.delete
+ end
+ end
+end

0 comments on commit 5700144

Please sign in to comment.