Skip to content

Commit

Permalink
Merge pull request #29 from Pardot/version_4
Browse files Browse the repository at this point in the history
Adding version 4 support, incrementing version to 1.1.0
  • Loading branch information
Justin Roberts authored Sep 26, 2016
2 parents 837bae6 + 6fd1ad0 commit fdc4dc8
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 12 deletions.
11 changes: 7 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ruby-pardot (1.0.2)
ruby-pardot (1.1.0)
crack
httparty

Expand All @@ -15,7 +15,7 @@ GEM
httparty (0.13.1)
json (~> 1.8)
multi_xml (>= 0.5.2)
json (1.8.1)
json (1.8.3)
multi_xml (0.5.5)
rspec (2.5.0)
rspec-core (~> 2.5.0)
Expand All @@ -25,13 +25,16 @@ GEM
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
safe_yaml (1.0.3)
safe_yaml (1.0.4)

PLATFORMS
ruby

DEPENDENCIES
bundler (>= 1.0.0)
bundler (>= 1.10)
fakeweb
rspec
ruby-pardot!

BUNDLED WITH
1.11.2
12 changes: 11 additions & 1 deletion lib/pardot/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Authentication

def authenticate
resp = post "login", nil, :email => @email, :password => @password, :user_key => @user_key
update_version(resp["version"]) if resp && resp["version"]
@api_key = resp && resp["api_key"]
end

Expand All @@ -14,6 +15,15 @@ def reauthenticate
@api_key = nil
authenticate
end


private

def update_version version
if version.is_a? Array
version = version.last
end
@version = version if version.to_i > 3
end

end
end
6 changes: 3 additions & 3 deletions lib/pardot/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ class Client
include Objects::Visits
include Objects::VisitorActivities

attr_accessor :email, :password, :user_key, :api_key, :format
attr_accessor :email, :password, :user_key, :api_key, :version, :format

def initialize email, password, user_key
def initialize email, password, user_key, version = 3
@email = email
@password = password
@user_key = user_key
@version = version

@format = "simple"
end


end
end
4 changes: 2 additions & 2 deletions lib/pardot/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def check_response http_response
rsp
end

def fullpath object, path, version = 3
full = File.join("/api", object, "version", version.to_s)
def fullpath object, path
full = File.join("/api", object, "version", @version.to_s)
unless path.nil?
full = File.join(full, path)
end
Expand Down
36 changes: 36 additions & 0 deletions lib/pardot/objects/prospects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@ def assign_by_id id, params
post "/do/assign/id/#{id}", params
end

def assign_by_fid fid, params
post "/do/assign/fid/#{fid}", params
end

def create email, params = {}
post "/do/create/email/#{email}", params
end

def delete_by_id id, params = {}
post "/do/delete/id/#{id}", params
end

def delete_by_fid fid, params = {}
post "/do/delete/fid/#{fid}", params
end

def read_by_email email, params = {}
post "/do/read/email/#{email}", params
end
Expand All @@ -38,6 +50,22 @@ def read_by_id id, params = {}
post "/do/read/id/#{id}", params
end

def read_by_fid fid, params = {}
post "/do/read/fid/#{fid}", params
end

def unassign_by_email email, params = {}
post "/do/unassign/email/#{email}", params
end

def unassign_by_id id, params = {}
post "/do/unassign/id/#{id}", params
end

def unassign_by_fid fid, params = {}
post "/do/unassign/fid/#{fid}", params
end

def update_by_email email, params = {}
post "/do/update/email/#{email}", params
end
Expand All @@ -46,6 +74,10 @@ def update_by_id id, params = {}
post "/do/update/id/#{id}", params
end

def update_by_fid fid, params = {}
post "/do/update/fid/#{fid}", params
end

def upsert_by_email email, params = {}
post "/do/upsert/email/#{email}", params
end
Expand All @@ -54,6 +86,10 @@ def upsert_by_id id, params = {}
post "/do/upsert/id/#{id}", params
end

def upsert_by_fid fid, params = {}
post "/do/upsert/fid/#{fid}", params
end

