Class: DK::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/draftking/posts/post.rb

Overview

tumblr Post

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, keep_tree: nil) ⇒ Post

Returns a new instance of Post.

Parameters:

  • hash (Hash)

    Post Data

  • keep_tree (Bool) (defaults to: nil)

    Attach Reblog Tree?



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/draftking/posts/post.rb', line 10

def initialize(hash, keep_tree: nil)
  return if hash.nil?
  @id         = hash['id']
  @state      = hash['state'] || DK::DRAFT
  @tags       = hash['tags']
  @comment    = hash['reblog']['comment']
  @summary    = hash['summary']
  @blog_url   = tumblr_url(hash['blog_name'])
  @reblog_key = hash['reblog_key']
  @keep_tree  = keep_tree.nil? ? false : keep_tree
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



7
8
9
# File 'lib/draftking/posts/post.rb', line 7

def comment
  @comment
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/draftking/posts/post.rb', line 7

def id
  @id
end

#keep_treeObject

Returns the value of attribute keep_tree.



7
8
9
# File 'lib/draftking/posts/post.rb', line 7

def keep_tree
  @keep_tree
end

#reblog_keyObject

Returns the value of attribute reblog_key.



7
8
9
# File 'lib/draftking/posts/post.rb', line 7

def reblog_key
  @reblog_key
end

#stateObject

Returns the value of attribute state.



7
8
9
# File 'lib/draftking/posts/post.rb', line 7

def state
  @state
end

#summaryObject

Returns the value of attribute summary.



7
8
9
# File 'lib/draftking/posts/post.rb', line 7

def summary
  @summary
end

#tagsObject

Returns the value of attribute tags.



7
8
9
# File 'lib/draftking/posts/post.rb', line 7

def tags
  @tags
end

Instance Method Details

#change_state(state:) ⇒ Object

Change the state of a post

Parameters:

  • state (String)

    New State



36
37
38
39
40
41
# File 'lib/draftking/posts/post.rb', line 36

def change_state(state:)
  return false unless VALID_STATE.include?(state)
  return false if @state == state
  @state = state
  true
end

#delete(client:, simulate: nil) ⇒ Object

Delete a Post

Parameters:

  • client (Tumblr::Client)

    Instance of tumblr Client

  • simulate (Bool) (defaults to: nil)

    Simulate Action?



61
62
63
64
65
# File 'lib/draftking/posts/post.rb', line 61

def delete(client:, simulate: nil)
  return 1 if simulate
  res = client.delete @blog_url, @id
  res['id'] ? 1 : 0
end

#generate_tags(keep_tags: nil, add_tags: nil, exclude: nil, credit: true) ⇒ Object

Generate post tags from post comment

Parameters:

  • keep_tags (Bool) (defaults to: nil)

    Preserve Existing Tags?

  • add_tags (String) (defaults to: nil)

    New tags

  • exclude (String) (defaults to: nil)

    Tags to exclude

  • credit (Bool) (defaults to: true)

    Give draftking a tag credit



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/draftking/posts/post.rb', line 98

def generate_tags(keep_tags: nil, add_tags: nil, exclude: nil, credit: true)
  tags  = @comment.gsub(%r{<(/)?p>}, '').gsub(%r{[\/\\|]}, ',')
  tags  = tags.gsub(' , ', ',').gsub(@comment, '')
  tags += ',' + add_tags if add_tags
  tags += ',' + @tags.join(',') if keep_tags
  tags.gsub!(exclude.to_s, '')
  tags += ',' + DK::CREDIT_TAG if credit
  tags.gsub!(/^\s*(,)*/, '') # Remove leading commas
  return @tags = tags unless @tags.join(',') == tags
  false
end

#passes_filter?(filter:) ⇒ Boolean

Check if a post needs to be modified

Parameters:

  • filter (String)

    Filter

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/draftking/posts/post.rb', line 53

def passes_filter?(filter:)
  return true if filter.nil?
  @comment.include?(filter)
end

#reblog(client:, simulate: nil) ⇒ Object

Reblog a Post

Parameters:

  • client (Tumblr::Client)

    Instance of tumblr Client

  • simulate (Bool) (defaults to: nil)

    Simulate Action?



70
71
72
73
74
75
76
# File 'lib/draftking/posts/post.rb', line 70

def reblog(client:, simulate: nil)
  return 1 if simulate
  client.reblog @blog_url,
                id: @id,
                reblog_key: @reblog_key,
                comment: @comment
end

#replace_comment(comment:) ⇒ Object

Add a comment to a post

Parameters:

  • comment (String)

    New Comment



45
46
47
48
49
# File 'lib/draftking/posts/post.rb', line 45

def replace_comment(comment:)
  return false if comment.nil? || @comment.include?(comment)
  @comment = comment || @comment
  true
end

#save(client:, simulate: nil) ⇒ Object

Save a post

Parameters:

  • client (Tumblr::Client)

    Instance of tumblr Client

  • simulate (Bool) (defaults to: nil)

    Simulate Action?



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/draftking/posts/post.rb', line 81

def save(client:, simulate: nil)
  return 1 if simulate
  res = client.edit @blog_url,
                    id:                 @id,
                    reblog_key:         @reblog_key,
                    state:              @state,
                    attach_reblog_tree: @keep_tree,
                    tags:               @tags,
                    caption:            @comment
  res['id'] ? 1 : 0
end

#to_sObject

String of post data



23
24
25
26
27
28
29
30
31
32
# File 'lib/draftking/posts/post.rb', line 23

def to_s
  "id = #{@id}\n" \
    "state = #{@state}\n" \
    "tags = #{@tags}\n" \
    "comment = #{@comment}\n" \
    "summary = #{@summary}\n" \
    "blog_url = #{@blog_url}\n" \
    "reblog_key = #{@reblog_key}\n" \
    "keep_tree = #{@keep_tree}\n"
end