lib/comcetera.rb in comcetera-0.2.0 vs lib/comcetera.rb in comcetera-0.3.1

- old
+ new

@@ -1,11 +1,20 @@ require 'open-uri' require 'timeout' class Comcetera + ERROR_CODES={ + "1"=>"Unknown subscriber", + "29"=>"Absent subscriber", + "21"=>"Facility not supported", + "11"=>"Teleserice not provisioned", + "13"=>"Call barred", + "36"=>"System Failure" + } + attr_accessor :operator_code, :msisdn - attr_accessor :error_code, :debug + attr_accessor :error_code, :error_message, :debug def initialize(attributes={}) attributes.each do |key, value| self.send("#{key}=", value) end @@ -19,11 +28,11 @@ end def retries @retries ||= 2 end - + def password @password || raise("No password set for Comcetera") end def username @@ -37,13 +46,13 @@ attempts += 1 begin Timeout::timeout(self.timeout) do body=open("http://api.comcetera.com/npl?user=#{self.username}&pass=#{self.password}&msisdn=#{msisdn}").read msisdn, operator_code = body.split("\n")[1].split(" ") # 2nd line, last word is the operator hexcode - unless operator_code.to_s =~ /ERR\d+/ + unless operator_code.to_s =~ /ERR(\d+)/ return new(:operator_code => operator_code, :msisdn => msisdn) else - return new(:operator_code => nil, :msisdn => msisdn, :error_code=>operator_code, :debug=>body) + return new(:operator_code => nil, :msisdn => msisdn, :error_code=>operator_code, :error_message=>ERROR_CODES[$1]||"Unknown Error", :debug=>body) end end rescue Timeout::Error, SystemCallError => e # ignore end