Skip to content

Commit 56e1269

Browse files
authored
Merge pull request #49 from DripEmail/repair_json_parsing
Repair json parsing issues.
2 parents a20094a + 0fc8f12 commit 56e1269

17 files changed

Lines changed: 118 additions & 90 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010

1111
- Your contribution here!
1212

13+
### Fixed
14+
15+
- [#48](https://github.com/DripEmail/drip-ruby/issues/48): Repair json parsing to work correctly
16+
1317
## [3.1.0] - 2018-06-05
1418

1519
[3.1.0]: https://github.com/DripEmail/drip-ruby/compare/v3.0.0...v3.1.0

lib/drip/client.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ def make_request(verb_klass, uri, options, step = 0)
116116

117117
def build_response(&block)
118118
response = yield
119-
Drip::Response.new(response.code.to_i, response.body)
119+
Drip::Response.new(response.code.to_i, response.body || response.body == "" ? JSON.parse(response.body) : nil)
120+
rescue JSON::ParserError
121+
Drip::Response.new(response.code.to_i, nil)
120122
end
121123

122124
def connection_options(uri_scheme)

test/drip/client/accounts_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ def setup
88
context "#accounts" do
99
setup do
1010
@response_status = 200
11-
@response_body = "stub"
11+
@response_body = "{}"
1212

1313
stub_request(:get, "https://api.getdrip.com/v2/accounts").
1414
to_return(status: @response_status, body: @response_body, headers: {})
1515
end
1616

1717
should "send the right request" do
18-
expected = Drip::Response.new(@response_status, @response_body)
18+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
1919
assert_equal expected, @client.accounts
2020
end
2121
end
2222

2323
context "#account" do
2424
setup do
2525
@response_status = 200
26-
@response_body = "stub"
26+
@response_body = "{}"
2727
@id = 9999999
2828

2929
stub_request(:get, "https://api.getdrip.com/v2/accounts/#{@id}").
3030
to_return(status: @response_status, body: @response_body, headers: {})
3131
end
3232

3333
should "send the right request" do
34-
expected = Drip::Response.new(@response_status, @response_body)
34+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
3535
assert_equal expected, @client.account(@id)
3636
end
3737
end

test/drip/client/broadcasts_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ def setup
88
context "#broadcasts" do
99
setup do
1010
@response_status = 200
11-
@response_body = "stub"
11+
@response_body = "{}"
1212

1313
stub_request(:get, "https://api.getdrip.com/v2/12345/broadcasts").
1414
to_return(status: @response_status, body: @response_body, headers: {})
1515
end
1616

1717
should "send the correct request" do
18-
expected = Drip::Response.new(@response_status, @response_body)
18+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
1919
assert_equal expected, @client.broadcasts
2020
end
2121
end
2222

2323
context "#broadcast" do
2424
setup do
2525
@response_status = 200
26-
@response_body = "stub"
26+
@response_body = "{}"
2727
@id = 99999
2828

2929
stub_request(:get, "https://api.getdrip.com/v2/12345/broadcasts/#{@id}").
3030
to_return(status: @response_status, body: @response_body, headers: {})
3131
end
3232

3333
should "send the correct request" do
34-
expected = Drip::Response.new(@response_status, @response_body)
34+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
3535
assert_equal expected, @client.broadcast(@id)
3636
end
3737
end

test/drip/client/campaign_subscriptions_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ def setup
88
context "#campaign_subscriptions" do
99
setup do
1010
@response_status = 200
11-
@response_body = "stub"
11+
@response_body = "{}"
1212
@subscriber_id = "abc123"
1313

1414
stub_request(:get, "https://api.getdrip.com/v2/12345/subscribers/#{@subscriber_id}/campaign_subscriptions").
1515
to_return(status: @response_status, body: @response_body, headers: {})
1616
end
1717

1818
should "send the right request" do
19-
expected = Drip::Response.new(@response_status, @response_body)
19+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
2020
assert_equal expected, @client.campaign_subscriptions(@subscriber_id)
2121
end
2222
end

test/drip/client/campaigns_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ def setup
88
context "#campaigns" do
99
setup do
1010
@response_status = 200
11-
@response_body = "stub"
11+
@response_body = "{}"
1212

1313
stub_request(:get, "https://api.getdrip.com/v2/12345/campaigns").
1414
to_return(status: @response_status, body: @response_body, headers: {})
1515
end
1616

1717
should "send the right request" do
18-
expected = Drip::Response.new(@response_status, @response_body)
18+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
1919
assert_equal expected, @client.campaigns
2020
end
2121
end
2222

2323
context "#campaign" do
2424
setup do
2525
@response_status = 200
26-
@response_body = "stub"
26+
@response_body = "{}"
2727
@id = 9999999
2828

2929
stub_request(:get, "https://api.getdrip.com/v2/12345/campaigns/#{@id}").
3030
to_return(status: @response_status, body: @response_body, headers: {})
3131
end
3232

3333
should "send the right request" do
34-
expected = Drip::Response.new(@response_status, @response_body)
34+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
3535
assert_equal expected, @client.campaign(@id)
3636
end
3737
end
@@ -71,15 +71,15 @@ def setup
7171
context "#campaign_subscribers" do
7272
setup do
7373
@response_status = 200
74-
@response_body = "stub"
74+
@response_body = "{}"
7575
@id = 9999999
7676

7777
stub_request(:get, "https://api.getdrip.com/v2/12345/campaigns/#{@id}/subscribers").
7878
to_return(status: @response_status, body: @response_body, headers: {})
7979
end
8080

8181
should "send the right request" do
82-
expected = Drip::Response.new(@response_status, @response_body)
82+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
8383
assert_equal expected, @client.campaign_subscribers(@id)
8484
end
8585
end

test/drip/client/conversions_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ def setup
88
context "#conversions" do
99
setup do
1010
@response_status = 200
11-
@response_body = "stub"
11+
@response_body = "{}"
1212

1313
stub_request(:get, "https://api.getdrip.com/v2/12345/goals").
1414
to_return(status: @response_status, body: @response_body, headers: {})
1515
end
1616

1717
should "send the right request" do
18-
expected = Drip::Response.new(@response_status, @response_body)
18+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
1919
assert_equal expected, @client.conversions
2020
end
2121
end
2222

2323
context "#conversion" do
2424
setup do
2525
@response_status = 200
26-
@response_body = "stub"
26+
@response_body = "{}"
2727
@id = 9999999
2828

2929
stub_request(:get, "https://api.getdrip.com/v2/12345/goals/#{@id}").
3030
to_return(status: @response_status, body: @response_body, headers: {})
3131
end
3232

3333
should "send the right request" do
34-
expected = Drip::Response.new(@response_status, @response_body)
34+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
3535
assert_equal expected, @client.conversion(@id)
3636
end
3737
end

test/drip/client/custom_fields_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ def setup
88
context "#custom_fields" do
99
setup do
1010
@response_status = 200
11-
@response_body = "stub"
11+
@response_body = "{}"
1212

1313
stub_request(:get, "https://api.getdrip.com/v2/12345/custom_field_identifiers").
1414
to_return(status: @response_status, body: @response_body, headers: {})
1515
end
1616

1717
should "send the right request" do
18-
expected = Drip::Response.new(@response_status, @response_body)
18+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
1919
assert_equal expected, @client.custom_fields
2020
end
2121
end

test/drip/client/events_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ def setup
2323
}.to_json
2424

