Created
March 30, 2014 22:21
-
-
Save nclzz/9880901 to your computer and use it in GitHub Desktop.
Moz API Url Metrics using Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MOZ API http://apiwiki.moz.com/categories/api-reference | |
require 'openssl' | |
require 'base64' | |
require 'cgi' | |
require 'open-uri' | |
# You can obtain you access id and secret key here: http://moz.com/products/api/keys | |
ACCESS_ID = "" | |
SECRET_KEY = "" | |
# Set your expires for several minutes into the future. | |
# Values excessively far in the future will not be honored by the Mozscape API. | |
expires = Time.now.to_i + 300 | |
# A new linefeed is necessary between your AccessID and Expires. | |
string_to_sign = "#{ACCESS_ID}\n#{expires}" | |
# Get the "raw" or binary output of the hmac hash. | |
binary_signature = OpenSSL::HMAC.digest('sha1', SECRET_KEY, string_to_sign) | |
# We need to base64-encode it and then url-encode that. | |
URL_SAFE_SIGNATURE = CGI::escape(Base64.encode64(binary_signature).chomp) | |
# This is the URL that we want metrics for. | |
object_url = 'www.wanderio.com' | |
#Define witch metric you wanto to obtain | |
# Add up all the bit flags you want returned. | |
# Learn more here: http://apiwiki.moz.com/query-parameters/ | |
cols = '5' | |
# Now put your entire request together. | |
# This example uses the Mozscape URL Metrics API. | |
request_url = "http://lsapi.seomoz.com/linkscape/url-metrics/#{object_url}?Cols=#{cols}&AccessID=#{ACCESS_ID}&Expires=#{expires}&Signature=#{URL_SAFE_SIGNATURE}" | |
# Go and fetch the URL | |
response = open(request_url).read | |
# "response" is now the JSON string returned fron the API | |
puts response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment