Digital Sanctum

software development, technology and other square topics

Ruby Technorati API Client

without comments

So as part of a pet project I decided to write a Ruby client for the Technorati API. There are several available requests that can be made and each has it's own options to pass for things like the response format. I should mention there is also a Technorati-Ruby gem available that looks like it was written back in 2004 and weighs in at around 500 lines. Mine weighs in at 130 lines or so but doesn't provide a response parser.

RUBY:
  1. # A Ruby client implementation of the Technorati API.
  2. # Copyright 2007 Shane Witbeck, Digital Sanctum, Inc.
  3. # Website: http://www.digitalsanctum.com
  4.  
  5. require 'net/http'
  6.  
  7. module Technorati
  8.  
  9.   protected
  10.  
  11.   class Api
  12.    
  13.     def execute
  14.       url = get_url     
  15.       resp = Net::HTTP.get_response(URI.parse(url))
  16.       resp.body     
  17.     end
  18.    
  19.     protected
  20.    
  21.     def init(key, context, options=nil)
  22.       @key = key
  23.       @context = context
  24.       @options = options ||= {}
  25.     end
  26.        
  27.     def get_url()
  28.       base_url = "http://api.technorati.com"
  29.       url = "#{base_url}/#{@context}?key=#{@key}"       
  30.       @options.each { |key,val| url <<"&#{key}=#{val}" }
  31.       url
  32.     end
  33.    
  34.     def add_option(key, val)
  35.       @options = @options.merge({key=>val}) unless (val.nil? || val == '')
  36.     end
  37.   end
  38.  
  39.   module TechnoratiFormat
  40.     attr_reader :format   
  41.     def format=(format) add_option(:format, format) end
  42.   end
  43.  
  44.   module TechnoratiCollection
  45.     attr_reader :limit, :start       
  46.     def limit=(limit) add_option(:limit, limit) end   
  47.     def start=(start) add_option(:start, start) end
  48.   end
  49.  
  50.   public
  51.    
  52.   class TopTags <Api     
  53.     include TechnoratiFormat
  54.     include TechnoratiCollection   
  55.     def initialize(key, options=nil)     
  56.       init(key, "toptags", options)
  57.     end
  58.   end
  59.  
  60.   class Cosmos <Api   
  61.     include TechnoratiFormat
  62.     include TechnoratiCollection   
  63.     attr_reader :url, :type, :current, :claim, :highlight   
  64.     def initialize(key, url, options={})     
  65.       init(key, "cosmos", options.merge({:url=>url}))     
  66.     end 
  67.     def url=(url) add_option(:url, url) end   
  68.     def type=(type) add_option(:type, type) end   
  69.     def current=(current) add_option(:current, current) end   
  70.     def claim=(claim) add_option(:claim, claim) end   
  71.     def highlight=(highlight) add_option(:highlight, highlight) end
  72.   end
  73.  
  74.   class Search <Api   
  75.     include TechnoratiFormat
  76.     include TechnoratiCollection   
  77.     attr_accessor :language, :authority   
  78.     def initialize(key, query, options={})
  79.       init(key, "search", options.merge({:query=>query}))     
  80.     end     
  81.     def authority=(authority) add_option(:authority, authority) end   
  82.     def language=(language) add_option(:language, language) end   
  83.     def query=(query) add_option(:query, query) end
  84.   end
  85.  
  86.   class Tag <Api
  87.     include TechnoratiFormat
  88.     include TechnoratiCollection   
  89.     attr_accessor :excerptsize, :topexcerptsize     
  90.     def initialize(key, tag, options={})
  91.       init(key, "tag", options.merge({:tag=>tag}))
  92.     end   
  93.     def excerptsize=(excerptsize) add_option(:excerptsize, excerptsize) end   
  94.     def topexcerptsize=(topexcerptsize) add_option(:topexcerptsize, topexcerptsize) end   
  95.     def tag=(tag) add_option(:tag, tag) end
  96.   end
  97.  
  98.   class DailyCounts <Api
  99.     include TechnoratiFormat   
  100.     attr_reader :days   
  101.     def initialize(key, q, options={})           
  102.       init(key, "dailycounts", options.merge({:q=>q}))
  103.     end       
  104.     def days=(days) add_option(:days, days) end   
  105.     def q=(q) add_option(:q, q) end
  106.   end
  107.  
  108.   class BlogInfo <Api
  109.     include TechnoratiFormat   
  110.     def initialize(key, url, options={})
  111.       init(key, "bloginfo", options.merge({:url=>url}))
  112.     end   
  113.     def url=(url) add_option(:url, url) end
  114.   end
  115.  
  116.   class BlogPostTags <Api   
  117.     attr_accessor :limit   
  118.     def initialize(key, url, options={})
  119.       init(key, "blogposttags", options.merge({:url=>url}))
  120.     end   
  121.     def limit=(limit) add_option(:limit, limit) end
  122.     def url=(url) add_option(:url, url) end
  123.   end
  124.  
  125.   class GetInfo <Api
  126.     include TechnoratiFormat   
  127.     def initialize(key, username, options={})
  128.       init(key, "getinfo", options.merge({:username=>username}))
  129.     end       
  130.     def username=(username) add_option(:username, username) end
  131.   end 
  132. end

Written by Shane

December 9th, 2007 at 7:14 pm

Leave a Reply