From 98322b82a10a200806a531fad6987af17c5abf7a Mon Sep 17 00:00:00 2001 From: Lars Pind Date: Fri, 1 Oct 2010 11:34:26 +0200 Subject: [PATCH 1/3] Parse response more intelligently, and store the entire response --- lib/clickatell/api/error.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/clickatell/api/error.rb b/lib/clickatell/api/error.rb index 70f6ea4..c68ff98 100644 --- a/lib/clickatell/api/error.rb +++ b/lib/clickatell/api/error.rb @@ -3,10 +3,10 @@ class API # Clickatell API Error exception. class Error < StandardError - attr_reader :code, :message + attr_reader :code, :message, :response - def initialize(code, message) - @code, @message = code, message + def initialize(code, message, response) + @code, @message, @response = code, message, response end # Creates a new Error from a Clickatell HTTP response string @@ -14,10 +14,14 @@ 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) + def self.parse(response) + response.body.split("\n").map do |line| + if line =~ /^ERR: (\d+), (.*)$/ + code, message = $1.to_i, $2 + break + end + end + self.new(code, message, response) end end From bd05825403c9c617dc77e19ace0224616e7f16d0 Mon Sep 17 00:00:00 2001 From: Lars Pind Date: Fri, 1 Oct 2010 11:44:42 +0200 Subject: [PATCH 2/3] If you're sending multiple SMS'es, don't raise an exception if there's just one problem, give me the whole response back so I can parse it myself --- lib/clickatell/api/error.rb | 18 ++++++++---------- lib/clickatell/response.rb | 8 +++++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/clickatell/api/error.rb b/lib/clickatell/api/error.rb index c68ff98..c90102b 100644 --- a/lib/clickatell/api/error.rb +++ b/lib/clickatell/api/error.rb @@ -3,10 +3,10 @@ class API # Clickatell API Error exception. class Error < StandardError - attr_reader :code, :message, :response + attr_reader :code, :message - def initialize(code, message, response) - @code, @message, @response = code, message, response + def initialize(code, message) + @code, @message = code, message end # Creates a new Error from a Clickatell HTTP response string @@ -14,14 +14,12 @@ def initialize(code, message, response) # # Error.parse("ERR: 001, Authentication error") # # => # - def self.parse(response) - response.body.split("\n").map do |line| - if line =~ /^ERR: (\d+), (.*)$/ - code, message = $1.to_i, $2 - break - end + def self.parse(error_string) + if error_string =~ /^ERR: (\d+), (.*)$/ + self.new($1, $2) + else + self.new(nil, error_string) end - self.new(code, message, response) end end diff --git a/lib/clickatell/response.rb b/lib/clickatell/response.rb index c393fc6..e20cbd3 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. From 928e9b3001d3ef1a73f6ef4a4857733827e7741c Mon Sep 17 00:00:00 2001 From: Lars Pind Date: Fri, 1 Oct 2010 11:54:20 +0200 Subject: [PATCH 3/3] Typo --- lib/clickatell/response.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/clickatell/response.rb b/lib/clickatell/response.rb index e20cbd3..e208325 100644 --- a/lib/clickatell/response.rb +++ b/lib/clickatell/response.rb @@ -14,7 +14,7 @@ def parse(http_response) lines = http_response.body.split("\n").reject {|line| line.strip.size == 0 } - if lines.size == 1 && lines.first =~ /^ERR:/ && + if lines.size == 1 && lines.first =~ /^ERR:/ raise Clickatell::API::Error.parse(lines.first) end results = lines.map do |line|