Class: DK::Post
- Inherits:
-
Object
- Object
- DK::Post
- Defined in:
- lib/draftking/posts/post.rb
Overview
Tumblr Post
Instance Attribute Summary collapse
-
#blog_url ⇒ Object
Returns the value of attribute blog_url.
-
#changed ⇒ Object
Returns the value of attribute changed.
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#data ⇒ Object
Returns the value of attribute data.
-
#image ⇒ Object
Returns the value of attribute image.
-
#photoset ⇒ Object
Returns the value of attribute photoset.
-
#reblog_key ⇒ Object
Returns the value of attribute reblog_key.
-
#saved ⇒ Object
Returns the value of attribute saved.
-
#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
-
#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.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/draftking/posts/post.rb', line 11 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 @from = @data.trail.first.blog.name rescue '<no ID>' # Direct map @id = @data.id @reblog_key = @data.reblog_key @summary = @data.summary @tags = @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)
154 155 156 157 158 159 |
# File 'lib/draftking/posts/post.rb', line 154 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 Attribute Details
#blog_url ⇒ Object
Returns the value of attribute blog_url.
8 9 10 |
# File 'lib/draftking/posts/post.rb', line 8 def blog_url @blog_url end |
#changed ⇒ Object
Returns the value of attribute changed.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def changed @changed end |
#comment ⇒ Object
Returns the value of attribute comment.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def comment @comment end |
#data ⇒ Object
Returns the value of attribute data.
8 9 10 |
# File 'lib/draftking/posts/post.rb', line 8 def data @data end |
#image ⇒ Object
Returns the value of attribute image.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def image @image end |
#photoset ⇒ Object
Returns the value of attribute photoset.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def photoset @photoset end |
#reblog_key ⇒ Object
Returns the value of attribute reblog_key.
8 9 10 |
# File 'lib/draftking/posts/post.rb', line 8 def reblog_key @reblog_key end |
#saved ⇒ Object
Returns the value of attribute saved.
7 8 9 |
# File 'lib/draftking/posts/post.rb', line 7 def saved @saved end |
#state ⇒ Object
Returns the value of attribute state.
8 9 10 |
# File 'lib/draftking/posts/post.rb', line 8 def state @state end |
#summary ⇒ Object
Returns the value of attribute summary.
8 9 10 |
# File 'lib/draftking/posts/post.rb', line 8 def summary @summary end |
#tags ⇒ Object
Returns the value of attribute tags.
8 9 10 |
# File 'lib/draftking/posts/post.rb', line 8 def @tags end |
Instance Method Details
#add_tags(tags) ⇒ Object
Appends CSV or array of tags
141 142 143 144 |
# File 'lib/draftking/posts/post.rb', line 141 def () = csv_to_a() if .is_a? String @tags += end |
#change_state(state) ⇒ Object
Change the state of a post
58 59 60 61 62 63 |
# File 'lib/draftking/posts/post.rb', line 58 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
135 136 137 138 |
# File 'lib/draftking/posts/post.rb', line 135 def @changed = true unless @tags.empty? @tags = [] end |
#delete(client:, simulate: nil) ⇒ Object
Delete a Post
83 84 85 86 87 88 |
# File 'lib/draftking/posts/post.rb', line 83 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
124 125 126 127 128 129 130 131 132 |
# File 'lib/draftking/posts/post.rb', line 124 def (keep_tags: nil, add_tags: nil, exclude: nil, credit: false) = (@comment) += csv_to_a() if += @tags if << DK::CREDIT_TAG if credit -= csv_to_a(exclude) if exclude @changed = true unless @tags.sort.uniq == .sort.uniq @tags = end |
#has_key_text?(key_text) ⇒ Boolean
Check if a post needs to be modified
75 76 77 78 |
# File 'lib/draftking/posts/post.rb', line 75 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
93 94 95 96 97 98 99 |
# File 'lib/draftking/posts/post.rb', line 93 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
67 68 69 70 71 |
# File 'lib/draftking/posts/post.rb', line 67 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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/draftking/posts/post.rb', line 104 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: @tags.join(','), caption: @comment return 0 unless res && res['id'] @changed = false @saved = 1 end |
#to_h ⇒ Object
Hash of post data
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/draftking/posts/post.rb', line 41 def to_h { tumblr_id: @id, state: @state, tags: @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
36 37 38 |
# File 'lib/draftking/posts/post.rb', line 36 def to_s to_h.map { |k, v| "#{k} = #{v}" }.join("\n") end |