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



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_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



159
160
161
162
# 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) ⇒ Object

Change the state of a post

Parameters:

  • state (String)

    New State



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_tagsObject

Remove existing Post tags



153
154
155
156
# File 'lib/draftking/posts/post.rb', line 153

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?



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

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



142
143
144
145
146
147
148
149
150
# 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) ⇒ Boolean

Check if a post needs to be modified

Parameters:

  • key_text (String)

    key_text

Returns:

  • (Boolean)


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

Parameters:

  • client (Tumblr::Client)

    Instance of Tumblr Client

  • simulate (Bool) (defaults to: nil)

    Simulate Action?



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

Parameters:

  • comment (String)

    New Comment



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

Parameters:

  • client (Tumblr::Client)

    Instance of Tumblr Client

  • simulate (Bool) (defaults to: nil)

    Simulate Action?



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_hObject

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_sObject

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