class DK::Post

Tumblr Post

Attributes

blog_url[RW]
changed[RW]
comment[RW]
data[RW]
image[RW]
photoset[RW]
reblog_key[RW]
saved[RW]
state[RW]
summary[RW]
tags[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

add_tags(tags) click to toggle source

Appends CSV or array of tags

# File lib/draftking/posts/post.rb, line 159
def add_tags(tags)
  tags = csv_to_a(tags) if tags.is_a? String
  @tags += tags
end
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
clear_tags() click to toggle source

Remove existing Post tags

# File lib/draftking/posts/post.rb, line 153
def clear_tags
  @changed = true unless @tags.empty?
  @tags = []
end
delete(client:, simulate: nil) click to toggle source

Delete a Post @param client [Tumblr::Client] Instance of Tumblr Client @param simulate [Bool] Simulate Action?

# 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) click to toggle source

Generate post tags from post comment @param keep_tags [Bool] Preserve Existing Tags? @param add_tags [String] New tags @param exclude [String] Tags to exclude @param credit [Bool] Give draftking a tag credit

# File lib/draftking/posts/post.rb, line 142
def generate_tags(keep_tags: nil, add_tags: nil, exclude: nil, credit: false)
  tags  = comment_to_tags(@comment)
  tags += csv_to_a(add_tags)    if add_tags
  tags += @tags                 if keep_tags
  tags << DK::CREDIT_TAG        if credit
  tags -= csv_to_a(exclude)     if exclude
  @changed = true unless @tags.sort.uniq == tags.sort.uniq
  @tags = tags
end
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

comment_to_tags(comment) click to toggle source
# File lib/draftking/posts/post.rb, line 191
def comment_to_tags(comment)
  comment.gsub(HTML_TAG_PATTERN, '') # Remove HTML Tags
         .gsub(%r{[\/\\|]}, ',')     # Convert Separators
         .gsub(' , ', ',')           # Clean up tags
         .split(',')                 # Return array
end
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