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?



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.tags

  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_urlObject

Returns the value of attribute blog_url.



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

def blog_url
  @blog_url
end

#changedObject

Returns the value of attribute changed.



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

def changed
  @changed
end

#commentObject

Returns the value of attribute comment.



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

def comment
  @comment
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#imageObject

Returns the value of attribute image.



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

def image
  @image
end

#photosetObject

Returns the value of attribute photoset.



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

def photoset
  @photoset
end

#reblog_keyObject

Returns the value of attribute reblog_key.



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

def reblog_key
  @reblog_key
end

#savedObject

Returns the value of attribute saved.



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

def saved
  @saved
end

#stateObject

Returns the value of attribute state.



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

def state
  @state
end

#summaryObject

Returns the value of attribute summary.



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

def summary
  @summary
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @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 add_tags(tags)
  tags = csv_to_a(tags) if tags.is_a? String
  @tags += tags
end

#change_state(state) ⇒ Object

Change the state of a post

Parameters:

  • state (String)

    New State



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_tagsObject

Remove existing Post tags



135
136
137
138
# File 'lib/draftking/posts/post.rb', line 135

def clear_tags
  @changed = true unless @tags.empty?
  @tags = []
end

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

Delete a Post

Parameters:

  • client (Tumblr::Client)

    Instance of Tumblr Client

  • simulate (Bool) (defaults to: nil)

    Simulate Action?



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

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: false)

    Give draftking a tag credit



124
125
126
127
128
129
130
131
132
# File 'lib/draftking/posts/post.rb', line 124

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) ⇒ Boolean

Check if a post needs to be modified

Parameters:

  • key_text (String)

    key_text

Returns:

  • (Boolean)


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

Parameters:

  • client (Tumblr::Client)

    Instance of Tumblr Client

  • simulate (Bool) (defaults to: nil)

    Simulate Action?



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

Parameters:

  • comment (String)

    New Comment



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

Parameters:

  • client (Tumblr::Client)

    Instance of Tumblr Client

  • simulate (Bool) (defaults to: nil)

    Simulate Action?



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_hObject

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_sObject

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