Class: DK::Post
- Inherits:
-
Object
- Object
- DK::Post
- Defined in:
- lib/draftking/posts/post.rb
Overview
tumblr Post
Instance Attribute Summary collapse
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#id ⇒ Object
Returns the value of attribute id.
-
#keep_tree ⇒ Object
Returns the value of attribute keep_tree.
-
#reblog_key ⇒ Object
Returns the value of attribute reblog_key.
-
#state ⇒ Object
Returns the value of attribute state.
-
#summary ⇒ Object
Returns the value of attribute summary.
-
#tags ⇒ Object
Returns the value of attribute tags.
Instance Method Summary collapse
-
#change_state(state:) ⇒ Object
Change the state of a post.
-
#delete(client:, simulate: nil) ⇒ Object
Delete a Post.
-
#generate_tags(keep_tags: nil, add_tags: nil, exclude: nil, credit: true) ⇒ Object
Generate post tags from post comment.
-
#initialize(hash, keep_tree: nil) ⇒ Post
constructor
A new instance of Post.
-
#passes_filter?(filter:) ⇒ Boolean
Check if a post needs to be modified.
-
#reblog(client:, simulate: nil) ⇒ Object
Reblog a Post.
-
#replace_comment(comment:) ⇒ Object
Add a comment to a post.
-
#save(client:, simulate: nil) ⇒ Object
Save a post.
-
#to_s ⇒ Object
String of post data.
Constructor Details
#initialize(hash, keep_tree: nil) ⇒ Post
Returns a new instance of Post.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/draftking/posts/post.rb', line 10 def initialize(hash, keep_tree: nil) return if hash.nil? @id = hash['id'] @state = hash['state'] || DK::DRAFT @tags = hash['tags'] @comment = hash['reblog']['comment'] @summary = hash['summary'] @blog_url = tumblr_url(hash['blog_name']) @reblog_key = hash['reblog_key'] @keep_tree = keep_tree.nil? ? false : keep_tree end |
Instance Attribute Details
#comment ⇒ Object
Returns the value of attribute comment.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def comment @comment end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def id @id end |
#keep_tree ⇒ Object
Returns the value of attribute keep_tree.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def keep_tree @keep_tree end |
#reblog_key ⇒ Object
Returns the value of attribute reblog_key.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def reblog_key @reblog_key end |
#state ⇒ Object
Returns the value of attribute state.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def state @state end |
#summary ⇒ Object
Returns the value of attribute summary.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def summary @summary end |
#tags ⇒ Object
Returns the value of attribute tags.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def @tags end |
Instance Method Details
#change_state(state:) ⇒ Object
Change the state of a post
36 37 38 39 40 41 |
# File 'lib/draftking/posts/post.rb', line 36 def change_state(state:) return false unless VALID_STATE.include?(state) return false if @state == state @state = state true end |
#delete(client:, simulate: nil) ⇒ Object
Delete a Post
61 62 63 64 65 |
# File 'lib/draftking/posts/post.rb', line 61 def delete(client:, simulate: nil) return 1 if simulate res = client.delete @blog_url, @id res['id'] ? 1 : 0 end |
#generate_tags(keep_tags: nil, add_tags: nil, exclude: nil, credit: true) ⇒ Object
Generate post tags from post comment
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/draftking/posts/post.rb', line 98 def (keep_tags: nil, add_tags: nil, exclude: nil, credit: true) = @comment.gsub(%r{<(/)?p>}, '').gsub(%r{[\/\\|]}, ',') = .gsub(' , ', ',').gsub(@comment, '') += ',' + if += ',' + @tags.join(',') if .gsub!(exclude.to_s, '') += ',' + DK::CREDIT_TAG if credit .gsub!(/^\s*(,)*/, '') # Remove leading commas return @tags = unless @tags.join(',') == false end |
#passes_filter?(filter:) ⇒ Boolean
Check if a post needs to be modified
53 54 55 56 |
# File 'lib/draftking/posts/post.rb', line 53 def passes_filter?(filter:) return true if filter.nil? @comment.include?(filter) end |
#reblog(client:, simulate: nil) ⇒ Object
Reblog a Post
70 71 72 73 74 75 76 |
# File 'lib/draftking/posts/post.rb', line 70 def reblog(client:, simulate: nil) return 1 if simulate client.reblog @blog_url, id: @id, reblog_key: @reblog_key, comment: @comment end |
#replace_comment(comment:) ⇒ Object
Add a comment to a post
45 46 47 48 49 |
# File 'lib/draftking/posts/post.rb', line 45 def replace_comment(comment:) return false if comment.nil? || @comment.include?(comment) @comment = comment || @comment true end |
#save(client:, simulate: nil) ⇒ Object
Save a post
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/draftking/posts/post.rb', line 81 def save(client:, simulate: nil) return 1 if simulate res = client.edit @blog_url, id: @id, reblog_key: @reblog_key, state: @state, attach_reblog_tree: @keep_tree, tags: @tags, caption: @comment res['id'] ? 1 : 0 end |
#to_s ⇒ Object
String of post data
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/draftking/posts/post.rb', line 23 def to_s "id = #{@id}\n" \ "state = #{@state}\n" \ "tags = #{@tags}\n" \ "comment = #{@comment}\n" \ "summary = #{@summary}\n" \ "blog_url = #{@blog_url}\n" \ "reblog_key = #{@reblog_key}\n" \ "keep_tree = #{@keep_tree}\n" end |