The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Net::WURFL::ScientiaMobile - Client for the ScientiaMobile cloud webservice

SYNOPSIS

    use Net::WURFL::ScientiaMobile;
    
    my $scientiamobile = Net::WURFL::ScientiaMobile->new(
        api_key => '...',
    );
    
    # process this HTTP request
    $scientiamobile->detectDevice($env);
    
    # check if the device is mobile
    if ($scientiamobile->getDeviceCapability('ux_full_desktop')) {
        print "This is a desktop browser.";
    }

DESCRIPTION

The WURFL Cloud Service by ScientiaMobile, Inc. is a cloud-based mobile device detection service that can quickly and accurately detect over 500 capabilities of visiting devices. It can differentiate between portable mobile devices, desktop devices, SmartTVs and any other types of devices that have a web browser.

This is the Perl Client for accessing the WURFL Cloud Service, and it requires a free or paid WURFL Cloud account from ScientiaMobile: http://www.scientiamobile.com/cloud

This module analyzes the $env data structure of your incoming HTTP request and extracts the device identifier string(s). It then queries the WURFL Cloud Service or the local cache (if any is configured) to get the device capabilities.

If you use a PSGI-compatible web framework (such as Catalyst, Dancer, Mojo and others), the easiest way to use this client is to apply the Plack::Middleware::WURFL::ScientiaMobile module to your application. It will provide the device capabilities to your request handlers automatically with minimal programming effort.

CONSTRUCTOR

The new constructor accepts the following named arguments.

api_key

Required. The full API key provided by the WURFL Cloud Service.

cache

A Net::WURFL::ScientiaMobile::Cache object (or class name as string). If none is provided, no caching will happen.

http_timeout

The timeout in milliseconds to wait for the WURFL Cloud request to complete. Defaults to 1000.

compression

Boolean flag to enable/disable compression for querying the WURFL Cloud Service. Using compression can increase CPU usage in very high traffic environments, but will decrease network traffic and latency. Defaults to true.

auto_purge

If true, the entire cache (e.g. memcache, etc.) will be cleared if the WURFL Cloud Service has been updated. This option should not be enabled for production use since it will result in a massive cache purge, which will result in higher latency lookups. Defaults to false.

report_interval

The interval in seconds that after which API will report its performance.

wcloud_servers

WURFL Cloud servers to use for uncached requests. The "weight" field can contain any positive number, the weights are relative to each other. Use this if you want to override the built-in server list. For example:

    my $scientiamobile = Net::WURFL::ScientiaMobile->new(
        api_key => '...',
        wcloud_servers => {
            # nickname => [ host => weight ],
            'wurfl_cloud' => [ 'api.wurflcloud.com' => 80 ],
        },
    );

METHODS FOR CAPABILITY DETECTION

detectDevice

    $scientiamobile->detectDevice($env);
    $scientiamobile->detectDevice($env, ['ux_full_desktop', 'brand_name']);

Get the requested capabilities from the WURFL Cloud for the given HTTP Request. If the second argument is not provided, all available capabilities will be fetched.

Refer to the documentation of your web framework to learn how to access $env. For example, Catalyst provides it in $ctx->request->env, Dancer provides it in request->env, Mojo provides it in $self->tx->req->env.

Instead of the $env hashref you can also supply a HTTP::Headers or a Mojo::Headers object. This is handy when you're not running in a PSGI environment and your web server doesn't supply a PSGI-compatible $env hashref (for example, when running ./myapp.pl daemon in a Mojolicious::Lite application. Note that the Dancer built-in web server still provides a PSGI-compatible $env).

getDeviceCapability

    my $is_wireless = $scientiamobile->getDeviceCapability('is_wireless_device');

Returns the value of the requested capability. If the capability does not exist, returns undef.

capabilities

Flat capabilities hashref, thus containing 'key' = 'value'> pairs. Since it is 'flattened', there are no groups in this array, just individual capabilities.

METHODS FOR SERVERS POOL MANAGEMENT

addCloudServer

    $scientiamobile->addCloudServer('wurfl_cloud', 'api.wurflcloud.com', 80);

Adds the specified WURFL Cloud Server. The last argument is the server's weight. It specifies the chances that this server will be chosen over the other servers in the pool. This number is relative to the other servers' weights.

clearServers

    $scientiamobile->clearServers;

Removes the WURFL Cloud Servers.

getWeightedServer

    my $server = $scientiamobile->getWeightedServer;

Uses a weighted-random algorithm to chose a server from the pool. It returns an arrayref whose first argument is the host and the second argument is the weight. You don't need to call this method usually. It is called internally when the client prepares the request to the WURFL Cloud Service.

UTILITY METHODS

getUserAgent

    my $user_agent = $scientiamobile->getUserAgent($env);

Reads the user agent string from $env.

getLoadedDate

    my $date = $scientiamobile->getLoadedDate;

Get the date that the WURFL Cloud Server was last updated as a UNIX timestamp (seconds since Epoch). This will be undef if there has not been a recent query to the server, or if the cached value was pushed out of memory.

SEE ALSO

Net::WURFL::ScientiaMobile::Cache, Plack::Middleware::WURFL::ScientiaMobile

AUTHOR

Alessandro Ranellucci <aar@cpan.org>

COPYRIGHT & LICENSE

Copyright 2012, ScientiaMobile, Inc.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.