Class: DK::Post
- Inherits:
-
Object
- Object
- DK::Post
- Defined in:
- lib/draftking/posts/post.rb
Overview
Tumblr Post
Instance Method Summary collapse
-
#add_tags(tags) ⇒ Object
Appends CSV or array of tags.
-
#change_state(state) ⇒ Object
Change the state of a post.
-
#clear_tags ⇒ Object
Remove existing Post tags.
-
#delete(client:, simulate: nil) ⇒ Object
Delete a Post.
-
#generate_tags(keep_tags: nil, add_tags: nil, exclude: nil, credit: false) ⇒ Object
Generate post tags from post comment.
-
#has_key_text?(key_text) ⇒ Boolean
Check if a post needs to be modified.
-
#initialize(hash, keep_tree: nil) ⇒ Post
constructor
A new instance of Post.
-
#reblog(client:, simulate: nil) ⇒ Object
Reblog a Post.
-
#replace_comment_with(comment) ⇒ Object
Add a comment to a post.
-
#save(client:, simulate: nil) ⇒ Object
Save a post.
-
#to_h ⇒ Object
Hash of post data.
-
#to_s ⇒ Object
String of post data.
Constructor Details
#initialize(hash, keep_tree: nil) ⇒ Post
Returns a new instance of Post.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/draftking/posts/post.rb', line 9 def initialize(hash, keep_tree: nil) return if hash.nil? @data = JSON.parse(hash.to_json, object_class: OpenStruct) # Translate @state = process_state(@data.state) @blog_url = tumblr_url(@data.blog_name) @image = original_image_url @photoset = @data.photoset_layout @keep_tree = keep_tree.nil? ? false : keep_tree @changed = false @saved = 0 @comment = @data.reblog.comment # Direct map @id = @data.id @reblog_key = @data.reblog_key @summary = @data.summary = @data. make_accessors(instance_variables) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (private)
151 152 153 154 155 156 |
# File 'lib/draftking/posts/post.rb', line 151 def method_missing(method, *args) if @data.respond_to?(method) return @data.send(method) unless method.to_s.include?('=') @data.send(method, args) end end |
Instance Method Details
#add_tags(tags) ⇒ Object
Appends CSV or array of tags
138 139 140 141 |
# File 'lib/draftking/posts/post.rb', line 138 def () = csv_to_a() if .is_a? String += end |
#change_state(state) ⇒ Object
Change the state of a post
55 56 57 58 59 60 |
# File 'lib/draftking/posts/post.rb', line 55 def change_state(state) return false unless VALID_STATE.include?(state) return false if @state == state @state = state @changed = true end |
#clear_tags ⇒ Object
Remove existing Post tags
132 133 134 135 |
# File 'lib/draftking/posts/post.rb', line 132 def @changed = true unless .empty? = [] end |
#delete(client:, simulate: nil) ⇒ Object
Delete a Post
80 81 82 83 84 85 |
# File 'lib/draftking/posts/post.rb', line 80 def delete(client:, simulate: nil) return 1 if simulate res = client.delete @blog_url, id @changed = true if res['id'] res['id'] ? 1 : 0 end |
#generate_tags(keep_tags: nil, add_tags: nil, exclude: nil, credit: false) ⇒ Object
Generate post tags from post comment
121 122 123 124 125 126 127 128 129 |
# File 'lib/draftking/posts/post.rb', line 121 def (keep_tags: nil, add_tags: nil, exclude: nil, credit: false) = (@comment) += csv_to_a() if += if << DK::CREDIT_TAG if credit -= csv_to_a(exclude) if exclude @changed = true unless .sort.uniq == .sort.uniq = end |
#has_key_text?(key_text) ⇒ Boolean
Check if a post needs to be modified
72 73 74 75 |
# File 'lib/draftking/posts/post.rb', line 72 def has_key_text?(key_text) return true if key_text.nil? @comment.include?(key_text) end |
#reblog(client:, simulate: nil) ⇒ Object
Reblog a Post
90 91 92 93 94 95 96 |
# File 'lib/draftking/posts/post.rb', line 90 def reblog(client:, simulate: nil) return 1 if simulate client.reblog @blog_url, id: id, reblog_key: @reblog_key, comment: @comment end |
#replace_comment_with(comment) ⇒ Object
Add a comment to a post
64 65 66 67 68 |
# File 'lib/draftking/posts/post.rb', line 64 def replace_comment_with(comment) return false if comment.nil? || @comment.include?(comment) @comment = comment @changed = true end |
#save(client:, simulate: nil) ⇒ Object
Save a post
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/draftking/posts/post.rb', line 101 def save(client:, simulate: nil) return 0 unless @changed return @saved = 1 if simulate res = client.edit @blog_url, id: id, reblog_key: @reblog_key, state: validate_state, attach_reblog_tree: @keep_tree, tags: .join(','), caption: @comment return 0 unless res && res['id'] @changed = false @saved = 1 end |
#to_h ⇒ Object
Hash of post data
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/draftking/posts/post.rb', line 38 def to_h { tumblr_id: @id, state: @state, tags: .join(','), comment: @comment, summary: @summary, blog_url: @blog_url, reblog_key: @reblog_key, keep_tree: @keep_tree, modified: @changed, image: @image } end |
#to_s ⇒ Object
String of post data
33 34 35 |
# File 'lib/draftking/posts/post.rb', line 33 def to_s to_h.map { |k, v| "#{k} = #{v}" }.join("\n") end |