Class: Comcetera
- Inherits:
-
Object
- Object
- Comcetera
- Defined in:
- lib/comcetera.rb
Constant Summary collapse
- ERROR_CODES =
{ "1"=>"Unknown subscriber", "29"=>"Absent subscriber", "21"=>"Facility not supported", "11"=>"Teleserice not provisioned", "13"=>"Call barred", "36"=>"System Failure" }
Class Attribute Summary collapse
-
.password ⇒ Object
Returns the value of attribute password.
-
.retries ⇒ Object
Returns the value of attribute retries.
-
.timeout ⇒ Object
Returns the value of attribute timeout.
-
.username ⇒ Object
Returns the value of attribute username.
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#error_code ⇒ Object
Returns the value of attribute error_code.
-
#error_message ⇒ Object
Returns the value of attribute error_message.
-
#msisdn ⇒ Object
Returns the value of attribute msisdn.
-
#operator_code ⇒ Object
Returns the value of attribute operator_code.
Class Method Summary collapse
-
.detect(msisdn) ⇒ Object
Attempt an operator lookup based on msisdn.
- .setup_fakeweb_response(options = {}) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(attributes = {}) ⇒ Comcetera
constructor
A new instance of Comcetera.
Constructor Details
#initialize(attributes = {}) ⇒ Comcetera
Returns a new instance of Comcetera.
17 18 19 20 21 |
# File 'lib/comcetera.rb', line 17 def initialize(attributes={}) attributes.each do |key, value| self.send("#{key}=", value) end end |
Class Attribute Details
.password ⇒ Object
Returns the value of attribute password.
24 25 26 |
# File 'lib/comcetera.rb', line 24 def password @password end |
.retries ⇒ Object
Returns the value of attribute retries.
24 25 26 |
# File 'lib/comcetera.rb', line 24 def retries @retries end |
.timeout ⇒ Object
Returns the value of attribute timeout.
24 25 26 |
# File 'lib/comcetera.rb', line 24 def timeout @timeout end |
.username ⇒ Object
Returns the value of attribute username.
24 25 26 |
# File 'lib/comcetera.rb', line 24 def username @username end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
15 16 17 |
# File 'lib/comcetera.rb', line 15 def debug @debug end |
#error_code ⇒ Object
Returns the value of attribute error_code.
15 16 17 |
# File 'lib/comcetera.rb', line 15 def error_code @error_code end |
#error_message ⇒ Object
Returns the value of attribute error_message.
15 16 17 |
# File 'lib/comcetera.rb', line 15 def @error_message end |
#msisdn ⇒ Object
Returns the value of attribute msisdn.
14 15 16 |
# File 'lib/comcetera.rb', line 14 def msisdn @msisdn end |
#operator_code ⇒ Object
Returns the value of attribute operator_code.
14 15 16 |
# File 'lib/comcetera.rb', line 14 def operator_code @operator_code end |
Class Method Details
.detect(msisdn) ⇒ Object
Attempt an operator lookup based on msisdn.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/comcetera.rb', line 43 def detect(msisdn) attempts = 0 while attempts <= self.retries 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+)/ return new(:operator_code => operator_code, :msisdn => msisdn) else 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 end new(:error_message=>"Timeout from Comcetera", :debug=>"#{self.retries} times no response within #{self.timeout} seconds") end |
.setup_fakeweb_response(options = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/comcetera.rb', line 64 def setup_fakeweb_response(={}) raise "FakeWeb is not defined. Please require 'fakeweb' and make sure the fakeweb rubygem is installed." unless defined?(FakeWeb) raise ArgumentError.new("Option missing: :msisdn") unless [:msisdn] raise ArgumentError.new("Option missing: :result") unless [:result] [:username]||= self.username [:password]||= self.password FakeWeb.register_uri :get, "http://api.comcetera.com/npl?user=#{[:username]}&pass=#{[:password]}&msisdn=#{[:msisdn]}", :body=> <<-MSG QUERYOK #{[:msisdn]} #{[:result]} ENDBATCH MSG end |
Instance Method Details
#==(other) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/comcetera.rb', line 78 def ==(other) [:operator_code, :msisdn, :error_code, :error_message, :debug].each do |attribute| return false unless self.send(attribute) == other.send(attribute) end true end |