Class: DK::Client

Inherits:
Object
  • Object
show all
Includes:
Posts, TDrafts, TQueue
Defined in:
lib/draftking/client.rb

Overview

Tumblr Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Posts

#all_posts, #auto_poster, #calculate_result, #call_source, #comment_posts, #dashboard?, #generate_worker, #get_posts, #index_within_limit?, #likes?, #limited_posts, #post_operation, #posts_to_queue, #setup_done, #setup_operation, #setup_undone, #show_progress, #shufflex, #some_posts, #some_test_data, #source_string, #tag_posts, #tumblr_url

Methods included from TQueue

#move_to_drafts

Methods included from TDrafts

#drafts_to_queue, #strip_old_comments, #strip_tags

Constructor Details

#initialize(options = {}) ⇒ Client

Initialize instance of DraftKing for the specified blog

Parameters:

  • options (:blog_name) (defaults to: {})
    String

    Target blog name

  • options (:comment) (defaults to: {})
    String

    Default post comment



22
23
24
25
26
27
# 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

Instance Attribute Details

#auto_tagObject

Returns the value of attribute auto_tag.



16
17
18
# File 'lib/draftking/client.rb', line 16

def auto_tag
  @auto_tag
end

#before_idObject

Returns the value of attribute before_id.



15
16
17
# File 'lib/draftking/client.rb', line 15

def before_id
  @before_id
end

#blogObject

Returns the value of attribute blog.



9
10
11
# File 'lib/draftking/client.rb', line 9

def blog
  @blog
end

#blog_nameObject

Options



13
14
15
# File 'lib/draftking/client.rb', line 13

def blog_name
  @blog_name
end

#blog_urlObject

Returns the value of attribute blog_url.



9
10
11
# File 'lib/draftking/client.rb', line 9

def blog_url
  @blog_url
end

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/draftking/client.rb', line 8

def client
  @client
end

#commentObject

Options



13
14
15
# File 'lib/draftking/client.rb', line 13

def comment
  @comment
end

#creditObject

Returns the value of attribute credit.



15
16
17
# File 'lib/draftking/client.rb', line 15

def credit
  @credit
end

#d_sizeObject

Returns the value of attribute d_size.



10
11
12
# File 'lib/draftking/client.rb', line 10

def d_size
  @d_size
end

#keep_tagsObject

Returns the value of attribute keep_tags.



15
16
17
# File 'lib/draftking/client.rb', line 15

def keep_tags
  @keep_tags
end

#keep_treeObject

Returns the value of attribute keep_tree.



14
15
16
# File 'lib/draftking/client.rb', line 14

def keep_tree
  @keep_tree
end

#limitObject

Options



13
14
15
# File 'lib/draftking/client.rb', line 13

def limit
  @limit
end

#messageObject

Returns the value of attribute message.



16
17
18
# File 'lib/draftking/client.rb', line 16

def message
  @message
end

#muteObject

Returns the value of attribute mute.



14
15
16
# File 'lib/draftking/client.rb', line 14

def mute
  @mute
end

#offsetObject

Options



13
14
15
# File 'lib/draftking/client.rb', line 13

def offset
  @offset
end

#q_sizeObject

Returns the value of attribute q_size.



10
11
12
# File 'lib/draftking/client.rb', line 10

def q_size
  @q_size
end

#q_spaceObject

Returns the value of attribute q_space.



10
11
12
# File 'lib/draftking/client.rb', line 10

def q_space
  @q_space
end

#shuffleObject

Returns the value of attribute shuffle.



14
15
16
# File 'lib/draftking/client.rb', line 14

def shuffle
  @shuffle
end

#simulateObject

Returns the value of attribute simulate.



15
16
17
# File 'lib/draftking/client.rb', line 15

def simulate
  @simulate
end

#sourceObject

Returns the value of attribute source.



16
17
18
# File 'lib/draftking/client.rb', line 16

def source
  @source
end

#stateObject

Returns the value of attribute state.



16
17
18
# File 'lib/draftking/client.rb', line 16

def state
  @state
end

#tagsObject

Returns the value of attribute tags.



17
18
19
# File 'lib/draftking/client.rb', line 17

def tags
  @tags
end

#test_dataObject

Returns the value of attribute test_data.



14
15
16
# File 'lib/draftking/client.rb', line 14

def test_data
  @test_data
end

#typeObject

Returns the value of attribute type.



17
18
19
# File 'lib/draftking/client.rb', line 17

def type
  @type
end

#userObject

Returns the value of attribute user.



8
9
10
# File 'lib/draftking/client.rb', line 8

def user
  @user
end

Instance Method Details

#act_on_blog(name: nil) ⇒ Object

Collect/Refresh Account Info

Parameters:

  • name (String) (defaults to: nil)

    Name of blog to target



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/draftking/client.rb', line 65

def act_on_blog(name: nil)
  return unless connected?
  @user = JSON.parse(@client.info['user'].to_json, object_class: OpenStruct)
  @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

#configure_tumblr_client(options) ⇒ Object

Configure tumblr_client gem

Parameters:

  • options (:file)
    String

    JSON File with API Keys

  • options (:keys)
    Hash

    Hash with API Keys



57
58
59
60
61
# File 'lib/draftking/client.rb', line 57

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?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/draftking/client.rb', line 79

def connected?
  @client && @client.info['status'] != 401
end

#process_options(options) ⇒ Object

Read Config



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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)
end

#process_source(src) ⇒ Object

Process options



84
85
86
87
88
89
# File 'lib/draftking/client.rb', line 84

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')
end