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 34 35 36 37 |
# 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 = begin @data.trail.first.blog.name rescue '<no ID>' end # 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)
172 173 174 175 176 177 |
# File 'lib/draftking/posts/post.rb', line 172 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
159 160 161 162 |
# File 'lib/draftking/posts/post.rb', line 159 def () = csv_to_a() if .is_a? String @tags += end |
#change_state(state) ⇒ Object
Change the state of a post
62 63 64 65 66 67 |
# File 'lib/draftking/posts/post.rb', line 62 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
153 154 155 156 |
# File 'lib/draftking/posts/post.rb', line 153 def @changed = true unless @tags.empty? @tags = [] end |
#delete(client:, simulate: nil) ⇒ Object
Delete a Post
87 88 89 90 91 92 |
# File 'lib/draftking/posts/post.rb', line 87 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
142 143 144 145 146 147 148 149 150 |
# File 'lib/draftking/posts/post.rb', line 142 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
79 80 81 82 |
# File 'lib/draftking/posts/post.rb', line 79 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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/draftking/posts/post.rb', line 97 def reblog(client:, simulate: nil) return 1 if simulate retries = 0 begin client.reblog @blog_url, id: id, reblog_key: @reblog_key, comment: @comment rescue retries += 1 retry unless retries > MAX_RETRY raise IOError, 'Connection to Tumblr timed-out!' end end |
#replace_comment_with(comment) ⇒ Object
Add a comment to a post
71 72 73 74 75 |
# File 'lib/draftking/posts/post.rb', line 71 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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/draftking/posts/post.rb', line 115 def save(client:, simulate: nil) return 0 unless @changed return @saved = 1 if simulate retries = 0 begin res = client.edit @blog_url, id: id, reblog_key: @reblog_key, state: validate_state, attach_reblog_tree: @keep_tree, tags: @tags.join(','), caption: @comment rescue retries += 1 retry unless retries > MAX_RETRY raise IOError, 'Connection to Tumblr timed-out!' end return 0 unless res && res['id'] @changed = false @saved = 1 end |
#to_h ⇒ Object
Hash of post data
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/draftking/posts/post.rb', line 45 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
40 41 42 |
# File 'lib/draftking/posts/post.rb', line 40 def to_s to_h.map { |k, v| "#{k} = #{v}" }.join("\n") end |