Class: Tumblr::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/tumblr/post.rb,
lib/tumblr/post/link.rb,
lib/tumblr/post/audio.rb,
lib/tumblr/post/photo.rb,
lib/tumblr/post/quote.rb,
lib/tumblr/post/video.rb,
lib/tumblr/post/regular.rb,
lib/tumblr/post/conversation.rb

Direct Known Subclasses

Audio, Conversation, Link, Photo, Quote, Regular, Video

Defined Under Namespace

Classes: Audio, Conversation, Link, Photo, Quote, Regular, Video

Constant Summary collapse

BASIC_PARAMS =
[:date,:tags,:format,:group,:generator,:private,
:slug,:state,:'send-to-twitter',:'publish-on',:'reblog-key']
POST_PARAMS =
[:title,:body,:source,:caption,:'click-through-url',
:quote,:name,:url,:description,:conversation,
:embed,:'externally-hosted-url']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(post_id = nil) ⇒ Post

Returns a new instance of Post.



22
23
24
# File 'lib/tumblr/post.rb', line 22

def initialize(post_id = nil)
  @post_id = post_id if post_id
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



20
21
22
# File 'lib/tumblr/post.rb', line 20

def date
  @date
end

#formatObject

Returns the value of attribute format.



19
20
21
# File 'lib/tumblr/post.rb', line 19

def format
  @format
end

#generatorObject

Returns the value of attribute generator.



20
21
22
# File 'lib/tumblr/post.rb', line 20

def generator
  @generator
end

#groupObject

Returns the value of attribute group.



20
21
22
# File 'lib/tumblr/post.rb', line 20

def group
  @group
end

#post_idObject (readonly)

Returns the value of attribute post_id.



19
20
21
# File 'lib/tumblr/post.rb', line 19

def post_id
  @post_id
end

#reblog_keyObject

Returns the value of attribute reblog_key.



20
21
22
# File 'lib/tumblr/post.rb', line 20

def reblog_key
  @reblog_key
end

#slugObject

Returns the value of attribute slug.



20
21
22
# File 'lib/tumblr/post.rb', line 20

def slug
  @slug
end

#stateObject

Returns the value of attribute state.



19
20
21
# File 'lib/tumblr/post.rb', line 19

def state
  @state
end

#typeObject (readonly)

Returns the value of attribute type.



19
20
21
# File 'lib/tumblr/post.rb', line 19

def type
  @type
end

Class Method Details

.parameters(*attributes) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/tumblr/post.rb', line 11

def self.parameters(*attributes)
  if !attributes.blank?
    @parameters = attributes
    attr_accessor *@parameters
  end
  @parameters
end

Instance Method Details

#add_to_queue(email, password, pubdate = nil) ⇒ Object

Adds to Queue. Pass an additional date to publish at a specific date.



122
123
124
125
126
127
# File 'lib/tumblr/post.rb', line 122

def add_to_queue(email, password, pubdate = nil)
  self.state = :queue
  self.publish_on(pubdate) if pubdate
  return edit(email,password) if post_id
  write(email,password)
end

#delete(email, password) ⇒ Object



91
92
93
# File 'lib/tumblr/post.rb', line 91

def delete(email, password)
  Writer.new(email,password).delete(to_h)
end

#edit(email, password) ⇒ Object



87
88
89
# File 'lib/tumblr/post.rb', line 87

def edit(email, password)
  Writer.new(email,password).edit(to_h)
end

#like(email, password) ⇒ Object



95
96
97
98
99
# File 'lib/tumblr/post.rb', line 95

def like(email,password)
  if (post_id && reblog_key)
    Reader.new(email,password).like(:'post-id' => post_id, :'reblog-key' => reblog_key)
  end
end

#private=(bool) ⇒ Object



26
27
28
# File 'lib/tumblr/post.rb', line 26

def private=(bool)
  @private = bool ? true : false
end

#private?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/tumblr/post.rb', line 30

def private?
  @private
end

#publish_now(email, password) ⇒ Object

Write to Tumblr and set state to Publish



108
109
110
111
112
# File 'lib/tumblr/post.rb', line 108

def publish_now(email, password)
  self.state = :published
  return edit(email,password) if post_id
  write(email,password)
end

#publish_on(pubdate = nil) ⇒ Object



65
66
67
68
# File 'lib/tumblr/post.rb', line 65

def publish_on(pubdate=nil)
  @publish_on = pubdate if state.eql?(:queue) && pubdate
  @publish_on
end

#save_as_draft(email, password) ⇒ Object

Save as a draft



115
116
117
118
119
# File 'lib/tumblr/post.rb', line 115

def save_as_draft(email, password)
  self.state = :draft
  return edit(email,password) if post_id
  write(email,password)
end

#send_to_twitter(status = false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/tumblr/post.rb', line 54

def send_to_twitter(status=false)
  if status
    if status.to_sym.eql?(:no)
      @send_to_twitter = false
    else
      @send_to_twitter = status
    end
  end
  @send_to_twitter
end

#tags(*post_tags) ⇒ Object



34
35
36
37
# File 'lib/tumblr/post.rb', line 34

def tags(*)
  @tags = .join(',') if !.blank?
  @tags
end

#to_hObject

Convert to a hash to be used in post writing/editing



71
72
73
74
75
76
77
78
79
80
# File 'lib/tumblr/post.rb', line 71

def to_h
  post_hash = {}
  basics = [:post_id, :type, :date, :tags, :format, :group, :generator,
            :slug, :state, :send_to_twitter, :publish_on, :reblog_key]
  params = basics.select {|opt| respond_to?(opt) && send(opt) }
  params |= self.class.parameters.select {|opt| send(opt) } unless self.class.parameters.blank?
  params.each { |key| post_hash[key.to_s.gsub('_','-').to_sym] = send(key) } unless params.empty?
  post_hash[:private] = 1 if private?
  post_hash
end

#to_sObject

Convert post to a string for writing to a file



138
139
140
141
142
143
# File 'lib/tumblr/post.rb', line 138

def to_s
  post_string = YAML.dump(post_data)
  post_string += "---\x0D\x0A"
  post_string += YAML.load(to_yaml)['body']
  post_string
end

#to_yamlObject

Convert post to a YAML representation



130
131
132
133
134
135
# File 'lib/tumblr/post.rb', line 130

def to_yaml
  post = {}
  post['data'] = post_data
  post['body'] = to_h[post_body].to_s
  YAML.dump(post)
end

#unlike(email, password) ⇒ Object



101
102
103
104
105
# File 'lib/tumblr/post.rb', line 101

def unlike(email,password)
  if (post_id && reblog_key)
    Reader.new(email,password).unlike(:'post-id' => post_id, :'reblog-key' => reblog_key)
  end
end

#write(email, password) ⇒ Object

Publish this post to Tumblr



83
84
85
# File 'lib/tumblr/post.rb', line 83

def write(email, password)
  Writer.new(email,password).write(to_h)
end