2525
@response_status = 201
26-
@response_body = "stub"
26+
@response_body = "{}"
2727

2828
stub_request(:post, "https://api.getdrip.com/v2/12345/events").
2929
to_return(status: @response_status, body: @response_body, headers: {})
3030
end
3131

3232
should "send the right request" do
33-
expected = Drip::Response.new(@response_status, @response_body)
33+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
3434
assert_equal expected, @client.track_event(@email, @action, @properties)
3535
end
3636
end
@@ -50,14 +50,14 @@ def setup
5050
}.to_json
5151

5252
@response_status = 201
53-
@response_body = "stub"
53+
@response_body = "{}"
5454

5555
stub_request(:post, "https://api.getdrip.com/v2/12345/events").
5656
to_return(status: @response_status, body: @response_body, headers: {})
5757
end
5858

5959
should "send the right request" do
60-
expected = Drip::Response.new(@response_status, @response_body)
60+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
6161
assert_equal expected, @client.track_event(@email, @action, @properties, @options)
6262
end
6363
end
@@ -78,29 +78,29 @@ def setup
7878

7979
@payload = { "batches" => [{ "events" => @events }] }.to_json
8080
@response_status = 201
81-
@response_body = "stub"
81+
@response_body = "{}"
8282

8383
stub_request(:post, "https://api.getdrip.com/v2/12345/events/batches").
8484
to_return(status: @response_status, body: @response_body, headers: {})
8585
end
8686

8787
should "send the right request" do
88-
expected = Drip::Response.new(@response_status, @response_body)
88+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
8989
assert_equal expected, @client.track_events(@events)
9090
end
9191
end
9292

9393
context "#event_actions" do
9494
setup do
9595
@response_status = 200
96-
@response_body = "stub"
96+
@response_body = "{}"
9797

9898
stub_request(:get, "https://api.getdrip.com/v2/12345/event_actions").
9999
to_return(status: @response_status, body: @response_body, headers: {})
100100
end
101101

102102
should "send the right request" do
103-
expected = Drip::Response.new(@response_status, @response_body)
103+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
104104
assert_equal expected, @client.event_actions
105105
end
106106
end

test/drip/client/forms_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ def setup
88
context "#forms" do
99
setup do
1010
@response_status = 200
11-
@response_body = "stub"
11+
@response_body = "{}"
1212

1313
stub_request(:get, "https://api.getdrip.com/v2/12345/forms").
1414
to_return(status: @response_status, body: @response_body, headers: {})
1515
end
1616

1717
should "send the right request" do
18-
expected = Drip::Response.new(@response_status, @response_body)
18+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
1919
assert_equal expected, @client.forms
2020
end
2121
end
2222

2323
context "#form" do
2424
setup do
2525
@response_status = 200
26-
@response_body = "stub"
26+
@response_body = "{}"
2727
@id = 9999999
2828

2929
stub_request(:get, "https://api.getdrip.com/v2/12345/forms/#{@id}").
3030
to_return(status: @response_status, body: @response_body, headers: {})
3131
end
3232

3333
should "send the right request" do
34-
expected = Drip::Response.new(@response_status, @response_body)
34+
expected = Drip::Response.new(@response_status, JSON.parse(@response_body))
3535
assert_equal expected, @client.form(@id)
3636
end
3737
end

0 commit comments

Comments
 (0)