diff --git a/lib/clickatell/api/error.rb b/lib/clickatell/api/error.rb index 70f6ea4..c90102b 100644 --- a/lib/clickatell/api/error.rb +++ b/lib/clickatell/api/error.rb @@ -15,9 +15,11 @@ def initialize(code, message) # Error.parse("ERR: 001, Authentication error") # # => # def self.parse(error_string) - error_details = error_string.split(':').last.strip - code, message = error_details.split(',').map { |s| s.strip } - self.new(code, message) + if error_string =~ /^ERR: (\d+), (.*)$/ + self.new($1, $2) + else + self.new(nil, error_string) + end end end diff --git a/lib/clickatell/response.rb b/lib/clickatell/response.rb index c393fc6..e208325 100644 --- a/lib/clickatell/response.rb +++ b/lib/clickatell/response.rb @@ -12,10 +12,12 @@ class << self def parse(http_response) return { 'OK' => 'session_id' } if API.test_mode - if http_response.body.scan(/ERR/).any? - raise Clickatell::API::Error.parse(http_response.body) + lines = http_response.body.split("\n").reject {|line| line.strip.size == 0 } + + if lines.size == 1 && lines.first =~ /^ERR:/ + raise Clickatell::API::Error.parse(lines.first) end - results = http_response.body.split("\n").map do |line| + results = lines.map do |line| # YAML.load converts integer strings that have leading zeroes into integers # using octal rather than decimal. This isn't what we want, so we'll strip out any # leading zeroes in numbers here.