Module: DK::Posts

Included in:
Client
Defined in:
lib/draftking/posts.rb,
lib/draftking/posts/posts_helpers.rb

Overview

Helper Methods

Instance Method Summary collapse

Instance Method Details

#all_posts(last_id: 0, offset: 0) ⇒ [Post]

Collect all Drafts

Parameters:

  • last_id (Int) (defaults to: 0)

    ID of post to begin reading from (for reading Drafts)

  • offset (Int) (defaults to: 0)

    Post index to start reading from (for reading Queue)

  • blog_url (string)

    URL of blog to read from

  • source (Symbol)

    Get posts from :draft or :queue

Returns:

  • ([Post])

    Array of Post Hash data



115
116
117
118
119
# File 'lib/draftking/posts.rb', line 115

def all_posts(last_id: 0, offset: 0)
  chunk = some_posts(before_id: last_id, offset: offset)
  return chunk if chunk.empty?
  chunk + all_posts(last_id: chunk.last['id'], offset: offset + chunk.size)
end

#comment_posts(options = {}) ⇒ int

Add a comment to Posts

Parameters:

  • options (:credit) (defaults to: {})
    Bool

    Give dk credit?

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

    String to add as comment

  • options (:limit) (defaults to: {})
    Int

    Max number of modified posts

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

    Message to display during processing

  • options (:source) (defaults to: {})
    Symbol

    Target posts from :draft or :queue

  • options (:simulate) (defaults to: {})
    bool

    Simulation?

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

    Suppress progress indicator

Returns:

  • (int)

    Number of modified posts



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/draftking/posts.rb', line 48

def comment_posts(options = {})
  src = source_string(options[:source])
  options[:message] = "Adding #{src} comment \'#{comment}\': "
  post_operation(options) do |post, _|
    changed = post.replace_comment(comment: @comment)
    changed = post.generate_tags(keep_tags: @keep_tags,
                                 add_tags:  @tags,
                                 exclude:   @comment,
                                 credit:    @credit) || changed if @tags
    changed
  end
end

#get_posts[Post]

Determine draft data to use.

Parameters:

  • options (:test_data)
    [Hash]

    Array of post hash data

  • options (:limit)
    Int

    Limit # of posts selected

  • options (:blog_url)
    string

    URL of blog to read from

  • options (:source)
    Symbol

    Get posts from :draft or :queue

  • options (:before_id)
    Int
    :draft

    ID of post to begin reading from

  • options (:offset)
    Int
    :queue

    Post index to start reading from

Returns:

  • ([Post])

    Array of Post Hash data



89
90
91
92
93
# File 'lib/draftking/posts.rb', line 89

def get_posts
  return @test_data if @test_data
  return all_posts.uniq unless @limit
  some_posts(offset: @offset, before_id: @before_id)
end

#post_operation(options) ⇒ int

Common code for Post operations

Parameters:

  • options (:limit)
    Int

    Maximum number of posts to process

  • options (:message)
    String

    Message to display during processing

  • options (:shuffle)
    Bool

    Randomize order of posts

  • options (:blog_name)
    String

    Name of blog to target

  • options (:mute)
    String

    Suppress progress indicator

  • options (:test_data)
    [Hash]

    Array of post hash data

  • options (:simulate)
    bool

    Simulation?

Returns:

  • (int)

    Number of modified posts



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/draftking/posts.rb', line 16

def post_operation(options)
  posts, total, modified = setup_operation(options)

  posts.each_with_index do |post, index|
    po = Post.new(post, keep_tree: @keep_tree)
    changed   = yield(po, index) || !po.keep_tree
    modified += (changed ? po.save(client: @client, simulate: @simulate) : 0)
    show_progress(current: index, total: total, message: message) unless @mute
  end

  show_progress(message: message, done: true, modified: modified) unless @mute
  act_on_blog(name: @blog_name) # Refresh account info
  modified
end

#setup_done(modified) ⇒ Object

Values for displaying completed process



18
19
20
21
22
23
# File 'lib/draftking/posts/posts_helpers.rb', line 18

def setup_done(modified)
  indicator  = ''
  newline    = "\n"
  progress   = "(#{modified} modified)"
  [indicator, newline, progress]
end

#setup_operation(options) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/draftking/posts.rb', line 31

