Skip to content

Commit cc3386f

Browse files
Merge pull request #1 from Springest/raise_error_on_validation_fail
Raise error if Help Scout returns validation errors
2 parents dac2703 + e520fa2 commit cc3386f

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

help_scout.gemspec

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
99
spec.authors = ["Dennis Paagman", "Miriam Tocino", "Mark Mulder"]
1010
1111

12-
spec.summary = "HelpScout is a an api client for Help Scout"
12+
spec.summary = "HelpScout is an API client for Help Scout"
1313
spec.homepage = "https://github.com/Springest/help_scout"
1414
spec.license = "MIT"
1515

@@ -18,11 +18,11 @@ Gem::Specification.new do |spec|
1818
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
1919
spec.require_paths = ["lib"]
2020

21-
spec.add_runtime_dependency "httparty", "~> 0.13"
21+
spec.add_runtime_dependency "httparty", "~> 0.14"
2222

2323
spec.add_development_dependency "bundler", "~> 1.12"
24-
spec.add_development_dependency "rake", "~> 10.0"
25-
spec.add_development_dependency "rspec", "~> 3.0"
26-
spec.add_development_dependency "webmock", "~> 2.0"
24+
spec.add_development_dependency "rake", "~> 10.5"
25+
spec.add_development_dependency "rspec", "~> 3.5"
26+
spec.add_development_dependency "webmock", "~> 2.1"
2727
spec.add_development_dependency "byebug"
2828
end

lib/help_scout.rb

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
require "httparty"
33

44
class HelpScout
5+
class ValidationError < StandardError; end
6+
class NotImplementedError < StandardError; end
7+
8+
HTTP_CREATED = 201
9+
HTTP_BAD_REQUEST = 400
10+
511
attr_accessor :last_response
612

713
def initialize(api_key)
@@ -18,9 +24,16 @@ def initialize(api_key)
1824
def create_conversation(data)
1925
post("conversations", { body: data })
2026

21-
# Extract ID of created conversation from the Location header
22-
conversation_uri = last_response.headers["location"]
23-
conversation_uri.match(/(\d+)\.json$/)[1]
27+
if last_response.code == HTTP_CREATED
28+
# Extract ID of created conversation from the Location header
29+
conversation_uri = last_response.headers["location"]
30+
return conversation_uri.match(/(\d+)\.json$/)[1]
31+
elsif last_response.code == HTTP_BAD_REQUEST
32+
# Validation failed so return the errors
33+
raise ValidationError, last_response.parsed_response["message"]
34+
else
35+
raise NotImplementedError, "Help Scout returned something that is not implemented by the help_scout gem yet. Sorry."
36+
end
2437
end
2538

2639
# Public: Get conversation

lib/help_scout/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class HelpScout
2-
VERSION = "0.1.0"
2+
VERSION = "0.2.0"
33
end

spec/helpscout_spec.rb

+32
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,38 @@
1818
)
1919
expect(client.create_conversation(data)).to eq '123'
2020
end
21+
22+
context 'with invalid input' do
23+
it 'returns validation errors' do
24+
data = { subject: "Help me!", customer: { email: "" } }
25+
26+
url = 'https://api.helpscout.net/v1/conversations.json'
27+
stub_request(:post, url).
28+
to_return(
29+
status: 400,
30+
headers: {
31+
'Content-Type' => 'application/json'
32+
},
33+
body: { error: "Input could not be validated", message: "Email is not valid" }.to_json
34+
)
35+
36+
expect { client.create_conversation(data) }.to raise_error(HelpScout::ValidationError, "Email is not valid")
37+
end
38+
end
39+
40+
context 'with a not implemented status code' do
41+
it 'returns a not implemented error' do
42+
data = { subject: "Help me!" }
43+
44+
url = 'https://api.helpscout.net/v1/conversations.json'
45+
stub_request(:post, url).
46+
to_return(
47+
status: 500,
48+
)
49+
50+
expect { client.create_conversation(data) }.to raise_error(HelpScout::NotImplementedError)
51+
end
52+
end
2153
end
2254

2355
describe '#search_conversations' do

0 commit comments

Comments
 (0)