class Mobi::Mtld::Da::Carrier::CarrierApi

Copyright
Author

The main class for the Carrier Detection API. Used to load the data file and to perform lookups using IPv4 addresses. For usage in a Web App it is recommended to use the CarrierApiWeb (CarrierApiWeb.php) class so you dont have to pass the client's IP address manually.

Please note that it is advisable to load only a single instance of this class to avoid multiple loadings of the data file.

Example usage:

            ip = '62.40.34.220'
            carrier_api = CarrierApi.new
            carrier_api.load_data_from_file '/path/to/sample.dat'

# get all properties
            props = carrier_api.get_properties ip

            # .... use the properties ....
            if props.has_key?'mobileNetworkOperator'
                    operator_name =  props.get 'mobileNetworkOperator'
                    puts 'mobileNetworkOperator: ' + operator_name
            end

            # get a single property
            mcc_prop = carrier_api.get_property ip, 'mcc'
            if !mcc_prop.nil?
                    puts 'MCC: ' + mcc_prop
            end

Please see the code in the examples directory for additional samples.

Constants

HEADERS_TO_CHECK

A list of HTTP headers to choose the original client IP address from. In addition to these the RemoteAddr (REMOTE_ADDR) is also used as a final fallback.

VERSION

Public Class Methods

new() click to toggle source

Public Instance Methods

get_data_file_creation_date() click to toggle source

Returns the data file creation date in ISO8601 format.

return

the creation date

get_data_file_version() click to toggle source

Returns the version of the data file.

return

the version

get_ip(key_vals) click to toggle source

Get the most suitable IP address from the given key_vals set of HTTP headers. This function checks the headers defined in HEADERS_TO_CHECK.

get_properties(ipv4) click to toggle source

Get the Carrier properties for a given IP address.

ipv4

The IP address to find carrier properties for, or an array of HTTP

header values keyed by HTTP headers names. Both must be given as a string.

return

The found properties or null of no properties found.

get_property(ipv4, property_name) click to toggle source

Try and get a specific property for a given IP address.

Note: if multiple properties are needed for the same IP it is more efficient to call getProperties() once than repeated calls to getProperty().

ipv4

The IP address to find carrier properties for, or an array of HTTP

header values keyed by HTTP headers names. Both must be given as a string.

+property_name

Name of the property to return

return

Property object or nil of no property found

get_property_names() click to toggle source

A set of all the possible property names. The Set contains PropertyName objects that each have a name and an associated data type.

return

Array of possible property names.

is_public_ip(ip) click to toggle source

An IP address is considered public if it is not in any of the following ranges:

1) any local address
   IP:  0

2) a local loopback address
   range:  127/8

3) a site local address i.e. IP is in any of the ranges:
   range:  10/8 
   range:  172.16/12 
   range:  192.168/16

4) a link local address 
   range:  169.254/16
ip

IP address to check

return

True if it is public, false if it is invalid or is not public

load_data_from_file(path) click to toggle source

Load the data file from the provided path. The data file is reloaded every time this method is called.

path

The path on disk to the data file.