Skip to content

Latest commit

 

History

History
92 lines (68 loc) · 3.67 KB

README.md

File metadata and controls

92 lines (68 loc) · 3.67 KB

LiveOak UserAgent Example

The UserAgent example retrieves the useragent of the device accessing the site and stores how many times that particular useragent has accessed the site.

Features

  • How to use the client to read, query, write and update other LiveOak resources.

  • How to retrieve the headers of the client accessing the site.

Target

The target specified in the metadata.json file is set for every access under the /useragentscript/app path which currently only contains the /useragentscript/app/index.html file.

"target-path" : "/useragentscript/app/**",

Running the Example

The target for this example is setup to be applied to anything under the /useragentscript/app/ path. There is only the index.html resource available here.

To try out this example:

Script

// The name of this application
var APPLICATION_NAME = "useragentscript";
// The resource path to the storage resource
var STORAGE_RESOURCE = "/" + APPLICATION_NAME + "/storage";
// The resource path to the storage collection
var USERAGENT_COLLECTION = STORAGE_RESOURCE + "/useragents";

function postRead(response, libraries) {
    // get the client from the library object. The client allows for accessing other resources in LiveOak
    var client = libraries.client;

    //if the useragents collection does not exist, then create it
    var useragentsCollection = client.read(USERAGENT_COLLECTION);
    if (useragentsCollection == null) {
       var useragentsCollection = new liveoak.Resource('useragents');
       client.create(STORAGE_RESOURCE, useragentsCollection);
    }

    // The attributes from the request. This contains the http headers which contain the useragent string
    var attributes = response.request.context.attributes;

    var useragent;
    // check if we have any http headers, if not, then leave the useragent as undefined
    if (attributes != null && attributes.HTTP_REQUEST != null) {
       // get the useragent from the http headers.
       useragent = attributes.HTTP_REQUEST.headers["User-Agent"];
    }

    // if the useragent collection doesn't exist, then create one
    var useragentCollection = client.read(USERAGENT_COLLECTION);
    if (useragentCollection == null) {
      var useragentResource = new liveOak.Resource("useragents");
      client.create(STORAGE_RESOURCE, useragentResource);
    }

    // check if we already have this useragent in the colletion.
    var resources = client.read(USERAGENT_COLLECTION, { "q": "{'useragent': '"+ useragent + "'}", "fields": "*(*)"});
    // if we do already have this useragent, then update its count number
    if (resources.members != null && resources.members.size() > 0) {
      var resource = resources.members.get(0);
      resource.properties.count = resource.properties.count + 1;
      client.update(USERAGENT_COLLECTION + "/" + resource.id, resource);
    } else {
      var resource = new liveoak.Resource();
      resource.properties = { "useragent" : useragent, "count": 1};
      client.create(USERAGENT_COLLECTION, resource);
    }
}