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

Convert ruby-pardot to use JSON API instead of XML #5

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: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source :gemcutter
source 'https://rubygems.org'

# Specify your gem's dependencies in ruby-pardot.gemspec
gemspec
16 changes: 9 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
PATH
remote: .
specs:
ruby-pardot (1.0)
ruby-pardot (1.0.1)
crack
httparty

GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
crack (0.3.2)
crack (0.4.1)
safe_yaml (~> 0.9.0)
diff-lcs (1.1.2)
fakeweb (1.3.0)
httparty (0.10.2)
multi_json (~> 1.0)
httparty (0.12.0)
json (~> 1.8)
multi_xml (>= 0.5.2)
multi_json (1.6.1)
multi_xml (0.5.3)
json (1.8.1)
multi_xml (0.5.5)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
Expand All @@ -24,6 +25,7 @@ GEM
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
safe_yaml (0.9.7)

PLATFORMS
ruby
Expand Down
4 changes: 2 additions & 2 deletions lib/pardot/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Pardot
module Authentication

def authenticate
resp = post "login", nil, :email => @email, :password => @password, :user_key => @user_key
@api_key = resp["api_key"]
resp = post 'login', nil, :email => @email, :password => @password, :user_key => @user_key
@api_key = resp['api_key']
end

def authenticated?
Expand Down
9 changes: 4 additions & 5 deletions lib/pardot/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ class Client

include HTTParty
base_uri 'https://pi.pardot.com'
format :xml


include Authentication
include Http

Expand All @@ -24,9 +23,9 @@ def initialize email, password, user_key
@password = password
@user_key = user_key

@format = "simple"
@output = 'simple'
@format = 'json'
end



end
end
5 changes: 3 additions & 2 deletions lib/pardot/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ class ExpiredApiKeyError < Error; end
class ResponseError < Error
def initialize(res)
@res = res
@attributes = {}
end

def to_s
@res["__content__"]
@res['__content__']
end

def code
@res["code"].to_i
@res['code'].to_i
end

def inspect
Expand Down
21 changes: 10 additions & 11 deletions lib/pardot/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get object, path, params = {}, num_retries = 0
def post object, path, params = {}, num_retries = 0
smooth_params object, params
full_path = fullpath object, path
check_response self.class.post(full_path, :query => params)
check_response self.class.post(full_path, :body => params)

rescue Pardot::ExpiredApiKeyError => e
handle_expired_api_key :post, object, path, params, num_retries, e
Expand All @@ -36,35 +36,34 @@ def handle_expired_api_key method, object, path, params, num_retries, e
end

def smooth_params object, params
return if object == "login"
return if object == 'login'

authenticate unless authenticated?
params.merge! :user_key => @user_key, :api_key => @api_key, :format => @format
params.merge! :user_key => @user_key, :api_key => @api_key, :format => @format, :output => @output
end

def check_response http_response
rsp = http_response["rsp"]

error = rsp["err"] if rsp
error ||= "Unknown Failure: #{rsp.inspect}" if rsp && rsp["stat"] == "fail"
rsp = http_response['rsp'] || http_response || {}
error = rsp['err']
status = ( rsp['@attributes'] && rsp['@attributes']['stat'] ) || rsp['stat']
error ||= "Unknown Failure: #{rsp.inspect}" if status == 'fail'
content = error['__content__'] if error.is_a?(Hash)

if [error, content].include?("Invalid API key or user key") && @api_key
if [error, content].include?('Invalid API key or user key') && @api_key
raise ExpiredApiKeyError.new @api_key
end

raise ResponseError.new error if error

rsp
end

def fullpath object, path, version = 3
full = File.join("/api", object, "version", version.to_s)
full = File.join('/api', object, 'version', version.to_s)
unless path.nil?
full = File.join(full, path)
end
full
end

end
end
28 changes: 28 additions & 0 deletions lib/pardot/objects/base_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Pardot
module Objects
class BaseObject

def initialize client
@client = client
end

def query params
result = get '/do/query', params, nil
result['total_results'] = result['total_results'].to_i if result['total_results']
result
end

protected

def get path, params = {}, result = self.class::OBJECT_NAME
response = @client.get self.class::OBJECT_NAME, path, params
result ? response[result] : response
end

def post path, params = {}, result = self.class::OBJECT_NAME
response = @client.post self.class::OBJECT_NAME, path, params
result ? response[result] : response
end
end
end
end
36 changes: 4 additions & 32 deletions lib/pardot/objects/lists.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,22 @@
module Pardot
module Objects

module Lists

def lists
@lists ||= Lists.new self
end

class Lists

def initialize client
@client = client
end

class Lists < ::Pardot::Objects::BaseObject
OBJECT_NAME = 'list'

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

def query params
result = get "/do/query", params, "result"
result["total_results"] = result["total_results"].to_i if result["total_results"]
result
post '/do/create', params
end

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

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

protected

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

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

end

end
end
end
30 changes: 3 additions & 27 deletions lib/pardot/objects/opportunities.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
module Pardot
module Objects

module Opportunities

def opportunities
@opportunities ||= Opportunities.new self
end

class Opportunities

def initialize client
@client = client
end

def query params
result = get "/do/query", params, "result"
result["total_results"] = result["total_results"].to_i if result["total_results"]
result
end
class Opportunities < ::Pardot::Objects::BaseObject
OBJECT_NAME = 'opportunity'

def create_by_email email, params = {}
post "/do/create/prospect_email/#{email}", params
Expand All @@ -34,21 +24,7 @@ def read_by_id id, params = {}
def update_by_id id, params = {}
post "/do/update/id/#{id}", params
end

protected

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

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

end

end
end
end
29 changes: 3 additions & 26 deletions lib/pardot/objects/prospects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@ def prospects
@prospects ||= Prospects.new self
end

class Prospects

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

class Prospects < ::Pardot::Objects::BaseObject
OBJECT_NAME = 'prospect'

def assign_by_email email, params
post "/do/assign/email/#{email}", params
end
Expand Down Expand Up @@ -53,21 +44,7 @@ def upsert_by_email email, params = {}
def upsert_by_id id, params = {}
post "/do/upsert/id/#{id}", params
end

protected

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

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

end

end
end
end
27 changes: 2 additions & 25 deletions lib/pardot/objects/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@ def users
@users ||= Users.new self
end

class Users

def initialize client
@client = client
end

def query params
result = get "/do/query", params, "result"
result["total_results"] = result["total_results"].to_i if result["total_results"]
result
end
class Users < ::Pardot::Objects::BaseObject
OBJECT_NAME = 'user'

def read_by_email email, params = {}
post "/do/read/email/#{email}", params
Expand All @@ -25,21 +16,7 @@ def read_by_email email, params = {}
def read_by_id id, params = {}
post "/do/read/id/#{id}", params
end

protected

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

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

end

end
end
end
Loading