Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for account and campaigns APIs #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions lib/pardot/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Client
include Authentication
include Http

include Objects::Accounts
include Objects::Campaigns
include Objects::Emails
include Objects::Lists
include Objects::ListMemberships
Expand Down
32 changes: 32 additions & 0 deletions lib/pardot/objects/accounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Pardot
module Objects
module Accounts
def accounts
@accounts ||= Accounts.new self
end

class Accounts

def initialize client
@client = client
end

def read params = {}
get "/do/read", params
end

protected

def get path, params = {}, result = "account"
response = @client.get "account", path, params
result ? response[result]: response
end

def post path, params = {}, result = "account"
response = @client.post "account", path, params
result ? response[result] : response
end
end
end
end
end
50 changes: 50 additions & 0 deletions lib/pardot/objects/campaigns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Pardot
module Objects
module Campaigns
def campaigns
@campaigns ||= Campaigns.new self
end

class Campaigns

def initialize(client)
@client = client
end

def query(search_criteria)
result = get('/do/query', search_criteria, 'result')
result['total_results'] = result['total_results'].to_i if result['total_results']
result
end

def create(params={})
post('/do/create', params)
end

# read_by_id
# update_by_id
[:read, :update].each do |verb|
define_method(verb) do |id, params ={}|
post(api_url(verb, 'id', id), params)
end
end

private

def api_url(verb, direct_to, value)
"/do/#{verb}/#{direct_to}/#{value}"
end

def get(path, params={}, result='campaign')
response = @client.get('campaign', path, params)
result ? response[result] : response
end

def post(path, params={}, result='campaign')
response = @client.post('campaign', path, params)
result ? response[result] : response
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/ruby-pardot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require 'pardot/error'
require 'pardot/authentication'

require 'pardot/objects/accounts'
require 'pardot/objects/campaigns'
require 'pardot/objects/emails'
require 'pardot/objects/lists'
require 'pardot/objects/list_memberships'
Expand Down
29 changes: 29 additions & 0 deletions spec/pardot/objects/accounts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe Pardot::Objects::Accounts do
before do
@client = create_client
end

describe "read" do

def sample_results
%(<?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="ok" version="1.0">
<account>
<id>123</id>
<company>Test Company</company>
</account>
</rsp>)
end

before do
@client = create_client
end

it "should read the account" do
fake_get "/api/account/version/3/do/read?user_key=bar&api_key=my_api_key&format=simple", sample_results

@client.accounts.read.should == {"id" => "123", "company" => "Test Company"}
end
end
end
111 changes: 111 additions & 0 deletions spec/pardot/objects/campaigns_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe Pardot::Objects::Campaigns do
before do
@client = create_client
end

describe "query" do

def sample_results
%(<?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="ok" version="1.0">
<result>
<total_results>2</total_results>
<campaign>
<id>123</id>
<name>First Campaign</name>
<cost>5</cost>
</campaign>
<campaign>
<id>321</id>
<name>Second Campaign</name>
<cost>0</cost>
</campaign>
</result>
</rsp>)
end

before do
@client = create_client
end

it "should query the campaigns" do
fake_get "/api/campaign/version/3/do/query?id_less_than=555&user_key=bar&api_key=my_api_key&format=simple", sample_results

@client.campaigns.query({"id_less_than" => "555"}).should == {"total_results"=>2,
"campaign"=>[
{"id"=>"123", "name"=>"First Campaign", "cost"=>"5"},
{"id"=>"321", "name"=>"Second Campaign", "cost"=>"0"}
]}
end
end

describe "create" do

def sample_results
%(<?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="ok" version="1.0">
<campaign>
<id>123</id>
<name>New Campaign</name>
<cost>5</cost>
</campaign>
</rsp>)
end

before do
@client = create_client
end

it "should create the campaign" do
fake_post "/api/campaign/version/3/do/create?name=New%20Campaign&cost=5&user_key=bar&api_key=my_api_key&format=simple", sample_results

@client.campaigns.create({"name" => "New Campaign", "cost" => "5"}).should == {"id" => "123", "name" => "New Campaign", "cost" => "5"}
end
end

describe "read" do

def sample_results
%(<?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="ok" version="1.0">
<campaign>
<id>123</id>
<name>Test Campaign</name>
<cost>0</cost>
</campaign>
</rsp>)
end

before do
@client = create_client
end

it "should read the campaign by id" do
fake_post "/api/campaign/version/3/do/read/id/123?user_key=bar&api_key=my_api_key&format=simple", sample_results

@client.campaigns.read(123).should == {"id" => "123", "name" => "Test Campaign", "cost" => "0"}
end
end

describe "update" do

def sample_results
%(<?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="ok" version="1.0">
<campaign>
<id>123</id>
<name>Test Campaign Updated</name>
<cost>0</cost>
</campaign>
</rsp>)
end

before do
@client = create_client
end

it "should update the campaign by id" do
fake_post "/api/campaign/version/3/do/update/id/123?name=Test%20Campaign%20Updated&user_key=bar&api_key=my_api_key&format=simple", sample_results

@client.campaigns.update(123, {"name" => "Test Campaign Updated"}).should == {"id" => "123", "name" => "Test Campaign Updated", "cost" => "0"}
end
end
end
5 changes: 3 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'crack'
require 'httparty'

require 'ruby-pardot'

Dir["#{File.dirname(__FILE__)}/../lib/pardot/objects/**/*.rb"].each { |f| require f }
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

require 'ruby-pardot'