Skip to content

Instantly share code, notes, and snippets.

@dleavitt
Created September 1, 2011 01:17
Show Gist options
  • Save dleavitt/1185165 to your computer and use it in GitHub Desktop.
Save dleavitt/1185165 to your computer and use it in GitHub Desktop.
Simple Tumblr v2 API
require 'httparty'
module Tumblr
class API
include HTTParty
attr_reader :api_key, :access_token
base_uri 'api.tumblr.com/v2'
def initialize(options = {})
self.access_token = options[:access_token] if options[:access_token]
self.api_key = options[:api_key] if options[:api_key]
end
def api_key=(val)
@api_key = val
self.class.default_params :api_key => @api_key
end
def access_token=(val)
@access_token = val
self.class.default_params :access_token => @access_token
end
protected
def do_get(path, key = nil, query = {})
# TODO: error handling
res = self.class.get(path, query => query)['response']
key ? res[key] : res
end
end
class Blog < Tumblr::API
attr_accessor :base_hostname
def initialize(base_hostname, options = {})
@base_hostname = base_hostname
super options
end
def info
do_get '/info', 'blog', {}
end
def avatar(size = {})
# TODO: decode image
query = {:size => size} if size
self.class.get '/avatar', :query => query
end
# requires oauth
# def followers(query = {})
# do_get('/followers', query)['users']
# end
def posts(query = {})
do_get('/posts', 'posts', query)
end
def do_get(path, key = nil, query = {})
super "/blog/#{base_hostname}"+path, key, query
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment