Class: Comcetera

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

.passwordObject

Returns the value of attribute password.



24
25
26
# File 'lib/comcetera.rb', line 24

def password
  @password
end

.retriesObject

Returns the value of attribute retries.



24
25
26
# File 'lib/comcetera.rb', line 24

def retries
  @retries
end

.timeoutObject

Returns the value of attribute timeout.



24
25
26
# File 'lib/comcetera.rb', line 24

def timeout
  @timeout
end

.usernameObject

Returns the value of attribute username.



24
25
26
# File 'lib/comcetera.rb', line 24

def username
  @username
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



15
16
17
# File 'lib/comcetera.rb', line 15

def debug
  @debug
end

#error_codeObject

Returns the value of attribute error_code.



15
16
17
# File 'lib/comcetera.rb', line 15

def error_code
  @error_code
end

#error_messageObject

Returns the value of attribute error_message.



15
16
17
# File 'lib/comcetera.rb', line 15

def error_message
  @error_message
end

#msisdnObject

Returns the value of attribute msisdn.



14
15
16
# File 'lib/comcetera.rb', line 14

def msisdn
  @msisdn
end

#operator_codeObject

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

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/comcetera.rb', line 64

def setup_fakeweb_response(options={})
  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 options[:msisdn]
  raise ArgumentError.new("Option missing: :result") unless options[:result]
  options[:username]||= self.username
  options[:password]||= self.password
  FakeWeb.register_uri :get, "http://api.comcetera.com/npl?user=#{options[:username]}&pass=#{options[:password]}&msisdn=#{options[:msisdn]}", :body=> <<-MSG
QUERYOK
#{options[:msisdn]} #{options[: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