Class: Telein::Client
- Inherits:
-
Object
- Object
- Telein::Client
- Defined in:
- lib/telein/client.rb
Overview
Class used to query Telein servers for carrier codes
No API key given
The client doesn't make any request if the API key is missing, returning a 999 code immediately.
Invalid phones
Telein can tell if a given phone is invalid but each request is a credit spent so the class also encapsulates the code from Telein::Util::Phone to detect invalid ones preemptively.
All servers down
If all Registered servers are down, then the client return 101 for carrier_code
Instance Method Summary collapse
-
#carrier_code_for(value) ⇒ Integer
Queries the Telein servers for the carrier code of value.
Instance Method Details
#carrier_code_for(value) ⇒ Integer
Queries the Telein servers for the carrier code of value
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/telein/client.rb', line 33 def carrier_code_for(value) return 999 unless Telein.api_key phone = Telein::Util::Phone.new(value) if phone.valid? Telein.servers.each do |server| begin curl = Curl::Easy.new do |easy| easy.url = server.query_url_for(phone.to_telein_s) end # client request curl.http_get # telein response response = curl.body_str # response parsing (carrier#number) carrier_code, _ = response.split('#') return 101 if carrier_code.to_i == 0 return carrier_code.to_i rescue next end end # all servers down return 101 else # invalid phone return 100 end end |