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
-
#all_posts(last_id: 0, offset: 0) ⇒ [Post]
Collect all Posts.
-
#calculate_result(result_q) ⇒ Object
Determine number of modified posts.
-
#comment_posts(options = {}) ⇒ int
Add a comment to Posts.
-
#get_posts ⇒ [Post]
Determine draft data to use.
-
#index_within_limit?(index, limit) ⇒ Boolean
index < limit.
-
#limited_posts ⇒ Object
Get @limit # of Posts.
-
#post_operation(options) ⇒ int
Common code for Post operations.
-
#posts_to_queue(posts) ⇒ Object
Create queue of Posts for worker threads.
-
#setup_done(modified) ⇒ Object
Values for displaying completed process.
-
#setup_operation(options) ⇒ Object
Common initialization for post operations.
-
#setup_undone(current, total) ⇒ Object
Values for displaying in-progress process.
-
#show_progress(current: 0, total: 0, message: '', done: false, modified: 0) ⇒ Object
Display progress percentage.
-
#some_posts(before_id: 0, offset: 0) ⇒ [Post]
Get up to 50 Posts.
-
#source_string(symbol) ⇒ Object
Convert source symbol to string.
-
#tag_posts(options) ⇒ int
Number of modified posts.
-
#tumblr_url(blog_name) ⇒ Object
Construct tumblr URL string.
Instance Method Details
#all_posts(last_id: 0, offset: 0) ⇒ [Post]
Collect all Posts
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
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/draftking/posts.rb', line 72 def comment_posts( = {}) src = source_string([:source]) [:message] = "Adding #{src} comment \'#{comment}\': " post_operation() do |post, _| changed = post.replace_comment(comment: @comment) changed = post.(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.
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
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_posts ⇒ Object
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
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() work_q, total, result_q = setup_operation() 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: ) unless @mute end rescue ThreadError # Queue empty end end end workers.map(&:join) modified = calculate_result(result_q) show_progress(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() () 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
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}#{}#{progress}#{' ' * 30}\r#{newline}" $stdout.flush unless done end |
#some_posts(before_id: 0, offset: 0) ⇒ [Post]
Get up to 50 Posts
124 125 126 127 128 129 130 |
# File 'lib/draftking/posts.rb', line 124 def some_posts(before_id: 0, offset: 0) = { limit: [(@limit || 50), 50].min } [@source == :draft ? :before_id : :offset] = (@source == :draft ? before_id : offset) result = @client.send(@source, @blog_url, ).first[1] result.is_a?(Integer) ? [] : result end |
#source_string(symbol) ⇒ Object
Convert source symbol to string
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.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/draftking/posts.rb', line 94 def tag_posts() src = source_string([:source]) [:message] = "Tagging #{src} with #{[:add_tags]}: " post_operation() do |post, _| post.(keep_tags: @keep_tags, add_tags: @tags, exclude: @comment, credit: @credit) end end |
#tumblr_url(blog_name) ⇒ Object
Construct tumblr URL string
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 |