a59f432b526d655af60895645e96d629948199a5
[rails.git] / lib / quova.rb
1 ##
2 # Load required libraries
3 require 'soap/wsdlDriver'
4
5 ##
6 # Monkey patch WSDL parser to stop it moaning
7 module WSDL
8   class Parser
9     def warn(_msg)
10     end
11   end
12 end
13
14 ##
15 # Provide interface to Quova geolocation service
16 module Quova
17   ##
18   # Access details for WSDL description
19   WSDL_URL = "https://webservices.quova.com/OnDemand/GeoPoint/v1/default.asmx?WSDL"
20   WSDL_USER = QUOVA_USERNAME
21   WSDL_PASS = QUOVA_PASSWORD
22
23   ##
24   # Status codes
25   SUCCESS = 0
26   IPV6_NO_SUPPORT = 1
27   INVALID_CREDENTIALS = 2
28   NOT_MAPPED = 3
29   INVALID_IP_FORMAT = 4
30   IP_ADDRESS_NULL = 5
31   ACCESS_DENIED = 6
32   QUERY_LIMIT = 7
33   OUT_OF_SERVICE = 10
34
35   ##
36   # Create SOAP endpoint
37   @@soap = SOAP::WSDLDriverFactory.new(WSDL_URL).create_rpc_driver
38   @@soap.options["protocol.http.basic_auth"] << [WSDL_URL, WSDL_USER, WSDL_PASS]
39
40   ##
41   # Accessor for SOAP endpoint
42   def self.soap
43     @@soap
44   end
45
46   ##
47   # Class representing geolocation details for an IP address
48   class IpInfo
49     def initialize(ip_address)
50       @ipinfo = Quova.soap.GetIpInfo(:ipAddress => ip_address)
51     end
52
53     def status
54       @ipinfo["GetIpInfoResult"]["Response"]["Status"].to_i
55     end
56
57     def country_code
58       @ipinfo["GetIpInfoResult"]["Location"]["Country"]["Name"]
59     end
60
61     def country_confidence
62       @ipinfo["GetIpInfoResult"]["Location"]["Country"]["Confidence"]
63     end
64   end
65 end