class DK::Post
Attributes
blog_url[RW]
changed[RW]
comment[RW]
data[RW]
image[RW]
photoset[RW]
reblog_key[RW]
saved[RW]
state[RW]
summary[RW]
Public Class Methods
new(hash, keep_tree: nil)
click to toggle source
@param hash [Hash] Post
Data @param keep_tree [Bool] Attach Reblog Tree?
# 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.tags make_accessors(instance_variables) end
Public Instance Methods
change_state(state)
click to toggle source
Change the state of a post @param state [String] New State
# 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
delete(client:, simulate: nil)
click to toggle source
has_key_text?(key_text)
click to toggle source
Check if a post needs to be modified @param key_text [String] key_text
# 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)
click to toggle source
Reblog a Post
@param client [Tumblr::Client] Instance of Tumblr
Client
@param simulate [Bool] Simulate Action?
# 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)
click to toggle source
Add a comment to a post @param comment [String] New Comment
# 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)
click to toggle source
Save a post @param client [Tumblr::Client] Instance of Tumblr
Client
@param simulate [Bool] Simulate Action?
# 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()
click to toggle source
Hash of post data
# 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()
click to toggle source
String of post data
# File lib/draftking/posts/post.rb, line 40 def to_s to_h.map { |k, v| "#{k} = #{v}" }.join("\n") end
Private Instance Methods
csv_to_a(csv)
click to toggle source
# File lib/draftking/posts/post.rb, line 198 def csv_to_a(csv) csv.split(',') end
make_accessors(keys)
click to toggle source
# File lib/draftking/posts/post.rb, line 166 def make_accessors(keys) for key in keys singleton_class.class_eval { attr_accessor key.to_s.delete('@') } end end
method_missing(method, *args)
click to toggle source
# 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
original_image_url()
click to toggle source
# File lib/draftking/posts/post.rb, line 179 def original_image_url return nil unless @data.photos @data.photos.first.original_size.url unless @data.photos.empty? end
process_state(state)
click to toggle source
# File lib/draftking/posts/post.rb, line 184 def process_state(state) return DK::DRAFT unless state || state.empty return DK::QUEUE if state == 'queued' return DK::PUBLISH if state.include?('pub') state end
validate_state()
click to toggle source
# File lib/draftking/posts/post.rb, line 202 def validate_state raise "Invalid Post.state: #{@state}" unless VALID_STATE.include?(@state) @state end