| 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
| 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 |
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)
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:
Returns a hash containing information about the query URL.
Valid Return Keys:
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:
Any additional arguments are optional and may be passed as a hash. Here’s a description of each optional argument:
Optional Arguments:
Returns a hash containing information about the query URL.
Valid Return Keys:
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']})"
}
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:
Any additional arguments are optional and may be passed as a hash. Here’s a description of each optional argument:
Optional Arguments:
Returns a hash containing information about the query URL and a list of blogs.
Valid Return Keys:
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:
Returns a hash containing information about the user and a list of blogs associated with that user.
Valid Return Keys:
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:
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:
Any additional arguments are optional and may be passed as a hash. Here’s a description of each optional argument:
Optional Arguments:
Returns a hash containing information about the query and a list of blogs.
Valid Return Keys:
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:
Any additional arguments are optional and may be passed as a hash. Here’s a description of each optional argument:
Optional Arguments:
Returns a hash containing information about the query URL and a list of blogs.
Valid Return Keys:
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:
Valid Return Keys:
Example:
# print out the top 20 tags on technorati
puts tr.top_tags.items.map { |tag|
"#{tag.tag} (#{tag.posts})"
}