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 Posts

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



151
152
153
154
155
# File 'lib/draftking/posts.rb', line 151

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

#calculate_result(result_q) ⇒ Object

Determine number of modified posts



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

def calculate_result(result_q)
  modified = 0
  modified += result_q.pop until result_q.empty?
  modified
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



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/draftking/posts.rb', line 72

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



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

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

#index_within_limit?(index, limit) ⇒ Boolean

index < limit

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/draftking/posts/posts_helpers.rb', line 50

def index_within_limit?(index, limit)
  return true if limit.nil? || limit == 0
  index < limit
end

#limited_postsObject

Get @limit # of Posts



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/draftking/posts.rb', line 133

def limited_posts
  result = []
  until result.size >= @limit
    chunk = some_posts(offset: @offset, before_id: @before_id)
    break if chunk.empty?
    result += chunk
    @offset    = chunk.size
    @before_id = chunk.last['id']
  end
  result.take(@limit)
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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/draftking/posts.rb', line 17

def post_operation(options)
  work_q, total, result_q = setup_operation(options)
  workers = (0...4).map do
    Thread.new do
      begin
        while post = work_q.pop(true)
          po = Post.new(post, keep_tree: @keep_tree)
          changed = yield(po, result_q.size) || !po.keep_tree
          result_q.push((changed ? po.save(client: @client, simulate: @simulate) : 0))
          show_progress(current: result_q.size, total: total, message: message) unless @mute
        end
      rescue ThreadError # Queue empty
      end
    end
  end
  workers.map(&:join)
  modified = calculate_result(result_q)
  show_progress(message: message, done: true, modified: modified) unless @mute
  act_on_blog(name: @blog_name) # Refresh account info
  modified
end

#posts_to_queue(posts) ⇒ Object

Create queue of Posts for worker threads



50
51
52
53
54
# File 'lib/draftking/posts.rb', line 50

def posts_to_queue(posts)
  work_q = Queue.new
  posts.each { |p| work_q.push(p) }
  work_q
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

Common initialization for post operations



40
41
42
43
44
45
46
47
# File 'lib/draftking/posts.rb', line 40

def setup_operation(options)
  process_options(options)
  act_on_blog(name: @blog_name)
  posts = @shuffle ? get_posts.shuffle : get_posts
  posts = posts.take(@limit) if @limit
  work_q = posts_to_queue(posts)
  [work_q, work_q.size, Queue.new]
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 Posts

Parameters:

  • 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



124
125
126
127
128
129
130
# File 'lib/draftking/posts.rb', line 124

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



94
95
96
97
98
99
100
101
102
103
# File 'lib/draftking/posts.rb', line 94

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