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.
-
#call_source(options) ⇒ Object
Dashboard integration.
-
#comment_posts(options = {}) ⇒ int
Add a comment to Posts.
- #dashboard? ⇒ Boolean
-
#generate_worker(*data, block) ⇒ Object
Work queue processor.
-
#get_posts ⇒ [Post]
Determine draft data to use.
-
#index_within_limit?(index, limit) ⇒ Boolean
index < limit.
- #likes? ⇒ Boolean
-
#limited_posts ⇒ Object
Get @limit # of Posts.
-
#post_operation(options, &block) ⇒ 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, max_id: nil, since_id: nil) ⇒ [Post]
Get up to 50 Posts.
-
#some_test_data ⇒ Object
Handle limits for test data.
-
#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
174 175 176 177 178 |
# File 'lib/draftking/posts.rb', line 174 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
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/draftking/posts.rb', line 62 def calculate_result(result_q) mod_count = 0 mod_posts = [] return [mod_count, mod_posts] if result_q.empty? while post = result_q.pop mod_count += post.saved mod_posts << post if post.saved > 0 break if result_q.empty? end [mod_count, mod_posts] end |
#call_source(options) ⇒ Object
Dashboard integration
149 150 151 152 153 |
# File 'lib/draftking/posts.rb', line 149 def call_source() return @client.send('dashboard', ).first[1] if dashboard? return @client.send('likes', ).first[1] if likes? @client.send(@source, @blog_url, ).first[1] end |
#comment_posts(options = {}) ⇒ int
Add a comment to Posts
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/draftking/posts.rb', line 83 def comment_posts( = {}) src = source_string([:source]) [:message] = "Adding #{src} comment \'#{comment}\': " post_operation() do |post, _| post.replace_comment_with(@comment) post.(keep_tags: , add_tags: , exclude: @comment, credit: @credit) if @auto_tag end end |
#dashboard? ⇒ Boolean
185 186 187 |
# File 'lib/draftking/posts.rb', line 185 def dashboard? @source == :dashboard end |
#generate_worker(*data, block) ⇒ Object
Work queue processor
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/draftking/posts.rb', line 29 def generate_worker(*data, block) work, results, total = data begin while post = work.pop(true) po = Post.new(post, keep_tree: @keep_tree) block.call(po, results.size) # Do work on Post po.save(client: @client, simulate: @simulate) results.push(po) show_progress(current: results.size, total: total, message: ) unless @mute end rescue ThreadError # Queue empty end end |
#get_posts ⇒ [Post]
Determine draft data to use.
123 124 125 126 127 128 129 130 |
# File 'lib/draftking/posts.rb', line 123 def get_posts print "Getting posts...\r" unless @mute return some_test_data if @test_data return some_posts(offset: @offset) if dashboard? 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.zero? index < limit end |
#likes? ⇒ Boolean
189 190 191 |
# File 'lib/draftking/posts.rb', line 189 def likes? @source == :likes end |
#limited_posts ⇒ Object
Get @limit # of Posts
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/draftking/posts.rb', line 156 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, &block) ⇒ int
Common code for Post operations
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/draftking/posts.rb', line 17 def post_operation(, &block) work, total, results, reporter = setup_operation() workers = (0...DK::MAX_THREADS).map { Thread.new { generate_worker(work, results, total, block) } } workers.map(&:join) mod_count, mod_posts = calculate_result(results) show_progress(message: , done: true, modified: mod_count) unless @mute reporter.new(objects: mod_posts, title: REPORT_TITLE, fields: REPORT_FIELDS, simulate: @simulate).show unless @mute act_on_blog(name: @blog_name) # Refresh account info [mod_count, mod_posts] end |
#posts_to_queue(posts) ⇒ Object
Create queue of Posts for worker threads
55 56 57 58 59 |
# File 'lib/draftking/posts.rb', line 55 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
44 45 46 47 48 49 50 51 52 |
# File 'lib/draftking/posts.rb', line 44 def setup_operation() print "Setup\r" unless @mute () act_on_blog(name: @blog_name) posts = @shuffle ? get_posts.reverse.shuffle : get_posts.reverse work = posts_to_queue(posts) reporter = [:reporter] || DK::Reporter [work, work.size, Queue.new, reporter] 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}#{message}#{progress}#{' ' * 30}\r#{newline}" $stdout.flush unless done end |
#some_posts(before_id: 0, offset: 0, max_id: nil, since_id: nil) ⇒ [Post]
Get up to 50 Posts
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/draftking/posts.rb', line 136 def some_posts(before_id: 0, offset: 0, max_id: nil, since_id: nil) = { limit: [(@limit || 50), 50].min } [:max_id] = max_id if max_id [:since_id] = since_id if since_id [@source == :draft ? :before_id : :offset] = (@source == :draft ? before_id : offset) unless dashboard? [:type] = @type if @type result = call_source() result.is_a?(Integer) ? [] : result end |
#some_test_data ⇒ Object
Handle limits for test data
181 182 183 |
# File 'lib/draftking/posts.rb', line 181 def some_test_data @limit ? @test_data.take(@limit) : @test_data 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
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/draftking/posts.rb', line 104 def tag_posts() src = source_string([:source]) [:message] = "Tagging #{src} with #{options[:add_tags]}: " post_operation() do |post, _| post.(keep_tags: , add_tags: , exclude: @comment, credit: @credit) if @auto_tag 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 |