Class Technorati
In: lib/technorati.rb  (CVS)
examples/referrer.rb  (CVS)
Parent: Object

Ruby interface for the Technorati API.

Note: In order to use this library you’ll need an API key from the Technorati developers page.

Using this class is straighforward; you create a new instance of this class with your API key, then query Technorati with the URL you want to learn about, like so:

  # read API key from ~/.technorati_key
  api_key_path = File.expand_path('~/.technorati_key')
  api_key = File.read(api_key_path).strip

  # create a new Technorati instance
  tr = Technorati.new(api_key)

  # fetch information about links to my page
  cosmos = tr.cosmos('http://pablotron.org/')

  # print out the name and URL of the first link
  puts %w{name link}.map { |key|
    val = cosmos['items'][0][key]
    "#{key}: #{val}"
  }

Each of the query methods allows you to (optionally) refine your search by passing an hash of additional parameters as the second parameter. For example, to limit the cosmos query above to 10 results of type ‘weblog’, you would write it like this:

  # define cosmos query options
  cosmos_opts = {
    'limit' => 10,        # limit to 10 results
    'type'  => 'weblog',  # results of type 'weblog'
  }

  # fetch information about links to my page
  cosmos = tr.cosmos('http://pablotron.org/', cosmos_opts)

See the documentation for each method for additional information on the available options.

By the way, all of the hashes returned by class have just in time convenience methods for their keys. What does this mean? Instead of writing this:

  results['items'][0]['excerpt']

You can write it in a more Ruby-esque way, like so:

  results.items.first.excerpt

Methods

blog_info   blog_post_tags   bloginfo   cosmos   info   key_info   load_key   new   search   tag   top_tags  

Classes and Modules

Class Technorati::APIError
Class Technorati::Error
Class Technorati::HTTPError
Class Technorati::Referrer
Class Technorati::URLError

Constants

VERSION = '0.2.0'   Release version.
DEFAULTS = { 'api_url' => 'http://api.technorati.com/', 'user_agent' => "Technorati-Ruby/#{Technorati::VERSION} Ruby/#{RUBY_VERSION}", }   Default options for Technorati.new.
PROXY_ENV_VARS = %w{TECHNORATI_HTTP_PROXY HTTP_PROXY http_proxy}   Environment variables to check for HTTP proxy

Public Class methods

Load Technorati key.

Attempts to load Technorati key from given path. If a path isn’t given, this method checks ENV[‘TECHNORATI_KEY’], and if that isn’t specified, defaults to ’~/.technorati_key’.

Example:

  key = Technorati.load_key

Connect to Technorati with key key

Note: Will not raise an # exception until you make an actual call. You can get a key from the Technorati developers page.

Example:

  # read key from $HOME/.technorati_key
  api_key = Technorati.load_key

  # use key to connect to technorati
  t = Technorati.new(api_key)

Public Instance methods

Return information about the blog associated with a given URL. See technorati.com/developers/api/bloginfo.html for additional information.

Note: This method has changed since 0.1.x. Version 0.1.0 had a method named Technorati#bloginfo that accepted an optional second parameter which was silently ignored. The current version has been renamed Technorati#blog_info , and does not accept a second parameter.

