Skip to content

Commit 66bce69

Browse files
author
Jared Pearson
authored
Update based on Rubocop auto correct (#56)
1 parent f0fe253 commit 66bce69

38 files changed

+658
-699
lines changed

.rubocop.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
AllCops:
2+
NewCops: enable
3+
4+
Metrics/BlockLength:
5+
Enabled: false
6+
Metrics/MethodLength:
7+
Enabled: false
8+
Layout/LineLength:
9+
Enabled: false

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
source :gemcutter
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
24

35
# Specify your gem's dependencies in ruby-pardot.gemspec
46
gemspec

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ PATH
66
httparty (= 0.18.1)
77

88
GEM
9-
remote: http://rubygems.org/
9+
remote: https://rubygems.org/
1010
specs:
1111
crack (0.4.3)
1212
safe_yaml (~> 1.0.0)
@@ -44,4 +44,4 @@ DEPENDENCIES
4444
ruby-pardot!
4545

4646
BUNDLED WITH
47-
2.1.4
47+
2.2.7

Rakefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
require 'bundler'
22
Bundler::GemHelper.install_tasks
33

4-
54
require 'rspec/core'
65
require 'rspec/core/rake_task'
76

8-
desc "Run all specs"
7+
desc 'Run all specs'
98
RSpec::Core::RakeTask.new(:spec) do |t|
10-
t.pattern = "./spec/**/*_spec.rb"
9+
t.pattern = './spec/**/*_spec.rb'
1110
end

lib/pardot/authentication.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
module Pardot
22
module Authentication
3-
43
# @deprecated Use of username and password authentication is deprecated.
54
def authenticate
6-
raise "Authentication not available when using Salesforce access token. See https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_oauth_and_connected_apps.htm for more information." if using_salesforce_access_token?
7-
warn "[DEPRECATION] Use of username and password authentication is deprecated in favor of Salesforce OAuth. See https://developer.pardot.com/kb/authentication/ for more information."
8-
resp = post "login", nil, nil, nil, :email => @email, :password => @password, :user_key => @user_key
9-
update_version(resp["version"]) if resp && resp["version"]
10-
@api_key = resp && resp["api_key"]
5+
if using_salesforce_access_token?
6+
raise 'Authentication not available when using Salesforce access token. See https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_oauth_and_connected_apps.htm for more information.'
7+
end
8+
9+
warn '[DEPRECATION] Use of username and password authentication is deprecated in favor of Salesforce OAuth. See https://developer.pardot.com/kb/authentication/ for more information.'
10+
resp = post 'login', nil, nil, nil, email: @email, password: @password, user_key: @user_key
11+
update_version(resp['version']) if resp && resp['version']
12+
@api_key = resp && resp['api_key']
1113
end
12-
14+
1315
def authenticated?
14-
@api_key != nil || @salesforce_access_token != nil
16+
!@api_key.nil? || !@salesforce_access_token.nil?
1517
end
1618

1719
def using_salesforce_access_token?
1820
@salesforce_access_token != nil
1921
end
20-
22+
2123
def reauthenticate
22-
raise "Reauthentication not available when using Salesforce access token. See https://developer.salesforce.com/docs/atlas.en-us.mobile_sdk.meta/mobile_sdk/oauth_refresh_token_flow.htm for more information." if using_salesforce_access_token?
23-
warn "[DEPRECATION] Use Salesforce OAuth to refresh the Salesforce access token. See https://developer.salesforce.com/docs/atlas.en-us.mobile_sdk.meta/mobile_sdk/oauth_refresh_token_flow.htm for more information."
24+
if using_salesforce_access_token?
25+
raise 'Reauthentication not available when using Salesforce access token. See https://developer.salesforce.com/docs/atlas.en-us.mobile_sdk.meta/mobile_sdk/oauth_refresh_token_flow.htm for more information.'
26+
end
27+
28+
warn '[DEPRECATION] Use Salesforce OAuth to refresh the Salesforce access token. See https://developer.salesforce.com/docs/atlas.en-us.mobile_sdk.meta/mobile_sdk/oauth_refresh_token_flow.htm for more information.'
2429
@api_key = nil
2530
authenticate
2631
end
2732

2833
private
2934

30-
def update_version version
31-
if version.is_a? Array
32-
version = version.last
33-
end
35+
def update_version(version)
36+
version = version.last if version.is_a? Array
3437
@version = version if version.to_i > 3
3538
end
36-
3739
end
3840
end

lib/pardot/client.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module Pardot
2-
32
class Client
4-
53
include HTTParty
64
base_uri 'https://pi.pardot.com'
75
format :xml
@@ -23,14 +21,18 @@ class Client
2321

2422
attr_accessor :email, :password, :user_key, :api_key, :version, :salesforce_access_token, :business_unit_id, :format
2523

26-
# @deprecated Arguments email, password and user_key are deprecated. Use salesforce_access_token with Salesforce OAuth.
27-
def initialize email = nil, password = nil, user_key = nil, version = 3, salesforce_access_token = nil, business_unit_id = nil
28-
if !(email.nil? || password.nil? || user_key.nil?) then
29-
warn "[DEPRECATION] Use of username and password authentication is deprecated in favor of Salesforce OAuth. See https://developer.pardot.com/kb/authentication/ for more information."
24+
# @deprecated Arguments email, password and user_key are deprecated. Use salesforce_access_token with Salesforce OAuth.
25+
def initialize(email = nil, password = nil, user_key = nil, version = 3, salesforce_access_token = nil, business_unit_id = nil)
26+
unless email.nil? || password.nil? || user_key.nil?
27+
warn '[DEPRECATION] Use of username and password authentication is deprecated in favor of Salesforce OAuth. See https://developer.pardot.com/kb/authentication/ for more information.'
3028
end
3129

32-
raise "business_unit_id required when using Salesforce access_token" if !salesforce_access_token.nil? && business_unit_id.nil?
33-
raise "Invalid business_unit_id value. Expected ID to start with '0Uv' and be length of 18 characters." if !business_unit_id.nil? && (!business_unit_id.start_with?('0Uv') || business_unit_id.length != 18)
30+
if !salesforce_access_token.nil? && business_unit_id.nil?
31+
raise 'business_unit_id required when using Salesforce access_token'
32+
end
33+
if !business_unit_id.nil? && (!business_unit_id.start_with?('0Uv') || business_unit_id.length != 18)
34+
raise "Invalid business_unit_id value. Expected ID to start with '0Uv' and be length of 18 characters."
35+
end
3436

3537
@email = email
3638
@password = password
@@ -39,8 +41,7 @@ def initialize email = nil, password = nil, user_key = nil, version = 3, salesfo
3941
@salesforce_access_token = salesforce_access_token
4042
@business_unit_id = business_unit_id
4143

42-
@format = "simple"
44+
@format = 'simple'
4345
end
44-
4546
end
4647
end

lib/pardot/error.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
module Pardot
22
class Error < StandardError; end
3+
34
class NetError < Error; end
5+
46
class ExpiredApiKeyError < Error; end
7+
58
class AccessTokenExpiredError < Error; end
69

710
class ResponseError < Error
@@ -10,11 +13,11 @@ def initialize(res)
1013
end
1114

1215
def to_s
13-
@res["__content__"]
16+
@res['__content__']
1417
end
1518

1619
def code
17-
@res["code"].to_i
20+
@res['code'].to_i
1821
end
1922

2023
def inspect

lib/pardot/http.rb

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,80 @@
11
module Pardot
22
module Http
3-
4-
def get object, path, params = {}, num_retries = 0
3+
def get(object, path, params = {}, num_retries = 0)
54
smooth_params object, params
65
full_path = fullpath object, path
76
headers = create_auth_header object
8-
check_response self.class.get(full_path, :query => params, :headers => headers)
9-
7+
check_response self.class.get(full_path, query: params, headers: headers)
108
rescue Pardot::ExpiredApiKeyError => e
119
handle_expired_api_key :get, object, path, params, num_retries, e
12-
1310
rescue SocketError, Interrupt, EOFError, SystemCallError, Timeout::Error, MultiXml::ParseError => e
14-
raise Pardot::NetError.new(e)
11+
raise Pardot::NetError, e
1512
end
16-
17-
def post object, path, params = {}, num_retries = 0, bodyParams = {}
13+
14+
def post(object, path, params = {}, num_retries = 0, bodyParams = {})
1815
smooth_params object, params
1916
full_path = fullpath object, path
2017
headers = create_auth_header object
21-
check_response self.class.post(full_path, :query => params, :body => bodyParams, :headers => headers)
22-
18+
check_response self.class.post(full_path, query: params, body: bodyParams, headers: headers)
2319
rescue Pardot::ExpiredApiKeyError => e
2420
handle_expired_api_key :post, object, path, params, num_retries, e
25-
2621
rescue SocketError, Interrupt, EOFError, SystemCallError, Timeout::Error, MultiXml::ParseError => e
27-
raise Pardot::NetError.new(e)
22+
raise Pardot::NetError, e
2823
end
29-
24+
3025
protected
31-
32-
# @deprecated With Salesforce OAuth, this method will never be invoked.
33-
def handle_expired_api_key method, object, path, params, num_retries, e
26+
27+
# @deprecated With Salesforce OAuth, this method will never be invoked.
28+
def handle_expired_api_key(method, object, path, params, num_retries, e)
3429
raise e unless num_retries == 0
35-
30+
3631
reauthenticate
37-
32+
3833
send(method, object, path, params, 1)
3934
end
40-
41-
def smooth_params object, params
42-
return if object == "login"
43-
35+
36+
def smooth_params(object, params)
37+
return if object == 'login'
38+
4439
authenticate unless authenticated?
45-
params.merge! :format => @format
40+
params.merge! format: @format
4641
end
4742

48-
def create_auth_header object
49-
return if object == "login"
50-
if using_salesforce_access_token? then
51-
{
43+
def create_auth_header(object)
44+
return if object == 'login'
45+
46+
if using_salesforce_access_token?
47+
{
5248
:Authorization => "Bearer #{@salesforce_access_token}",
5349
'Pardot-Business-Unit-Id' => @business_unit_id
5450
}
5551
else
56-
{ :Authorization => "Pardot api_key=#{@api_key}, user_key=#{@user_key}" }
52+
{ Authorization: "Pardot api_key=#{@api_key}, user_key=#{@user_key}" }
5753
end
5854
end
59-
60-
def check_response http_response
61-
rsp = http_response["rsp"]
62-
63-
error = rsp["err"] if rsp
64-
error ||= "Unknown Failure: #{rsp.inspect}" if rsp && rsp["stat"] == "fail"
55+
56+
def check_response(http_response)
57+
rsp = http_response['rsp']
58+
59+
error = rsp['err'] if rsp
60+
error ||= "Unknown Failure: #{rsp.inspect}" if rsp && rsp['stat'] == 'fail'
6561
content = error['__content__'] if error.is_a?(Hash)
66-
67-
if [error, content].include?("access_token is invalid") && using_salesforce_access_token?
68-
raise AccessTokenExpiredError.new "Access token is invalid. Use Salesforce OAuth to refresh the existing Salesforce access token or to retrieve a new token. See https://developer.salesforce.com/docs/atlas.en-us.mobile_sdk.meta/mobile_sdk/oauth_refresh_token_flow.htm for more information."
69-
end
70-
if [error, content].include?("Invalid API key or user key") && @api_key
71-
raise ExpiredApiKeyError.new @api_key
62+
63+
if [error, content].include?('access_token is invalid') && using_salesforce_access_token?
64+
raise AccessTokenExpiredError,
65+
'Access token is invalid. Use Salesforce OAuth to refresh the existing Salesforce access token or to retrieve a new token. See https://developer.salesforce.com/docs/atlas.en-us.mobile_sdk.meta/mobile_sdk/oauth_refresh_token_flow.htm for more information.'
7266
end
73-
74-
raise ResponseError.new error if error
75-
67+
raise ExpiredApiKeyError, @api_key if [error, content].include?('Invalid API key or user key') && @api_key
68+
69+
raise ResponseError, error if error
70+
7671
rsp
7772
end
78-
79-
def fullpath object, path
80-
full = File.join("/api", object, "version", @version.to_s)
81-
unless path.nil?
82-
full = File.join(full, path)
83-
end
73+
74+
def fullpath(object, path)
75+
full = File.join('/api', object, 'version', @version.to_s)
76+
full = File.join(full, path) unless path.nil?
8477
full
8578
end
86-
8779
end
8880
end

lib/pardot/objects/custom_fields.rb

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
module Pardot
22
module Objects
33
module CustomFields
4-
54
def custom_fields
65
@custom_fields ||= CustomFields.new self
76
end
8-
7+
98
class CustomFields
10-
11-
def initialize client
9+
def initialize(client)
1210
@client = client
1311
end
14-
15-
def query params
16-
result = get "/do/query", params, "result"
17-
result["total_results"] = result["total_results"].to_i if result["total_results"]
12+
13+
def query(params)
14+
result = get '/do/query', params, 'result'
15+
result['total_results'] = result['total_results'].to_i if result['total_results']
1816
result
1917
end
20-
18+
2119
protected
22-
23-
def get path, params = {}, result = "customField"
24-
response = @client.get "customField", path, params
20+
21+
def get(path, params = {}, result = 'customField')
22+
response = @client.get 'customField', path, params
2523
result ? response[result] : response
2624
end
27-
28-
def post path, params = {}, result = "user"
29-
response = @client.post "customField", path, params
25+
26+
def post(path, params = {}, result = 'user')
27+
response = @client.post 'customField', path, params
3028
result ? response[result] : response
3129
end
32-
3330
end
34-
3531
end
3632
end
3733
end

0 commit comments

Comments
 (0)