class DK::Client

Tumblr Client

Attributes

auto_tag[RW]
before_id[RW]
blog[RW]
blog_name[RW]
blog_url[RW]
client[RW]
comment[RW]
credit[RW]
d_size[RW]
greedy[RW]
keep_tags[RW]
keep_tree[RW]
limit[RW]
message[RW]
mute[RW]
offset[RW]
q_size[RW]
q_space[RW]
show_pi[RW]
shuffle[RW]
simulate[RW]
source[RW]
state[RW]
tags[RW]
test_data[RW]
type[RW]
user[RW]

Public Class Methods

new(options = {}) click to toggle source

Initialize instance of DraftKing for the specified blog @param options [String] Target blog name @param options [String] Default post comment

# File lib/draftking/client.rb, line 22
def initialize(options = {})
  return unless configure_tumblr_client(options)
  @client = Tumblr::Client.new
  process_options(options)
  act_on_blog(name: @blog_name)
end

Public Instance Methods

act_on_blog(name: nil) click to toggle source

Collect/Refresh Account Info @param name [String] Name of blog to target

# File lib/draftking/client.rb, line 66
def act_on_blog(name: nil)
  return unless connected?
  @user = client_user_info_to_ostruct
  @blog_name = name ? name.gsub('.tumblr.com', '') : @user.blogs.first.name
  @blog_url  = tumblr_url(@blog_name)
  @user.blogs.each do |blog|
    next unless blog.name == @blog_name
    @blog    = blog
    @q_size  = blog.queue
    @d_size  = blog.drafts
    @q_space = 300 - @q_size
  end
end
block_with_retry(opts = {}, &block) click to toggle source
# File lib/draftking/client.rb, line 85
def block_with_retry(opts = {}, &block)
  retries = 0
  begin
    return block.call(opts)
  rescue
    retry unless (retries += 1) > MAX_RETRY
    return nil
  end
end
client_user_info_to_ostruct() click to toggle source
# File lib/draftking/client.rb, line 80
def client_user_info_to_ostruct
  info = block_with_retry { |_| @client.info['user'].to_json }
  JSON.parse(info, object_class: OpenStruct)
end
configure_tumblr_client(options) click to toggle source

Configure tumblr_client gem @param options [String] JSON File with API Keys @param options [Hash] Hash with API Keys

# File lib/draftking/client.rb, line 58
def configure_tumblr_client(options)
  keys = DK::Config.validate_keys(options[:keys])
  return DK::Config.configure_tumblr_gem(keys: keys) unless keys.nil?
  DK::Config.configure_tumblr_gem(file: options[:config_file])
end
connected?() click to toggle source
# File lib/draftking/client.rb, line 95
def connected?
  @client && @client.info['status'] != 401
end
process_options(options) click to toggle source

Read Config

# File lib/draftking/client.rb, line 30
def process_options(options)
  @blog_name = options[:blog_name] || @blog_name
  @credit    = options[:credit]    || @credit
  @key_text  = options[:key_text]  || @key_text
  @keep_tree = options[:keep_tree] || @keep_tree
  @keep_tags = options[:keep_tags] || @keep_tags
  @message   = options[:message]   || @message
  @mute      = options[:mute]      || @mute
  @shuffle   = options[:shuffle]   || @shuffle
  @simulate  = options[:simulate]  || @simulate
  @state     = options[:state]     || @state
  @test_data = options[:test_data] || @test_data
  @tags      = options[:add_tags]  || @tags
  @comment   = options[:comment]   || @comment.to_s
  @auto_tag  = options[:tags].nil? ? true : options[:tags]
  @source    = process_source(options[:source])
  @before_id = options[:before_id] || 0
  @offset    = options[:offset]    || 0
  @greedy    = options[:greedy]    || @greedy
  @limit     = options[:limit]
  @type      = options[:type]
  @blog_url  = tumblr_url(@blog_name)
  @show_pi   = options[:show_pi]
end
process_source(src) click to toggle source

Process options

# File lib/draftking/client.rb, line 100
def process_source(src)
  return :draft unless src
  return src if src.is_a? Symbol
  return :dashboard if src.include?('dash')
  return :queue if src.include?('queue')
  return :posts if src.include?('publish')
  return :draft if src.include?('draft')
end