Class: Data247

Inherits:
Object
  • Object
show all
Defined in:
lib/data247.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Data247

Returns a new instance of Data247.



8
9
10
11
12
# File 'lib/data247.rb', line 8

def initialize(attributes={})
  attributes.each do |key, value|
    self.send("#{key}=", value)
  end
end

Class Attribute Details

.passwordObject

Returns the value of attribute password.



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

def password
  @password
end

.retriesObject

Returns the value of attribute retries.



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

def retries
  @retries
end

.timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

.usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Instance Attribute Details

#msisdnObject

Returns the value of attribute msisdn.



6
7
8
# File 'lib/data247.rb', line 6

def msisdn
  @msisdn
end

#operator_codeObject

Returns the value of attribute operator_code.



6
7
8
# File 'lib/data247.rb', line 6

def operator_code
  @operator_code
end

#operator_nameObject

Returns the value of attribute operator_name.



6
7
8
# File 'lib/data247.rb', line 6

def operator_name
  @operator_name
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/data247.rb', line 6

def status
  @status
end

Class Method Details

.detect(msisdn) ⇒ Object

Attempt an operator lookup based on msisdn.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/data247.rb', line 34

def detect(msisdn)
  attempts = 0
  while attempts <= self.retries
    attempts += 1
    begin
      Timeout::timeout(self.timeout) do
        data=Hash.from_xml(open("http://api.data24-7.com/carrier.php?username=#{self.username}&password=#{self.password}&p1=#{msisdn}").read)["response"]["results"]["result"]
        status=data["status"].to_s.strip
        operator_name=data["carrier_name"].to_s.strip
        operator_code=data["carrier_id"].to_s.strip
        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

.setup_fakeweb_response(options = {}) ⇒ Object

When using FakeWeb for testing (which you should), use this to setup a fake response that returns the data you want.

Required parameters:

  • :msisdn

Optional parameters:

  • :status (defaults to “OK”)

  • :username (defaults to Data247.username)

  • :password (defaults to Data247.password)

  • :operator (defaults to “T-Mobile”)

  • :result (operator code, defaults to “123345”, the T-Mobile operator code)

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/data247.rb', line 68

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]
  options[:status]  ||= "OK"
  options[:username]||= self.username
  options[:password]||= self.password
  options[:operator] ||= "T-Mobile"
  options[:result] ||= "123345"
  FakeWeb.register_uri :get, "http://api.data24-7.com/carrier.php?username=#{options[:username]}&password=#{options[:password]}&p1=#{options[:msisdn]}", :body=> <<-MSG
<?xml version="1.0"?><response><results><result item="1"><status>#{options[:status]}</status><number>#{options[:msisdn]}</number><wless>y</wless><carrier_name>#{options[:operator]}</carrier_name><carrier_id>#{options[:result]}</carrier_id><sms_address>#{options[:msisdn]}@tmomail.net</sms_address><mms_address>#{options[:msisdn]}@tmomail.net</mms_address></result></results><balance>21.5000</balance></response>
MSG
end

Instance Method Details

#==(other) ⇒ Object



82
83
84
85
86
87
# File 'lib/data247.rb', line 82

def ==(other)
  [:operator_code, :msisdn, :status].each do |attribute|
    return false unless self.send(attribute) == other.send(attribute)
  end
  true
end