Class: Data247
- Inherits:
-
Object
- Object
- Data247
- Defined in:
- lib/data247.rb
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
-
#msisdn ⇒ Object
Returns the value of attribute msisdn.
-
#operator_code ⇒ Object
Returns the value of attribute operator_code.
-
#operator_name ⇒ Object
Returns the value of attribute operator_name.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
-
.detect(msisdn) ⇒ Object
Attempt an operator lookup based on msisdn.
-
.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.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(attributes = {}) ⇒ Data247
constructor
A new instance of Data247.
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
.password ⇒ Object
Returns the value of attribute password.
15 16 17 |
# File 'lib/data247.rb', line 15 def password @password end |
.retries ⇒ Object
Returns the value of attribute retries.
15 16 17 |
# File 'lib/data247.rb', line 15 def retries @retries end |
.timeout ⇒ Object
Returns the value of attribute timeout.
15 16 17 |
# File 'lib/data247.rb', line 15 def timeout @timeout end |
.username ⇒ Object
Returns the value of attribute username.
15 16 17 |
# File 'lib/data247.rb', line 15 def username @username end |
Instance Attribute Details
#msisdn ⇒ Object
Returns the value of attribute msisdn.
6 7 8 |
# File 'lib/data247.rb', line 6 def msisdn @msisdn end |
#operator_code ⇒ Object
Returns the value of attribute operator_code.
6 7 8 |
# File 'lib/data247.rb', line 6 def operator_code @operator_code end |
#operator_name ⇒ Object
Returns the value of attribute operator_name.
6 7 8 |
# File 'lib/data247.rb', line 6 def operator_name @operator_name end |
#status ⇒ Object
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)
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/data247.rb', line 68 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] [:status] ||= "OK" [:username]||= self.username [:password]||= self.password [:operator] ||= "T-Mobile" [:result] ||= "123345" FakeWeb.register_uri :get, "http://api.data24-7.com/carrier.php?username=#{[:username]}&password=#{[:password]}&p1=#{[:msisdn]}", :body=> <<-MSG <?xml version="1.0"?><response><results><result item="1"><status>#{[:status]}</status><number>#{[:msisdn]}</number><wless>y</wless><carrier_name>#{[:operator]}</carrier_name><carrier_id>#{[:result]}</carrier_id><sms_address>#{[:msisdn]}@tmomail.net</sms_address><mms_address>#{[: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 |