From 867c7565adffe7eba9d507940bda05b62542e0e9 Mon Sep 17 00:00:00 2001 From: Justin Roberts Date: Thu, 22 Sep 2016 08:59:41 -0400 Subject: [PATCH 1/3] Adding version 4 support, incrementing version to 1.1.0 --- Gemfile.lock | 9 +++-- lib/pardot/authentication.rb | 16 +++++++- lib/pardot/client.rb | 8 +++- lib/pardot/http.rb | 4 +- lib/pardot/objects/prospects.rb | 36 ++++++++++++++++++ lib/pardot/version.rb | 2 +- spec/pardot/authentication_spec.rb | 39 +++++++++++++++++++ spec/pardot/client_spec.rb | 28 ++++++++++++++ spec/pardot/http_spec.rb | 60 ++++++++++++++++++++++++++++++ 9 files changed, 193 insertions(+), 9 deletions(-) create mode 100644 spec/pardot/client_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index a776dc4..8a5a439 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - ruby-pardot (1.0.2) + ruby-pardot (1.1.0) crack httparty @@ -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) @@ -25,7 +25,7 @@ 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 @@ -35,3 +35,6 @@ DEPENDENCIES fakeweb rspec ruby-pardot! + +BUNDLED WITH + 1.11.2 diff --git a/lib/pardot/authentication.rb b/lib/pardot/authentication.rb index 84821b8..04763b3 100644 --- a/lib/pardot/authentication.rb +++ b/lib/pardot/authentication.rb @@ -3,6 +3,7 @@ module Authentication def authenticate resp = post "login", nil, :email => @email, :password => @password, :user_key => @user_key + set_version(parse_version(resp["version"])) if resp && resp["version"] @api_key = resp && resp["api_key"] end @@ -14,6 +15,19 @@ def reauthenticate @api_key = nil authenticate end - + + private + + def parse_version version + if version.is_a? Array + version = version.last + end + if version.to_i > 3 + return version + else + return 3 + end + end + end end diff --git a/lib/pardot/client.rb b/lib/pardot/client.rb index 914dbff..371a20b 100644 --- a/lib/pardot/client.rb +++ b/lib/pardot/client.rb @@ -20,16 +20,20 @@ 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 + def set_version version + @version = version + end end end diff --git a/lib/pardot/http.rb b/lib/pardot/http.rb index e0de31a..0dd33e9 100644 --- a/lib/pardot/http.rb +++ b/lib/pardot/http.rb @@ -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 diff --git a/lib/pardot/objects/prospects.rb b/lib/pardot/objects/prospects.rb index 704eb9a..583f3d2 100644 --- a/lib/pardot/objects/prospects.rb +++ b/lib/pardot/objects/prospects.rb @@ -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 @@ -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 @@ -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 @@ -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" diff --git a/lib/pardot/version.rb b/lib/pardot/version.rb index d71de34..e305cb3 100644 --- a/lib/pardot/version.rb +++ b/lib/pardot/version.rb @@ -1,3 +1,3 @@ module Pardot - VERSION = "1.0.2" + VERSION = "1.1.0" end diff --git a/spec/pardot/authentication_spec.rb b/spec/pardot/authentication_spec.rb index 845c265..ee0af8d 100644 --- a/spec/pardot/authentication_spec.rb +++ b/spec/pardot/authentication_spec.rb @@ -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", + %(\n\n my_api_key\n4\n\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 diff --git a/spec/pardot/client_spec.rb b/spec/pardot/client_spec.rb new file mode 100644 index 0000000..324f2e4 --- /dev/null +++ b/spec/pardot/client_spec.rb @@ -0,0 +1,28 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe Pardot::Client do + + def create_client + @client = Pardot::Client.new "user@test.com", "foo", "bar" + end + + describe "client" do + after do + @client.email.should == "user@test.com" + @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 "user@test.com", "password", "user_key" + @client.version.should == 3 + end + + it "should set variables with version" do + @client = Pardot::Client.new "user@test.com", "password", "user_key", 4 + @client.version.should == 4 + end + + end +end diff --git a/spec/pardot/http_spec.rb b/spec/pardot/http_spec.rb index 9b98e70..cc21e27 100644 --- a/spec/pardot/http_spec.rb +++ b/spec/pardot/http_spec.rb @@ -68,5 +68,65 @@ def post object = "foo", path = "/bar", params = {} end end + + describe "getV4" do + + def get object = "foo", path = "/bar", params = {} + @client.set_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\n Login failed\n\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\n Invalid API key or user key\n\n) + + @client.should_receive(:handle_expired_api_key) + get + end + + end + + describe "postV4" do + + def post object = "foo", path = "/bar", params = {} + @client.set_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\n Login failed\n\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\n Invalid API key or user key\n\n) + + @client.should_receive(:handle_expired_api_key) + post + end + + end end \ No newline at end of file From bca5b05f528857d69c3cfeef3318c43fac7a3479 Mon Sep 17 00:00:00 2001 From: Justin Roberts Date: Thu, 22 Sep 2016 09:04:11 -0400 Subject: [PATCH 2/3] set_verion method wasn't necessary --- lib/pardot/authentication.rb | 10 +++------- lib/pardot/client.rb | 4 ---- spec/pardot/http_spec.rb | 4 ++-- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/pardot/authentication.rb b/lib/pardot/authentication.rb index 04763b3..7919285 100644 --- a/lib/pardot/authentication.rb +++ b/lib/pardot/authentication.rb @@ -3,7 +3,7 @@ module Authentication def authenticate resp = post "login", nil, :email => @email, :password => @password, :user_key => @user_key - set_version(parse_version(resp["version"])) if resp && resp["version"] + update_version(resp["version"]) if resp && resp["version"] @api_key = resp && resp["api_key"] end @@ -18,15 +18,11 @@ def reauthenticate private - def parse_version version + def update_version version if version.is_a? Array version = version.last end - if version.to_i > 3 - return version - else - return 3 - end + @version = version if version.to_i > 3 end end diff --git a/lib/pardot/client.rb b/lib/pardot/client.rb index 371a20b..36e9745 100644 --- a/lib/pardot/client.rb +++ b/lib/pardot/client.rb @@ -31,9 +31,5 @@ def initialize email, password, user_key, version = 3 @format = "simple" end - def set_version version - @version = version - end - end end diff --git a/spec/pardot/http_spec.rb b/spec/pardot/http_spec.rb index cc21e27..0039887 100644 --- a/spec/pardot/http_spec.rb +++ b/spec/pardot/http_spec.rb @@ -72,7 +72,7 @@ def post object = "foo", path = "/bar", params = {} describe "getV4" do def get object = "foo", path = "/bar", params = {} - @client.set_version "4" + @client.version = "4" @client.get object, path, params end @@ -102,7 +102,7 @@ def get object = "foo", path = "/bar", params = {} describe "postV4" do def post object = "foo", path = "/bar", params = {} - @client.set_version "4" + @client.version = "4" @client.post object, path, params end From 6fd1ad0a5802f992564e4111611b4c45a9c11db5 Mon Sep 17 00:00:00 2001 From: Justin Roberts Date: Mon, 26 Sep 2016 14:02:05 -0400 Subject: [PATCH 3/3] updating bundler version --- Gemfile.lock | 2 +- ruby-pardot.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 8a5a439..82e43ea 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -31,7 +31,7 @@ PLATFORMS ruby DEPENDENCIES - bundler (>= 1.0.0) + bundler (>= 1.10) fakeweb rspec ruby-pardot! diff --git a/ruby-pardot.gemspec b/ruby-pardot.gemspec index 979b4b9..f710b11 100644 --- a/ruby-pardot.gemspec +++ b/ruby-pardot.gemspec @@ -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"