Class: Tumblr::Post
- Inherits:
-
Object
- Object
- Tumblr::Post
- Defined in:
- lib/tumblr/post.rb,
lib/tumblr/post/chat.rb,
lib/tumblr/post/link.rb,
lib/tumblr/post/text.rb,
lib/tumblr/post/audio.rb,
lib/tumblr/post/photo.rb,
lib/tumblr/post/quote.rb,
lib/tumblr/post/video.rb,
lib/tumblr/post/answer.rb
Overview
A Tumblr::Post object can be serialized into a YAML front-matter formatted string, and provides convenient ways to publish, edit, and delete to the API. Don’t call #new directly, instead use Post::create to instantiate a subclass.
Defined Under Namespace
Classes: Answer, Audio, Chat, Link, Photo, Quote, Text, Video
Constant Summary collapse
- FIELDS =
[ :blog_name, :id, :post_url, :type, :timestamp, :date, :format, :reblog_key, :tags, :bookmarklet, :mobile, :source_url, :source_title, :total_posts, :photos, :dialogue, :player, :text, :question, :asking_name, :asking_url # Post-specific response fields ]
- POST_BODY_SEPARATOR =
Some post types have several “body keys”, which allow the YAML front-matter serialization to seem a bit more human. This separator separates those keys.
"\n\n"
Class Method Summary collapse
-
.create(post_response) ⇒ Object
Insantiate a subclass of Tumblr::Post, corresponding to the post’s type.
-
.dump(post) ⇒ Object
Serialize a post.
-
.get_post_type(type) ⇒ Object
Get a subclass of Tumblr::Post based on a type token.
- .infer_post_type_from_extname(extname) ⇒ Object
- .infer_post_type_from_string(str) ⇒ Object
-
.load(doc) ⇒ Object
Transform a yaml front matter formatted String into a subclass of Tumblr::Post.
- .load_from_binary(file, post_type = nil) ⇒ Object
-
.load_from_path(path) ⇒ Object
Load a document and transform into a post via file path.
-
.pair_post_body_types(keys, values) ⇒ Object
Pair the post body keys for a particular post type with a list of values.
-
.parse(doc) ⇒ Object
Transform a yaml front matter formatted String into a set of parameters to create a post.
-
.perform(request) ⇒ Object
Given a Request, perform it and transform the response into a list of Post objects.
-
.post_body_keys ⇒ Object
A post_body_key determines what parts of the serialization map to certain fields in the post request.
Instance Method Summary collapse
- #date ⇒ Object
-
#delete(client) ⇒ Object
Given a client, delete this post.
- #draft! ⇒ Object
- #draft? ⇒ Boolean
-
#edit(client) ⇒ Object
Given a client, edit this post.
- #format ⇒ Object
-
#id ⇒ Object
Below this line are public methods that are used to transform this post into an API request.
-
#initialize(post_response = {}) ⇒ Post
constructor
A new instance of Post.
-
#markdown? ⇒ Boolean
These are handy convenience methods.
-
#meta_data ⇒ Object
Which parts of this post represent it’s meta data (eg. they’re not part of the body).
-
#post(client) ⇒ Object
Given a client, publish this post to tumblr.
- #post_url ⇒ Object
- #private? ⇒ Boolean
- #publish! ⇒ Object
- #published? ⇒ Boolean
- #queue! ⇒ Object
- #queued? ⇒ Boolean
- #reblog_key ⇒ Object
-
#request_parameters ⇒ Object
Transform this Post into a hash ready to be serialized and posted to the API.
-
#serialize ⇒ Object
Transform this post into it’s YAML front-matter post form.
- #slug ⇒ Object
- #state ⇒ Object
- #tags ⇒ Object
- #tweet ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(post_response = {}) ⇒ Post
Returns a new instance of Post.
147 148 149 150 151 152 |
# File 'lib/tumblr/post.rb', line 147 def initialize(post_response = {}) post_response.delete_if {|k,v| !(FIELDS | Tumblr::Client::POST_OPTIONS).map(&:to_s).include? k.to_s } post_response.each_pair do |k,v| instance_variable_set "@#{k}".to_sym, v end end |
Class Method Details
.create(post_response) ⇒ Object
Insantiate a subclass of Tumblr::Post, corresponding to the post’s type.
36 37 38 39 |
# File 'lib/tumblr/post.rb', line 36 def self.create(post_response) type = post_response["type"].to_s.capitalize.to_sym get_post_type(post_response["type"]).new(post_response) end |
.dump(post) ⇒ Object
Serialize a post.
143 144 145 |
# File 'lib/tumblr/post.rb', line 143 def self.dump(post) post.serialize end |
.get_post_type(type) ⇒ Object
Get a subclass of Tumblr::Post based on a type token.
42 43 44 |
# File 'lib/tumblr/post.rb', line 42 def self.get_post_type(type) const_get type.to_s.capitalize.to_sym end |
.infer_post_type_from_extname(extname) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/tumblr/post.rb', line 103 def self.infer_post_type_from_extname(extname) require 'rack' mime_type = Rack::Mime.mime_type extname case mime_type.split("/").first when "image" :photo when "video" :video when "audio" :audio else :text end end |
.infer_post_type_from_string(str) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/tumblr/post.rb', line 118 def self.infer_post_type_from_string(str) require 'uri' video_hosts = ["youtube.com", "vimeo.com", "youtu.be"] audio_hosts = ["open.spotify.com", "soundcloud.com", "snd.sc"] url = URI.parse(str) if url.is_a?(URI::HTTP) return :video if video_hosts.find {|h| url.host.include?(h) } return :audio if audio_hosts.find {|h| url.host.include?(h) } :link elsif url.scheme.eql?("spotify") :audio else :text end rescue URI::InvalidURIError :text end |
.load(doc) ⇒ Object
Transform a yaml front matter formatted String into a subclass of Tumblr::Post
47 48 49 |
# File 'lib/tumblr/post.rb', line 47 def self.load(doc) create parse(doc) end |
.load_from_binary(file, post_type = nil) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/tumblr/post.rb', line 62 def self.load_from_binary(file, post_type = nil) file_size_in_mb = File.size(file.path).to_f / 2**20 raise ArgumentError, "File size is greater than 5 MB (Tumblr's limit)" if file_size_in_mb > 5 post_type ||= infer_post_type_from_extname File.extname(file.path) get_post_type(post_type).new "data" => file.read end |
.load_from_path(path) ⇒ Object
Load a document and transform into a post via file path
52 53 54 55 56 57 58 59 60 |
# File 'lib/tumblr/post.rb', line 52 def self.load_from_path(path) raise ArgumentError, "Given path: #{path} is not a file" unless File.file? File.(path) post_type = infer_post_type_from_extname File.extname(path) if post_type == :text load File.read(File.(path)) else load_from_binary File.new(File.(path), "rb"), post_type end end |
.pair_post_body_types(keys, values) ⇒ Object
Pair the post body keys for a particular post type with a list of values. If the length list of values is greater than the list of keys, the last key should be paired with the remaining values joined together.
96 97 98 99 100 101 |
# File 'lib/tumblr/post.rb', line 96 def self.pair_post_body_types(keys, values) values.fill(keys.length - 1) do |i| values[keys.length - 1, values.length].join(POST_BODY_SEPARATOR) end keys.map(&:to_s).zip values end |
.parse(doc) ⇒ Object
Transform a yaml front matter formatted String into a set of parameters to create a post.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/tumblr/post.rb', line 70 def self.parse(doc) doc =~ /^(\s*---(.*?)---\s*)/m if Regexp.last_match = YAML.load(Regexp.last_match[2].strip) doc_body = doc.sub(Regexp.last_match[1],'').strip else = {} doc_body = doc end ["type"] ||= infer_post_type_from_string(doc_body) ["format"] ||= "markdown" post_type = get_post_type(["type"]) pairs = if post_type.post_body_keys.length > 1 pair_post_body_types(post_type.post_body_keys, doc_body.split(POST_BODY_SEPARATOR).dup) else [[post_type.post_body_keys.first, doc_body]] end Hash[pairs].merge() end |
.perform(request) ⇒ Object
Given a Request, perform it and transform the response into a list of Post objects.
28 29 30 31 32 33 |
# File 'lib/tumblr/post.rb', line 28 def self.perform(request) response = request.perform posts = response.parse["response"]["posts"] (posts || []).map{|post| self.create(post) } end |
.post_body_keys ⇒ Object
A post_body_key determines what parts of the serialization map to certain fields in the post request.
138 139 140 |
# File 'lib/tumblr/post.rb', line 138 def self.post_body_keys [:body] end |
Instance Method Details
#date ⇒ Object
222 223 224 |
# File 'lib/tumblr/post.rb', line 222 def date @date end |
#delete(client) ⇒ Object
Given a client, delete this post.
174 175 176 177 |
# File 'lib/tumblr/post.rb', line 174 def delete(client) raise "Must have an id to delete a post" unless id client.delete(:id => id) end |
#draft! ⇒ Object
268 269 270 |
# File 'lib/tumblr/post.rb', line 268 def draft! @state ="draft" end |
#draft? ⇒ Boolean
248 249 250 |
# File 'lib/tumblr/post.rb', line 248 def draft? @state.to_s == "draft" end |
#edit(client) ⇒ Object
Given a client, edit this post.
168 169 170 171 |
# File 'lib/tumblr/post.rb', line 168 def edit(client) raise "Must have an id to edit a post" unless id client.edit(request_parameters) end |
#format ⇒ Object
226 227 228 |
# File 'lib/tumblr/post.rb', line 226 def format @format end |
#id ⇒ Object
Below this line are public methods that are used to transform this post into an API request.
194 195 196 |
# File 'lib/tumblr/post.rb', line 194 def id @id.to_i unless @id.nil? end |
#markdown? ⇒ Boolean
These are handy convenience methods.
240 241 242 |
# File 'lib/tumblr/post.rb', line 240 def markdown? @format.to_s == "markdown" end |
#meta_data ⇒ Object
Which parts of this post represent it’s meta data (eg. they’re not part of the body).
188 189 190 |
# File 'lib/tumblr/post.rb', line 188 def request_parameters.reject {|k,v| self.class.post_body_keys.include?(k.to_sym) } end |
#post(client) ⇒ Object
Given a client, publish this post to tumblr.
163 164 165 |
# File 'lib/tumblr/post.rb', line 163 def post(client) client.post(request_parameters) end |
#post_url ⇒ Object
234 235 236 |
# File 'lib/tumblr/post.rb', line 234 def post_url @post_url end |
#private? ⇒ Boolean
256 257 258 |
# File 'lib/tumblr/post.rb', line 256 def private? @state.to_s == "private" end |
#publish! ⇒ Object
260 261 262 |
# File 'lib/tumblr/post.rb', line 260 def publish! @state = "published" end |
#published? ⇒ Boolean
244 245 246 |
# File 'lib/tumblr/post.rb', line 244 def published? @state.to_s == "published" end |
#queue! ⇒ Object
264 265 266 |
# File 'lib/tumblr/post.rb', line 264 def queue! @state = "queue" end |
#queued? ⇒ Boolean
252 253 254 |
# File 'lib/tumblr/post.rb', line 252 def queued? @state.to_s == "queued" or @state.to_s == "queue" end |
#reblog_key ⇒ Object
202 203 204 |
# File 'lib/tumblr/post.rb', line 202 def reblog_key @reblog_key end |
#request_parameters ⇒ Object
Transform this Post into a hash ready to be serialized and posted to the API. This looks for the fields of Tumblr::Client::POST_OPTIONS as methods on the object.
181 182 183 184 185 |
# File 'lib/tumblr/post.rb', line 181 def request_parameters Hash[(Tumblr::Client::POST_OPTIONS | [:id, :type]).map {|key| [key.to_s, send(key)] if respond_to?(key) && send(key) }] end |
#serialize ⇒ Object
Transform this post into it’s YAML front-matter post form.
155 156 157 158 159 160 |
# File 'lib/tumblr/post.rb', line 155 def serialize buffer = YAML.dump() buffer << "---\x0D\x0A" buffer << post_body buffer end |
#slug ⇒ Object
230 231 232 |
# File 'lib/tumblr/post.rb', line 230 def slug @slug end |
#state ⇒ Object
206 207 208 |
# File 'lib/tumblr/post.rb', line 206 def state @state end |
#tags ⇒ Object
210 211 212 213 214 215 216 |
# File 'lib/tumblr/post.rb', line 210 def if @tags.respond_to? :join @tags.join(",") else @tags end end |
#tweet ⇒ Object
218 219 220 |
# File 'lib/tumblr/post.rb', line 218 def tweet @tweet end |
#type ⇒ Object
198 199 200 |
# File 'lib/tumblr/post.rb', line 198 def type @type.to_s end |