def setup_operation(options)
  process_options(options)
  act_on_blog(name: @blog_name)
  posts = @shuffle ? get_posts.shuffle : get_posts
  posts = posts.take(@limit || @q_space)
  [posts, posts.size, 0]
end

#setup_undone(current, total) ⇒ Object

Values for displaying in-progress process



26
27
28
29
30
31
32
33
# File 'lib/draftking/posts/posts_helpers.rb', line 26

def setup_undone(current, total)
  tildes = current.to_i % 4
  indicator  = "~#{'~' * tildes}#{' ' * (3 - tildes)}> "
  newline    = nil
  percentage = total > 0 ? ((current.to_f / total.to_f) * 100).round : 0
  progress   = "#{current} / #{total} [#{percentage}\%] "
  [indicator, newline, progress]
end

#show_progress(current: 0, total: 0, message: '', done: false, modified: 0) ⇒ Object

Display progress percentage

Parameters:

  • current (Int) (defaults to: 0)

    Progress Counter

  • total (Int) (defaults to: 0)

    # Items to be processed

  • message (String) (defaults to: '')

    Display message for process

  • done (Bool) (defaults to: false)

    Processing Complete?

  • modified (Int) (defaults to: 0)

    # of items modified



10
11
12
13
14
15
# File 'lib/draftking/posts/posts_helpers.rb', line 10

def show_progress(current: 0, total: 0, message: '', done: false, modified: 0)
  indicator, newline, progress = setup_done(modified) if done
  indicator, newline, progress = setup_undone(current, total) unless done
  print "#{indicator}#{message}#{progress}#{' ' * 30}\r#{newline}"
  $stdout.flush unless done
end

#some_posts(before_id: 0, offset: 0) ⇒ [Post]

Get up to 50 Drafts

Parameters:

  • blog_url (string)

    URL of blog to read from

  • source (Symbol)

    Get posts from :draft or :queue

  • before_id (Int) (defaults to: 0)
    :draft

    ID of post to begin reading from

  • offset (Int) (defaults to: 0)
    :queue

    Post index to start reading from

Returns:

  • ([Post])

    Array of Post Hash data



101
102
103
104
105
106
107
# File 'lib/draftking/posts.rb', line 101

def some_posts(before_id: 0, offset: 0)
  options = { limit: [(@limit || 50), 50].min }
  options[@source == :draft ? :before_id : :offset] = (@source == :draft ? before_id : offset)

  result = @client.send(@source, @blog_url, options).first[1]
  result.is_a?(Integer) ? [] : result
end

#source_string(symbol) ⇒ Object

Convert source symbol to string

Parameters:

  • symbol (Symbol)

    Source Symbol



44
45
46
47
# File 'lib/draftking/posts/posts_helpers.rb', line 44

def source_string(symbol)
  return 'draft' unless symbol
  symbol.to_s
end

#tag_posts(options) ⇒ int

Returns Number of modified posts.

Parameters:

  • options (:credit)
    Bool

    Give dk credit?

  • options (:source)
    Symbol

    Target posts from :draft or :queue

  • options (:mute)
    String

    Suppress progress indicator

  • options (:blog_name)
    String

    Name of blog to target

  • options (:keep_tags)
    bool

    Preserve existing post tags

  • options (:keep_tree)
    bool

    Preserve existing post comments

  • options (:simulate)
    bool

    Simulation?

  • options (:comment)
    String

    Exclude :comment from tags

Returns:

  • (int)

    Number of modified posts



70
71
72
73
74
75
76
77
78
79
# File 'lib/draftking/posts.rb', line 70

def tag_posts(options)
  src = source_string(options[:source])
  options[:message] = "Tagging #{src} with #{options[:add_tags]}: "
  post_operation(options) do |post, _|
    post.generate_tags(keep_tags: @keep_tags,
                       add_tags:  @tags,
                       exclude:   @comment,
                       credit:    @credit)
  end
end

#tumblr_url(blog_name) ⇒ Object

Construct tumblr URL string

Parameters:

  • blog_name (String)

    Blog Name



37
38
39
40
# File 'lib/draftking/posts/posts_helpers.rb', line 37

def tumblr_url(blog_name)
  blog_name += '.tumblr.com' unless blog_name.include?('.')
  blog_name
end