Skip to content

Commit

Permalink
initial gem
Browse files Browse the repository at this point in the history
  • Loading branch information
dcunning committed May 17, 2011
0 parents commit cb5a426
Show file tree
Hide file tree
Showing 19 changed files with 458 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pkg/*
*.gem
.bundle
Empty file added .rspec
Empty file.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source :gemcutter

# Specify your gem's dependencies in ruby-pardot.gemspec
gemspec
34 changes: 34 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
PATH
remote: .
specs:
ruby-pardot (0.0.1)
crack
httparty

GEM
remote: http://rubygems.org/
specs:
crack (0.1.8)
diff-lcs (1.1.2)
fakeweb (1.3.0)
httparty (0.7.4)
crack (= 0.1.8)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
rspec-core (2.5.1)
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)

PLATFORMS
ruby

DEPENDENCIES
bundler (>= 1.0.0)
crack
fakeweb
httparty
rspec
ruby-pardot!
31 changes: 31 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
== Install

Add the following to your Gemfile

gem "ruby-pardot", :git => "git://github.com/pardot/ruby-pardot.git"

== Usage

=== Authentication

client = Pardot::Client.new email, password, user_key

# will raise a Pardot::ResponseError if login fails
# will raise a Pardot::NetError if the http call fails
client.authenticate

=== Accessing Prospects

prospects = client.prospects.query(:assigned => false)

prospects["total_results"] # number of prospects found

prospects["prospect"].each do |prospect|
puts prospect["first_name"]
end

=== Switching formats

client.format = "simple" # default
client.format = "mobile"
client.format = "full"
11 changes: 11 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'bundler'
Bundler::GemHelper.install_tasks


require 'rspec/core'
require 'rspec/core/rake_task'

desc "Run all specs"
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = "./spec/**/*_spec.rb"
end
14 changes: 14 additions & 0 deletions lib/pardot/authentication.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Pardot
module Authentication

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

def authenticated?
@api_key != nil
end

end
end
25 changes: 25 additions & 0 deletions lib/pardot/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Pardot

class Client

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

include Authentication
include Http
include Prospects

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

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

@format = "simple"
end


end
end
7 changes: 7 additions & 0 deletions lib/pardot/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Pardot

class Error < StandardError; end
class NetError < Error; end
class ResponseError < Error; end

end
54 changes: 54 additions & 0 deletions lib/pardot/http.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Pardot
module Http

def get object, path, params = {}
smooth_params object, params
path = fullpath object, path
check_response self.class.get path, :query => params

rescue SocketError, Interrupt, EOFError, SystemCallError => e
raise Pardot::NetError.new(e)
end

def post object, path, params = {}
smooth_params object, params
path = fullpath object, path
check_response self.class.post path, :query => params

rescue SocketError, Interrupt, EOFError, SystemCallError => e
raise Pardot::NetError.new(e)
end

protected

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

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

def check_response http_response
rsp = http_response["rsp"]

if rsp && rsp["err"]
raise ResponseError.new rsp["err"]
end

if rsp && rsp["stat"] == "fail"
raise ResponseError.new "Unknown Failure: #{rsp.inspect}"
end

rsp
end

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

end
end
71 changes: 71 additions & 0 deletions lib/pardot/prospects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module Pardot
module Prospects

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

def assign_by_email email, params
post "/do/assign/email/#{email}", params
end

def assign_by_id id, params
post "/do/assign/id/#{id}", params
end

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

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

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

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

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

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

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
3 changes: 3 additions & 0 deletions lib/pardot/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Pardot
VERSION = "0.0.1"
end
10 changes: 10 additions & 0 deletions lib/ruby-pardot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'httparty'
require 'crack'

require 'pardot/version'

require 'pardot/http'
require 'pardot/error'
require 'pardot/authentication'
require 'pardot/prospects'
require 'pardot/client'
27 changes: 27 additions & 0 deletions ruby-pardot.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- encoding: utf-8 -*-
require File.expand_path("../lib/pardot/version", __FILE__)

Gem::Specification.new do |s|
s.name = "ruby-pardot"
s.version = Pardot::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Dan Cunning"]
s.email = ["support@pardot.com"]
s.homepage = "http://github.com/pardot/ruby-pardot"
s.summary = "Library for interacting with the Pardot API"
s.description = "Library for interacting with the Pardot API"

s.required_rubygems_version = ">= 1.3.6"
s.rubyforge_project = "ruby-pardot"

s.add_dependency "crack"
s.add_dependency "httparty"

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

s.files = `git ls-files`.split("\n")
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
s.require_path = 'lib'
end
38 changes: 38 additions & 0 deletions spec/pardot/authentication_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Pardot::Authentication do

def create_client
@client = Pardot::Client.new "user@test.com", "foo", "bar"
end

describe "authenticate" 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</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

end

end
Loading

0 comments on commit cb5a426

Please sign in to comment.