Parameters:

  • url (required): URL to search for (‘http://’ and ‘www’ prefix are optional).

Returns a hash containing information about the query URL.

Valid Return Keys:

  • ‘url’: Blog URL.
  • ‘weblog/name’: Name of blog.
  • ‘weblog/url: URL of blog.
  • ‘weblog/rssurl’: RSS URL of blog.
  • ‘weblog/atomurl’: Atom URL of blog.
  • ‘weblog/inboundblogs’: Number of inbound blogs of blog.
  • ‘weblog/inboundlinks’: Number of inbound links of blog.
  • ‘weblog/lastupdate’: Date blog was last updated.
  • ‘weblog/rank’: Blog cosmos rank.
  • ‘weblog/lang’: Language of this blog.
  • ‘weblog/foafurl’: FOAF (Friend of a Friend) URL for this blog.
  • ‘inboundblogs’: Inbound blogs.
  • ‘inboundlinks’: Inbound links.

Raises Technorati::Error on error.

Example:

  # print out the Name, URL, and RSS URL for atrios.blogspot.com
  result = t.bloginfo('atrios.blogspot.com')
  blog_keys = { 'Name' => 'name', 'URL' =>'url', 'RSS' => 'rssurl' }
  puts blog_keys.map { |ary| "#{ary[0]}: #{result["weblog/#{ary[1]}"]}" }

Return top tags for posts on the given blog. See technorati.com/developers/api/blogposttags.html for additional information.

Parameters:

  • url (required): URL to search for (‘http://’ and ‘www’ prefix are optional).

Any additional arguments are optional and may be passed as a hash. Here’s a description of each optional argument:

Optional Arguments:

  • limit: Set this to a number larger than 0 and smaller or equal to 100 and it will return limit number of tags for the given blog. By default this value is 10.

Returns a hash containing information about the query URL.

Valid Return Keys:

  • ‘tag’: Tag value.
  • ‘posts’: Number of posts matching given tag.

Raises Technorati::Error on error.

Example:

  # print the top 10 tags for the site 'linuxbrit.co.uk':
  site = 'linuxbrit.co.uk'
  puts tr.blog_post_tags(site)['items'].map { |item|
    "#{item['tag']} (#{item['posts']})"
  }
bloginfo(url)

Alias for blog_info

Get a list of sites linking to the given URL. See technorati.com/developers/api/cosmos.html for additional information.

Note: This method has changed since 0.1.x. It takes a hash of optional arguments as the second parameter. Calling it with the old 0.1-style parameters will work, but is deprecated, will print a warning, and may stop working in a future release.

Required Parameters:

  • url: URL you are searching for. The ‘http://’ prefix is optional.

Any additional arguments are optional and may be passed as a hash. Here’s a description of each optional argument:

Optional Arguments:

  • limit: Set this to a number larger than 0 and smaller or equal to 100 and it will return limit number of links for a query. By default this value is 20.
  • type: Set this to ‘link’ and you’ll get the freshest links to your target URL. Set it to ‘weblog’ and you’ll get a reverse blogroll - the last set of blogs that linked to the target URL.
  • start: Set this to a number larger than 0 and you’ll get the start + limit freshest items (links or blogs), e.g. set it to limit + 1, and you’ll get the second page of rankings.
  • current: By default, cosmos returns the links that are currently on the source’s index page. If you set current to false, you will have all links to the given URL.
  • claim: Set to true to include Technorati member data in the result set when a blog has been successfully claimed. Defaults to false.

Returns a hash containing information about the query URL and a list of blogs.

Valid Return Keys:

  • ‘weblog/name’: Name of blog.
  • ‘weblog/url’: URL of blog.
  • ‘weblog/rssurl’: RSS syndication URL of blog.
  • ‘weblog/atomurl’: Atom syndication URL of blog.
  • ‘weblog/inboundblogs’: Number of inbound blogs.
  • ‘weblog/inboundlinks’: Number of inbound links.
  • ‘weblog/lastupdate’: Date of last update.
  • ‘nearestpermalink’: Nearest permanent link.
  • ‘excerpt’: Excerpt from page matching search result.
  • ‘linkcreated’: Date link was created.
  • ‘linkurl’: Link URL.
  • ‘weblog/rank’: Cosmos Rank.
  • rankingstart:
  • ‘url’: URL.
  • ‘items’: an array of hashes containing blogs

Raises Technorati::Error on error.

Examples:

Here’s a basic cosmos query:

  # basic cosmos query
  site = 'slashdot.org'
  puts tr.cosmos(site).items.map { |item| item.weblog_name }

And here’s the same query with some options:

  # set query options
  site = 'slashdot.org'
  cosmos_opts = { 'limit' => 35 }

  # run technorati cosmos query
  cosmos = tr.cosmos(site, cosmos_opts)

  # print out a list of the first 35 sites linking to slashdot.org
  puts cosmos.items.map { |item| item.weblog_name }

Get information that Technorati knows about a user. See technorati.com/developers/api/getinfo.html for additional information.

In the simplest case you can use Technorati#info to find out information that a blogger wants to make known about himself, along with some information that Technorati has calculated and verified about that person. The returned info is broken up into two sections: The first part describes some information that the user wants to allow people to know about him- or herself. The second part of the document is a listing of the weblogs that the user has successfully claimed and the information that Technorati knows about these weblogs.

Parameters:

  • user (required): Username

Returns a hash containing information about the user and a list of blogs associated with that user.

Valid Return Keys:

  • ‘username’: User name of user.
  • ‘firstname’: First name of user.
  • ‘lastname’: Last name of user.
  • ‘thumbnailpicture’: URL to thumbnail picture.
  • ‘weblog/name’: Name of blog containing match.
  • ‘weblog/url: URL of blog containing match.
  • ‘weblog/rssurl’: RSS URL of blog containing match.
  • ‘weblog/atomurl’: Atom URL of blog containing match.
  • ‘weblog/inboundblogs’: Number of inbound blogs of blog containing match.
  • ‘weblog/inboundlinks’: Number of inbound links of blog containing match.
  • ‘weblog/lastupdate’: Date blog was last updated.
  • ‘rank’: Cosmos ranking.
  • ‘lang’: Blog language as integer.
  • ‘lat’: Geographical information.
  • ‘lon’: Geographical information.
  • ‘foafurl’: FOAF URL.
  • ‘items’: an Array of Hashes with each matched user’s blog.

Raises Technorati::Error on error.

Example:

  # print out a list of blog URLs associated with 'giblet'
  puts t.info('giblet')['items'] map { |blog| blog['weblog/url'] }

Get information about your key usage. Note that calls to this method do not count towards your limit. See technorati.com/developers/api/keyinfo.html for additional information.

Valid Return Keys:

  • ‘apiqueries’: Number of queries today.
  • ‘maxqueries’: Maximum number of allowed queries.

Example:

  info = tr.key_info
  num_left = info.maxqueries - info.apiqueries
  puts "remaining queries: #{num_left}"

Get a list of sites containing the given search string. See technorati.com/developers/api/search.html for additional information.

Note: This method has changed since 0.1.x. It takes a hash of optional arguments as the second parameter. Calling it with the old 0.1-style parameters will work, but is deprecated, will print a warning, and may stop working in a future release.

Required Parameters:

  • words: an Array of words or a whitespace-delimited String

Any additional arguments are optional and may be passed as a hash. Here’s a description of each optional argument:

Optional Arguments:

  • start: Set this to a number larger than 0 and you’ll get the start + 20 freshest items (links or blogs), e.g. set it to 21, and you’ll get the second page of rankings (21 through 40).
  • claim: Set to true to include Technorati member data in the result set when a blog has been successfully claimed. Defaults to false.
  • limit: Set this to a number larger than 0 and smaller or equal to 100 and it will return limit number of links for a query. By default this value is 20.
  • language: Set this to an ISO 639-1 language code to retrieve results specific to that language. According to the Technorati API documetation, this feature is beta and may not work correctly.
  • authority: Set this to filter results to those from blogs with at least the Technorati Authority specified. Technorati calculates a blog’s authority by how many people link to it. Filtering by authority is a good way to refine your search results. There are four settings:
    • n: Any authority: All results (default if unspecified).
    • a1: A little authority: Results from blogs with at least one link.
    • a4: Some authority: Results from blogs with a handful of links.
    • a7: A lot of authority: Results from blogs with hundreds of links.

Returns a hash containing information about the query and a list of blogs.

Valid Return Keys:

  • ‘query’: Query string.
  • ‘querycount’: Number of matches.
  • ‘inboundblogs’: Number of inbound blogs.
  • ‘querytime’: Duration of query (in seconds).
  • ‘rankingstart’: Value of start parameter.
  • ‘weblog/name’: Name of blog containing match.
  • ‘weblog/url’: URL of blog containing match.
  • ‘weblog/rssurl’: RSS URL of blog containing match.
  • ‘weblog/atomurl’: Atom URL of blog containing match.
  • ‘weblog/inboundblogs’: Number of inbound blogs of blog containing match.
  • ‘weblog/inboundlinks’: Number of inbound links of blog containing match.
  • ‘weblog/lastupdate’: Date blog was last updated
  • ‘title’: Title of matching entry.
  • ‘excerpt’: Excerpt of matching entry with relevant text.
  • ‘created’: Date matching entry was created.
  • ‘items’: Array of hashes containing matching entries.

Raises Technorati::Error on error.

Examples:

  # basic search query
  tr.search('cooking')['items'].map { |item| item['weblog/url'] }

Here’s a more advanced search query:

  # search query with options
  words = 'indian cooking'
  opts = {
    'limit'     => 5,     # limit to first 5 results
    'authority' => 'a4',  # require a handful of links
  }

  # execute query
  results = tr.search(words, opts)

  # print results
  puts results['items'].map { |item|
    { 'weblog/url' => 'Blog',
      'title'      => 'Title',
      'created'    => 'Date',
      'excerpt'    => 'Excerpt',
    }.map { |row| "#{row[1]}: #{item[row[0]]}" }
  }.flatten

Get a list of posts with the given tag. See technorati.com/developers/api/tag.html for additional information.

Required Parameters:

  • tag: a String, such as ‘blues’ or ‘xylophone’

Any additional arguments are optional and may be passed as a hash. Here’s a description of each optional argument:

Optional Arguments:

  • start: Set this to a number larger than 0 and you’ll get the start + 20 freshest posts, e.g. set it to 21, and you’ll get the second page of posts (21 through 40).
  • limit: Set this to a number larger than 0 and smaller or equal to 100 and it will return limit number of links for a query. By default this value is 20.
  • excerptsize: Number of word characters to include in post excerpts. Defaults to 100.
  • topexcerptsize: Number of word characters to include in the first post excerpt. Defaults to 150.

Returns a hash containing information about the query URL and a list of blogs.

Valid Return Keys:

  • ‘weblog/name’: Name of blog.
  • ‘weblog/url’: URL of blog.
  • ‘weblog/rssurl’: RSS syndication URL of blog.
  • ‘weblog/atomurl’: Atom syndication URL of blog.
  • ‘weblog/inboundblogs’: Number of inbound blogs.
  • ‘weblog/inboundlinks’: Number of inbound links.
  • ‘weblog/lastupdate’: Date of last update.
  • ‘excerpt’: Excerpt from page matching search result.
  • ‘title’: Title of matching post.
  • ‘excerpt’: Excerpt of matching entry with relevant text.
  • ‘created’: Date matching entry was created.
  • ‘permalink’: Permanent link to this post.
  • ‘created’: Date matching entry was last updated.
  • ‘items’: an array of hashes containing matching posts

Raises Technorati::Error on error.

Examples:

  # search for posts matching 'banana' and print them out
  puts r.tag('banana').items.map { |post|
    "\"#{post.title}\" (#{post.permalink}):\n=> #{post.excerpt}"
  }

Get the top tags used on Technorati. See technorati.com/developers/api/toptags.html for additional information.

There are no required parameters for this method, however you may pass a hash of optional arguments. Here’s a description of each optional argument:

Optional Arguments:

  • start: Set this to a number larger than 0 and you’ll get the start + 20 highest-rated tags, e.g. set it to 21, and you’ll get the second page of tags (21 through 40).
  • limit: Set this to a number larger than 0 and smaller or equal to 100 and it will return limit number of tags. By default this value is 20.

Valid Return Keys:

  • ‘limit’: value of ‘limit’ parameter.
  • ‘items’: Array of hashes containing matching tags.
  • ‘tag’: Tag value.
  • ‘posts’: Number of posts matching given tag.

Example:

  # print out the top 20 tags on technorati
  puts tr.top_tags.items.map { |tag|
    "#{tag.tag} (#{tag.posts})"
  }

[Validate]