Coder Social home page Coder Social logo

comscore_ruby's Introduction

comscore_ruby

what is it

comscore_ruby is a minimal Ruby wrapper to comScore's SOAP API. It follows a design policy similar to that of sucker built for Amazon's API.

comScore's API is closed, you have to be a paying customer in order to access the data.

installation

[sudo] gem install comscore_ruby

initialization and authentication

comScore uses basic HTTP authentication so just provide your regular login details.

client = ComScore::Client.new(
  username,
  password,
  :log => false,
  :wait_time => 1
)

usage

There are only two core methods for the client which doesn't try to "over architect a spaghetti API":

  • get_report - used to...while get reports and
  • request - more generic used to make any kind of request

For reference, I'd recommend keeping comScore's SOAP API open as you code to understand what services you have available to you. Also ask them for their initial documentation which is absolutely horrible but at least their engineering staff is pretty helpful.

The response returned by either of these requests is actually a savon request object to allow for maximum flexibility. You can do things with this that are particularly nice:

  • response.to_hash which works well in most cases but has been known to be buggy with XML attributes (at least in Ruby 1.8.x)
  • response.doc the Nokogiri XML document to allow for some pretty advanced XPATH

9x out of 10 you'll be able to use to_hash on a response but you will sometimes have need to query with XPATH due to how to_hash parses certain XML attributes in comScore's results.

examples

# Find all media within the Canadian dictionary (from 2 months ago) that match "The Globe And Mail"
client.request(:key_measures, :fetch_media) do |xml|
  xml.parameterId("media")
  xml.fetchMediaQuery("xmlns" => "http://comscore.com/FetchMedia") {
    xml.SearchCritera(:ExactMatch => "false", :Critera => "The Globe And Mail")
  }
  xml.reportQuery("xmlns" => "http://comscore.com/ReportQuery") {
    xml.Parameter(:KeyId => "geo", :Value => ComScore::GEOGRAPHIES["Canada"])
    xml.Parameter(:KeyId => "loc", :Value => ComScore::LOCATIONS["Home and Work"])
    xml.Parameter(:KeyId => "timeType", :Value => ComScore::TIME_TYPES["Months"])
    xml.Parameter(:KeyId => "timePeriod", :Value => DateTime.now.to_date.to_comscore_time_period - 2)
    xml.Parameter(:KeyId => "mediaSetType", :Value => ComScore::MEDIA_SET_TYPES["Ranked Categories"])
  }
end

# Find all the categories (or media sets) within the Canadian dictionary (from 2 months ago)
client.request(:key_measures, :discover_parameter_values) do |xml|
  xml.parameterId("mediaSet")
  xml.query(:xmlns => "http://comscore.com/ReportQuery") {
    xml.Parameter(:KeyId => "geo", :Value => ComScore::GEOGRAPHIES["Canada"])
    xml.Parameter(:KeyId => "timeType", :Value => ComScore::TIME_TYPES["Months"])
    xml.Parameter(:KeyId => "timePeriod", :Value => DateTime.now.to_date.to_comscore_time_period - 2)
    xml.Parameter(:KeyId => "mediaSetType", :Value => ComScore::MEDIA_SET_TYPES["Ranked Categories"])
  }
end

# Get a key measures report for the Community - Lifestyles category (778226) for August, 2011
client.get_report(:key_measures,
  :geo => ComScore::GEOGRAPHIES["Canada"],
  :loc => ComScore::LOCATIONS["Home and Work"],
  :timeType => ComScore::TIME_TYPES["Months"],
  :timePeriod => Date.new(2011, 8),
  :mediaSet => 778226,
  :mediaSetType => ComScore::MEDIA_SET_TYPES["Ranked Categories"],
  :targetType => ComScore::TARGET_TYPES["Simple"],
  :targetGroup => ComScore::TARGET_GROUPS["Total Audience"],
  :measure => [ComScore::MEASURES["Total Unique Visitors (000)"]]
)

todo

  • More support for other parameters: geographies, locations etc. I'm Canadian, so I really only built it for the parameters I use and know of.
  • Passing of a hash to the initializer for config options including username and password so you don't have to awkwardly set logging off after initialization.

see also

My other client library romniture for those of you looking to pull data from Omniture as well.

comscore_ruby's People

Contributors

geronimod avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.