Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error if Help Scout returns validation errors #1

Merged
merged 5 commits into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions help_scout.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
spec.authors = ["Dennis Paagman", "Miriam Tocino", "Mark Mulder"]
spec.email = ["[email protected]", "[email protected]", "[email protected]"]

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

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

spec.add_runtime_dependency "httparty", "~> 0.13"
spec.add_runtime_dependency "httparty", "~> 0.14"

spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "webmock", "~> 2.0"
spec.add_development_dependency "rake", "~> 10.5"
spec.add_development_dependency "rspec", "~> 3.5"
spec.add_development_dependency "webmock", "~> 2.1"
spec.add_development_dependency "byebug"
end
19 changes: 16 additions & 3 deletions lib/help_scout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
require "httparty"

class HelpScout
class ValidationError < StandardError; end
class NotImplementedError < StandardError; end

HTTP_CREATED = 201
HTTP_BAD_REQUEST = 400

attr_accessor :last_response

def initialize(api_key)
Expand All @@ -18,9 +24,16 @@ def initialize(api_key)
def create_conversation(data)
post("conversations", { body: data })

# Extract ID of created conversation from the Location header
conversation_uri = last_response.headers["location"]
conversation_uri.match(/(\d+)\.json$/)[1]
if last_response.code == HTTP_CREATED
# Extract ID of created conversation from the Location header
conversation_uri = last_response.headers["location"]
return conversation_uri.match(/(\d+)\.json$/)[1]
elsif last_response.code == HTTP_BAD_REQUEST
# Validation failed so return the errors
raise ValidationError, last_response.parsed_response["message"]
else
raise NotImplementedError, "Help Scout returned something that is not implemented by the help_scout gem yet. Sorry."
end
end

# Public: Get conversation
Expand Down
2 changes: 1 addition & 1 deletion lib/help_scout/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class HelpScout
VERSION = "0.1.0"
VERSION = "0.2.0"
end
32 changes: 32 additions & 0 deletions spec/helpscout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@
)
expect(client.create_conversation(data)).to eq '123'
end

context 'with invalid input' do
it 'returns validation errors' do
data = { subject: "Help me!", customer: { email: "" } }

url = 'https://api.helpscout.net/v1/conversations.json'
stub_request(:post, url).
to_return(
status: 400,
headers: {
'Content-Type' => 'application/json'
},
body: { error: "Input could not be validated", message: "Email is not valid" }.to_json
)

expect { client.create_conversation(data) }.to raise_error(HelpScout::ValidationError, "Email is not valid")
end
end

context 'with a not implemented status code' do
it 'returns a not implemented error' do
data = { subject: "Help me!" }

url = 'https://api.helpscout.net/v1/conversations.json'
stub_request(:post, url).
to_return(
status: 500,
)

expect { client.create_conversation(data) }.to raise_error(HelpScout::NotImplementedError)
end
end
end

describe '#search_conversations' do
Expand Down