protected

def get path, params = {}, result = "prospect"
Expand Down
2 changes: 1 addition & 1 deletion lib/pardot/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Pardot
VERSION = "1.0.2"
VERSION = "1.1.0"
end
2 changes: 1 addition & 1 deletion ruby-pardot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |s|
s.add_dependency "crack"
s.add_dependency "httparty"

s.add_development_dependency "bundler", ">= 1.0.0"
s.add_development_dependency "bundler", ">= 1.10"
s.add_development_dependency "rspec"
s.add_development_dependency "fakeweb"

Expand Down
39 changes: 39 additions & 0 deletions spec/pardot/authentication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,45 @@ def authenticate
authenticate
@client.authenticated?.should == true
end

it "should use version 3" do
authenticate
@client.version.to_i.should == 3
end

end

describe "authenticateV4" do

before do
@client = create_client

fake_post "/api/login/version/3?email=user%40test.com&password=foo&user_key=bar",
%(<?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="ok" version="1.0">\n <api_key>my_api_key</api_key>\n<version>4</version>\n</rsp>\n)
end

def authenticate
@client.authenticate
end

it "should return the api key" do
authenticate.should == "my_api_key"
end

it "should set the api key" do
authenticate
@client.api_key.should == "my_api_key"
end

it "should make authenticated? true" do
authenticate
@client.authenticated?.should == true
end

it "should use version 4" do
authenticate
@client.version.to_i.should == 4
end

end

Expand Down
28 changes: 28 additions & 0 deletions spec/pardot/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Pardot::Client do

def create_client
@client = Pardot::Client.new "[email protected]", "foo", "bar"
end

describe "client" do
after do
@client.email.should == "[email protected]"
@client.password.should == "password"
@client.user_key.should == "user_key"
@client.format.should == "simple"
end

it "should set variables without version" do
@client = Pardot::Client.new "[email protected]", "password", "user_key"
@client.version.should == 3
end

it "should set variables with version" do
@client = Pardot::Client.new "[email protected]", "password", "user_key", 4
@client.version.should == 4
end

end
end
60 changes: 60 additions & 0 deletions spec/pardot/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,65 @@ def post object = "foo", path = "/bar", params = {}
end

end

describe "getV4" do

def get object = "foo", path = "/bar", params = {}
@client.version = "4"
@client.get object, path, params
end

it "should notice errors and raise them as Pardot::ResponseError" do
fake_get "/api/foo/version/4/bar?api_key=my_api_key&format=simple&user_key=bar",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Login failed</err>\n</rsp>\n)

lambda { get }.should raise_error(Pardot::ResponseError)
end

it "should catch and reraise SocketErrors as Pardot::NetError" do
Pardot::Client.should_receive(:get).and_raise(SocketError)

lambda { get }.should raise_error(Pardot::NetError)
end

it "should call handle_expired_api_key when the api key expires" do
fake_get "/api/foo/version/4/bar?api_key=my_api_key&format=simple&user_key=bar",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Invalid API key or user key</err>\n</rsp>\n)

@client.should_receive(:handle_expired_api_key)
get
end

end

describe "postV4" do

def post object = "foo", path = "/bar", params = {}
@client.version = "4"
@client.post object, path, params
end

it "should notice errors and raise them as Pardot::ResponseError" do
fake_post "/api/foo/version/4/bar?api_key=my_api_key&format=simple&user_key=bar",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Login failed</err>\n</rsp>\n)

lambda { post }.should raise_error(Pardot::ResponseError)
end

it "should catch and reraise SocketErrors as Pardot::NetError" do
Pardot::Client.should_receive(:post).and_raise(SocketError)

lambda { post }.should raise_error(Pardot::NetError)
end

it "should call handle_expired_api_key when the api key expires" do
fake_post "/api/foo/version/4/bar?api_key=my_api_key&format=simple&user_key=bar",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Invalid API key or user key</err>\n</rsp>\n)

@client.should_receive(:handle_expired_api_key)
post
end

end

end

0 comments on commit fdc4dc8

Please sign in to comment.