Ruby Quick Start Guide
These examples work with Ruby 1.9.2 and require the JSON library for Ruby. It can be installed with gem install json .
Creating A Link
In this example, the demo tool is creating a link intended to be shared on Twitter that redirects to http://developers.awe.sm/.
require 'rubygems'
require 'json'
require 'net/http'
require 'uri'
create_link_request = Net::HTTP.post_form(
URI.parse('http://api.awe.sm/url.txt'),
{
'v' => 3,
'url' => 'http://developers.awe.sm/',
'key' => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
'tool' => 'mKU7uN',
'channel' => 'twitter'
}
)
new_link = create_link_request.body
puts new_link
Output: http://demo.awe.sm/K7e
Retrieving Stats On A Link
Later, the stats for this link could be retrieved:
require 'rubygems'
require 'json'
require 'net/http'
require 'uri'
link_stats_request = Net::HTTP.post_form(
URI.parse('http://api.awe.sm/stats/demo.awe.sm_K7e.json'),
{
'v' => 3,
'key' => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9'
}
)
link_stats = JSON.parse(link_stats_request.body)
puts link_stats
Output hash:
{"awesm_id"=>"demo.awe.sm_K7e", "start_date"=>nil, "end_date"=>nil, "interval"=>"none", "with_zeros"=>false, "with_metadata"=>false, "with_conversions"=>false, "clicks"=>4}
Retrieving Aggregated Stats
Let's find out how many times http://developers.awe.sm/ has been shared and how much traffic social sharing created.
require 'rubygems'
require 'json'
require 'net/http'
require 'uri'
page_share_stats_request = Net::HTTP.post_form(
URI.parse('http://api.awe.sm/stats/range.json'),
{
'v' => 3,
'key' => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
'original_url' => 'http://developers.awe.sm/'
}
)
page_share_stats = JSON.parse(page_share_stats_request.body)
puts "#{page_share_stats["totals"]["clicks"]} clicks from #{page_share_stats["totals"]["shares"]} shares"
Outputs: 105 clicks from 3 shares
Let's find the top three social networks (service) shared to in September 2011.
require 'rubygems'
require 'json'
require 'net/http'
require 'uri'
service_share_stats_request = Net::HTTP.post_form(
URI.parse('http://api.awe.sm/stats/range.json'),
{
'v' => 3,
'key' => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
'group_by' => 'service',
'sort_type' => 'shares',
'per_page' => 3,
'start_date' => '2011-09-01',
'end_date' => '2011-10-01'
}
)
service_share_stats = JSON.parse(service_share_stats_request.body)
puts "#1: #{service_share_stats["groups"][0]["service"]}"
puts "#2: #{service_share_stats["groups"][1]["service"]}"
puts "#3: #{service_share_stats["groups"][2]["service"]}"
Output:
#1: twitter #2: facebook #3: email
Community Resources
Our friends at Topspin Media have created an awesm Ruby gem.
Last updated 2011-10-05