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 # 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.
- #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.
144 145 146 147 148 149 |
# File 'lib/tumblr/post.rb', line 144 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.
140 141 142 |
# File 'lib/tumblr/post.rb', line 140 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/tumblr/post.rb', line 100 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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/tumblr/post.rb', line 115 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.
93 94 95 96 97 98 |
# File 'lib/tumblr/post.rb', line 93 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 |
# 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"]) post_body_parts = doc_body.split(POST_BODY_SEPARATOR) pairs = pair_post_body_types(post_type.post_body_keys, post_body_parts.dup) 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.
135 136 137 |
# File 'lib/tumblr/post.rb', line 135 def self.post_body_keys [:body] end |
Instance Method Details
#date ⇒ Object
219 220 221 |
# File 'lib/tumblr/post.rb', line 219 def date @date end |
#delete(client) ⇒ Object
Given a client, delete this post.
171 172 173 174 |
# File 'lib/tumblr/post.rb', line 171 def delete(client) raise "Must have an id to delete a post" unless id client.delete(:id => id) end |
#draft! ⇒ Object
261 262 263 |
# File 'lib/tumblr/post.rb', line 261 def draft! @state ="draft" end |
#draft? ⇒ Boolean
241 242 243 |
# File 'lib/tumblr/post.rb', line 241 def draft? @state.to_s == "draft" end |
#edit(client) ⇒ Object
Given a client, edit this post.
165 166 167 168 |
# File 'lib/tumblr/post.rb', line 165 def edit(client) raise "Must have an id to edit a post" unless id client.edit(request_parameters) end |
#format ⇒ Object
223 224 225 |
# File 'lib/tumblr/post.rb', line 223 def format @format end |
#id ⇒ Object
Below this line are public methods that are used to transform this post into an API request.
191 192 193 |
# File 'lib/tumblr/post.rb', line 191 def id @id.to_i unless @id.nil? end |
#markdown? ⇒ Boolean
These are handy convenience methods.
233 234 235 |
# File 'lib/tumblr/post.rb', line 233 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).
185 186 187 |
# File 'lib/tumblr/post.rb', line 185 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.
160 161 162 |
# File 'lib/tumblr/post.rb', line 160 def post(client) client.post(request_parameters) end |
#private? ⇒ Boolean
249 250 251 |
# File 'lib/tumblr/post.rb', line 249 def private? @state.to_s == "private" end |
#publish! ⇒ Object
253 254 255 |
# File 'lib/tumblr/post.rb', line 253 def publish! @state = "published" end |
#published? ⇒ Boolean
237 238 239 |
# File 'lib/tumblr/post.rb', line 237 def published? @state.to_s == "published" end |
#queue! ⇒ Object
257 258 259 |
# File 'lib/tumblr/post.rb', line 257 def queue! @state = "queue" end |
#queued? ⇒ Boolean
245 246 247 |
# File 'lib/tumblr/post.rb', line 245 def queued? @state.to_s == "queued" or @state.to_s == "queue" end |
#reblog_key ⇒ Object
199 200 201 |
# File 'lib/tumblr/post.rb', line 199 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.
178 179 180 181 182 |
# File 'lib/tumblr/post.rb', line 178 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.
152 153 154 155 156 157 |
# File 'lib/tumblr/post.rb', line 152 def serialize buffer = YAML.dump() buffer << "---\x0D\x0A" buffer << post_body buffer end |
#slug ⇒ Object
227 228 229 |
# File 'lib/tumblr/post.rb', line 227 def slug @slug end |
#state ⇒ Object
203 204 205 |
# File 'lib/tumblr/post.rb', line 203 def state @state end |
#tags ⇒ Object
207 208 209 210 211 212 213 |
# File 'lib/tumblr/post.rb', line 207 def if @tags.respond_to? :join @tags.join(",") else @tags end end |
#tweet ⇒ Object
215 216 217 |
# File 'lib/tumblr/post.rb', line 215 def tweet @tweet end |
#type ⇒ Object
195 196 197 |
# File 'lib/tumblr/post.rb', line 195 def type @type.to_s end |