require 'open-uri' require 'timeout' require 'active_support' class Data247 attr_accessor :operator_name, :operator_code, :msisdn, :status def initialize(attributes={}) attributes.each do |key, value| self.send("#{key}=", value) end end class << self attr_accessor :username, :password, :timeout, :retries def timeout @timeout ||= 2 end def retries @retries ||= 2 end def password @password || raise("No password set for Data24-7") end def username @username || raise("No username set for Data24-7") end # Attempt an operator lookup based on msisdn. def detect(msisdn) attempts = 0 while attempts <= self.retries attempts += 1 begin Timeout::timeout(self.timeout) do data=Hash.from_xml(open("https://api.data24-7.com/carrier.php?username=#{self.username}&password=#{self.password}&p1=#{msisdn}").read)["response"]["results"]["result"] status=data["status"] operator_name=data["carrier_name"] operator_code=data["carrier_id"] unless status != "OK" return new(:operator_name=> operator_name, :operator_code => operator_code, :msisdn => msisdn, :status=>status) else return new(:operator_code => nil, :msisdn => msisdn, :status=>status) end end rescue Timeout::Error, SystemCallError => e # ignore end end new(:status=>"Timeout from Data24-7") end 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: :status") unless options[:status] options[:username]||= self.username options[:password]||= self.password FakeWeb.register_uri :get, "https://api.data24-7.com/carrier.php?username=#{options[:username]}&password=#{options[:password]}&p1=#{options[:msisdn]}", :body=> <<-MSG #{options[:status]}#{options[:msisdn]}yT-Mobile#{options[:result]}#{options[:msisdn]}@tmomail.net#{options[:msisdn]}@tmomail.net21.5000 MSG end end def ==(other) [:operator_code, :msisdn, :status].each do |attribute| return false unless self.send(attribute) == other.send(attribute) end true end end