module TumblrThemer::API

Attributes

api_key[W]
base_hostname[W]

Public Instance Methods

all_posts(params={}) click to toggle source
# File lib/tumblr-themer/api.rb, line 53
def all_posts(params={})
  url = URL.new("http://api.tumblr.com/v2/blog/#{base_hostname}/posts")
  url[:api_key] = api_key
  url[:reblog_info] = true
  url[:id] = params[:id] if params[:id]
  json = url.get.json

  status = json['meta']['status']
  if status == 401
    raise ConfigFileError, "invalid api_key found in config file"
  elsif status != 200
    raise RequestError, json['meta'].inspect
  end
  json
end
api_key() click to toggle source
# File lib/tumblr-themer/api.rb, line 26
def api_key
  @api_key ||= settings['api_key']
end
base_hostname() click to toggle source
# File lib/tumblr-themer/api.rb, line 5
def base_hostname
  name = @base_hostname || settings['blog']
  if name =~ /tumblr\.com/
    name
  else
    "#{name}.tumblr.com"
  end
end
posts() click to toggle source
# File lib/tumblr-themer/api.rb, line 30
def posts
  if settings['posts']
    selected_posts
  else
    all_posts['response']
  end
end
selected_posts() click to toggle source
# File lib/tumblr-themer/api.rb, line 38
def selected_posts
  blog = nil
  posts = []
  settings['posts'].each do |id|
    data = all_posts(:id => id)
    posts << data['response']['posts'].first
    blog ||= data['response']['blog']
  end

  {
    'posts' => posts,
    'blog' => blog
  }
end
settings(dirname='.') click to toggle source
# File lib/tumblr-themer/api.rb, line 14
def settings dirname='.'
  file = File.expand_path("#{dirname}/tumblr-theme.yml")

  unless File.exist?(file)
    raise ConfigFileError, "#{file} not found"
  end

  data = YAML.load_file(file)
  raise ConfigFileError, "no api_key found in config file" unless data['api_key']
  data
end