Skip to content

Commit b7d98e7

Browse files
author
Krisztian Szabo
committed
Use new features of rspec to simplify specs and implementation
1 parent 7f0b684 commit b7d98e7

File tree

2 files changed

+42
-47
lines changed

2 files changed

+42
-47
lines changed

lib/mkto_rest.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,11 @@ def debug=(bool)
2323
end
2424

2525
def authenticated?
26-
!@token.empty? && token_valid?
26+
!token.empty? && token_valid?
2727
end
2828

2929
def token_valid?
30-
return Time.now < @valid_until
31-
end
32-
33-
# used for testing only
34-
def __auth(token)
35-
@token = token
30+
valid_until && Time.now < valid_until
3631
end
3732

3833
# \options:
@@ -61,7 +56,7 @@ def get_leads(filtr, values = [], fields = [], &block)
6156
# values can be a string or an array
6257
values = values.split unless values.is_a? Array
6358
args = {
64-
access_token: @token,
59+
access_token: token,
6560
filterType: filtr.to_s,
6661
filterValues: values.join(',')
6762
}
@@ -112,13 +107,13 @@ def create_leads(leads, action = 'createOnly', partition = nil)
112107

113108
def associate_lead(id, cookie)
114109
authenticate unless authenticated?
115-
post(nil, "https://#{@host}/rest/v1/leads/#{id}/associate.json?#{URI.encode_www_form(cookie: cookie, access_token: @token)}")
110+
post(nil, "https://#{@host}/rest/v1/leads/#{id}/associate.json?#{URI.encode_www_form(cookie: cookie, access_token: token)}")
116111
end
117112

118113
def post(data, url = "https://#{@host}/rest/v1/leads.json")
119114
authenticate unless authenticated?
120115
fail 'client not authenticated.' unless authenticated?
121-
headers = { 'Authorization' => "Bearer #{@token}" }
116+
headers = { 'Authorization' => "Bearer #{token}" }
122117
body = MktoRest::HttpUtils.post(url, headers, data.to_json, @options)
123118
data = JSON.parse(body, symbolize_names: true)
124119
handle_errors(data[:errors]) if data[:success] == false

spec/mkto_rest_spec.rb

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@
44

55
describe MktoRest do
66

7-
before do
8-
@client_id = 'id'
9-
@client_key = 'key'
10-
@hostname = 'dummy.mktorest.com'
11-
@token = 'token'
12-
13-
@client = MktoRest::Client.new(host: @hostname, client_id: @client_id, client_secret: @client_key)
14-
@authenticated_client = MktoRest::Client.new(host: @hostname, client_id: @client_id, client_secret: @client_key)
15-
@authenticated_client.__auth(@token)
7+
let(:client_id) { 'id' }
8+
let(:client_key) { 'key' }
9+
let(:hostname) { 'dummy.mktorest.com' }
10+
let(:token) { 'token' }
11+
let(:client) { MktoRest::Client.new(host: hostname, client_id: client_id, client_secret: client_key) }
12+
let(:authenticated_client) { MktoRest::Client.new(host: hostname, client_id: client_id, client_secret: client_key) }
13+
let(:lead1) { MktoRest::Lead.new(authenticated_client, name: 'john', email: 'john@acme.com', id: 1) }
1614

17-
@lead1 = MktoRest::Lead.new(@authenticated_client, name: 'john', email: 'john@acme.com', id: 1)
15+
before do
16+
allow(authenticated_client).to receive(:valid_until).and_return(Time.now + (60 * 60 * 24))
17+
allow(authenticated_client).to receive(:token).and_return(token)
1818
end
1919

2020
describe 'v1 API' do
2121
context '#authenticated?' do
2222
context 'before authentication' do
2323
it 'should be false' do
24-
expect(@client.authenticated?) == false
24+
expect(client.authenticated?) == false
2525
end
2626
end
2727
context 'after authentication' do
2828
it 'should be true' do
29-
expect(@authenticated_client.authenticated?) == true
29+
expect(authenticated_client.authenticated?) == true
3030
end
3131
end
3232
end
@@ -35,19 +35,19 @@
3535
# repsonses samples are in responses_samples/*.json
3636
describe 'authentication' do
3737
it 'parses response' do
38-
set_authentication_stub_request(@hostname, @client_id, @client_key)
39-
expect { @client.authenticate }.not_to raise_error
40-
expect(@client.token).to_not be_nil
41-
expect(@client.expires_in).to_not be_nil
42-
expect(@client.valid_until).to_not be_nil
43-
expect(@client.token_type).to_not be_nil
44-
expect(@client.scope).to_not be_nil
38+
set_authentication_stub_request(hostname, client_id, client_key)
39+
expect { client.authenticate }.not_to raise_error
40+
expect(client.token).to_not be_nil
41+
expect(client.expires_in).to_not be_nil
42+
expect(client.valid_until).to_not be_nil
43+
expect(client.token_type).to_not be_nil
44+
expect(client.scope).to_not be_nil
4545
end
4646
end
4747
describe 'leads operations' do
4848
it 'uses correct HTTP GET body and headers' do
49-
set_get_leads_stub_request('email', 'john@acme.com', @hostname, @token)
50-
expect { @authenticated_client.get_leads :email, 'john@acme.com' }.not_to raise_error
49+
set_get_leads_stub_request('email', 'john@acme.com', hostname, token)
50+
expect { authenticated_client.get_leads :email, 'john@acme.com' }.not_to raise_error
5151
end
5252
end
5353

@@ -85,37 +85,37 @@
8585
let(:partition) { 'bizdev' }
8686

8787
it 'can be updated by id' do
88-
set_update_lead_stub_request(:id, 1, { 'someFieldX' => 'new_value' }, @hostname, @token)
89-
@lead1.update({ 'someFieldX' => 'new_value' }, :id)
88+
set_update_lead_stub_request(:id, 1, { 'someFieldX' => 'new_value' }, hostname, token)
89+
lead1.update({ 'someFieldX' => 'new_value' }, :id)
9090
end
9191
it 'can be updated by email' do
92-
set_update_lead_stub_request(:email, @lead1.email, { 'someFieldX' => 'new_value' }, @hostname, @token)
93-
@lead1.update({ 'someFieldX' => 'new_value' }, :email)
92+
set_update_lead_stub_request(:email, lead1.email, { 'someFieldX' => 'new_value' }, hostname, token)
93+
lead1.update({ 'someFieldX' => 'new_value' }, :email)
9494
end
9595

9696
it 'one can be created with email but w/out partition' do
97-
set_create_leads_stub_request(sample_lead, @hostname, @token)
98-
@authenticated_client.create_leads(sample_lead)
97+
set_create_leads_stub_request(sample_lead, hostname, token)
98+
authenticated_client.create_leads(sample_lead)
9999
end
100100

101101
it 'multiple leads can be created with emails but w/out partition' do
102-
set_create_leads_stub_request(sample_leads, @hostname, @token)
103-
@authenticated_client.create_leads(sample_leads)
102+
set_create_leads_stub_request(sample_leads, hostname, token)
103+
authenticated_client.create_leads(sample_leads)
104104
end
105105

106106
it 'multiple can be created with email and partition' do
107-
set_create_leads_stub_request(sample_leads, @hostname, @token, partition: partition)
108-
@authenticated_client.create_leads(sample_leads, 'createOnly', partition)
107+
set_create_leads_stub_request(sample_leads, hostname, token, partition: partition)
108+
authenticated_client.create_leads(sample_leads, 'createOnly', partition)
109109
end
110110
end
111111

112112
describe 'client' do
113-
let(:lead) { MktoRest::Lead.new(@authenticated_client, id: 1, email: 'joe@acme.com') }
114-
before { set_get_leads_stub_request('email', lead.email, @hostname, @token) }
113+
let(:lead) { MktoRest::Lead.new(authenticated_client, id: 1, email: 'joe@acme.com') }
114+
before { set_get_leads_stub_request('email', lead.email, hostname, token) }
115115

116116
describe '#get_leads' do
117117
let(:blk) { nil }
118-
let(:leads) { @authenticated_client.get_leads(:email, lead.email, &blk) }
118+
let(:leads) { authenticated_client.get_leads(:email, lead.email, &blk) }
119119

120120
it 'should return leads' do
121121
expect(leads).to_not be_empty
@@ -135,16 +135,16 @@
135135
describe '#associate_lead' do
136136
let(:id) { lead.id }
137137
let(:cookie) { 'id:287-GTJ-838&token:_mch-marketo.com-1396310362214-46169' }
138-
let(:association) { @authenticated_client.associate_lead(id, cookie) }
138+
let(:association) { authenticated_client.associate_lead(id, cookie) }
139139

140-
before { stub_associate_lead_request(@hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', @token) }
140+
before { stub_associate_lead_request(hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', token) }
141141

142142
it 'associates the lead' do
143143
expect(association).to include(success: true)
144144
end
145145

146146
context 'with an unathorized request' do
147-
before { stub_failed_associate_lead_request(@hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', @token) }
147+
before { stub_failed_associate_lead_request(hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', token) }
148148

149149
it 'raises a MktoRest::Errors::AccessTokenInvalid exception with the message' do
150150
expect { association }.to raise_error(MktoRest::Errors::AccessTokenInvalid, 'Unauthorized')

0 commit comments

Comments
 (0)