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 Drafts.
-
#comment_posts(options = {}) ⇒ int
Add a comment to Posts.
-
#get_posts ⇒ [Post]
Determine draft data to use.
-
#post_operation(options) ⇒ int
Common code for Post operations.
-
#setup_done(modified) ⇒ Object
Values for displaying completed process.
- #setup_operation(options) ⇒ Object
-
#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 Drafts.
-
#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 Drafts
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
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/draftking/posts.rb', line 48 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.
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
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/draftking/posts.rb', line 16 def post_operation() posts, total, modified = setup_operation() 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: ) unless @mute end show_progress(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() () 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
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 Drafts
101 102 103 104 105 106 107 |
# File 'lib/draftking/posts.rb', line 101 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.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/draftking/posts.rb', line 70 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 |