Class: Blogger::Post
Overview
Post
The post is the representation of a post on your blogger.com blog. It can handle the title, content, categories, and draft status of the post. These are used for uploading posts (just set the information to your liking) or retrieving them (read from the structure)
Example:
post = Blogger::Post.new(:title => "Sweet post", :categories = ["awesome", "sweet"])
post.draft = true
post.content = "I'll fill this in later"
Blogger::Account.new('username','password').post(blogid,post)
Constant Summary
Constants included from Formattable
Formattable::ACCEPTABLE_FORMATTERS
Instance Attribute Summary collapse
-
#blog ⇒ Object
reference to the blog we belong to.
-
#categories ⇒ Object
the categories of the post - array of strings.
-
#comments(force_download = false) ⇒ Object
Returns all comments from the post.
-
#content ⇒ Object
the content of the post.
-
#draft ⇒ Object
whether or not the post is a draft.
-
#etag ⇒ Object
:nodoc:.
-
#id ⇒ Object
the id of the post.
-
#title ⇒ Object
the title of the post.
Attributes included from Formattable
Instance Method Summary collapse
-
#comment(*args) ⇒ Object
Submits a comment to this post.
-
#delete ⇒ Object
Deletes the post from your blog.
-
#draft? ⇒ Boolean
Returns whether the post is a draft or not.
-
#initialize(opts = {}) ⇒ Post
constructor
Pass in a hash containing pre-set values if you’d like, including * :title - the title of the post * :content - the content of the post, either marked up or not * :categories - a list of categories, or just one string as a category * :draft - boolean, whether the post is a draft or not * :formatter - the formatter to use.
-
#inspect ⇒ Object
:nodoc:.
-
#parse(entry) ⇒ Object
:nodoc:.
-
#post_to(blog) ⇒ Object
Uploads this post to the provided blog.
-
#reload ⇒ Object
Reloads the post from blogger.com, using ETags for efficiency.
-
#retrieve_comments ⇒ Object
Downloads all comments from the post, and returns them ass Blogger::Comment objects.
-
#save ⇒ Object
(also: #push)
Saves any local changes to the post, and submits them to blogger.
-
#to_s ⇒ Object
Converts the post to an atom entry in string form.
-
#update_base_atom(entry) ⇒ Object
:nodoc:.
Methods included from Formattable
#format_bluecloth, #format_content, #format_haml, #format_maruku, #format_peg_markdown, #format_raw, #format_rdiscount, #format_redcloth
Constructor Details
#initialize(opts = {}) ⇒ Post
Pass in a hash containing pre-set values if you’d like, including
* :title - the title of the post
* :content - the content of the post, either marked up or not
* :categories - a list of categories, or just one string as a category
* :draft - boolean, whether the post is a draft or not
* :formatter - the formatter to use. :raw, :bluecloth, :redcloth, :peg_markdown, :maruku, :haml or :rdiscount
* :blog - the blog this post belongs to
287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/blogger.rb', line 287 def initialize(opts = {}) @categories = [] if opts[:atom] parse opts[:atom] else opts.each do |key, value| next if key =~ /blog/ instance_variable_set("@#{key}".to_sym, value) end end @blog = opts[:blog] @categories = [@categories] unless @categories.is_a? Array @formatter = (opts[:formatter]) ? opts[:formatter] : :raw end |
Instance Attribute Details
#blog ⇒ Object
reference to the blog we belong to
276 277 278 |
# File 'lib/blogger.rb', line 276 def blog @blog end |
#categories ⇒ Object
the categories of the post - array of strings
270 271 272 |
# File 'lib/blogger.rb', line 270 def categories @categories end |
#comments(force_download = false) ⇒ Object
Returns all comments from the post. Passing true
to this method will cause a forced re-download of comments
274 275 276 |
# File 'lib/blogger.rb', line 274 def comments @comments end |
#content ⇒ Object
the content of the post
268 269 270 |
# File 'lib/blogger.rb', line 268 def content @content end |
#draft ⇒ Object
whether or not the post is a draft
272 273 274 |
# File 'lib/blogger.rb', line 272 def draft @draft end |
#etag ⇒ Object
:nodoc:
277 278 279 |
# File 'lib/blogger.rb', line 277 def etag @etag end |
#id ⇒ Object
the id of the post
264 265 266 |
# File 'lib/blogger.rb', line 264 def id @id end |
#title ⇒ Object
the title of the post
266 267 268 |
# File 'lib/blogger.rb', line 266 def title @title end |
Instance Method Details
#comment(*args) ⇒ Object
Submits a comment to this post. You can use 2 methods of submitting your comment:
my_comment = Comment.new(:title => "cool", :content => "I *loved* this post!", :formatter => :rdiscount)
mypost.comment(my_comment)
or, more easily
mypost.comment(:title => "cool", :content => "I *loved* this post!", :formatter => :rdiscount)
The currently authenticated user will be the comment author. This is a limitation of Blogger (and probably a good one!)
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/blogger.rb', line 418 def comment(*args) comm = (args[0].is_a? Blogger::Comment) ? args[0] : Blogger::Comment.new(args[0]) comm.post = self NotLoggedInError.new("You aren't logged into Blogger.").raise unless @blog.account.authenticated? path = "/feeds/#{@blog.id}/#{@id}/comments/default" data = comm.to_s puts data+"\n\n" resp = GoogleAuth.post(path, data, @blog.account.auth_token) raise Blogger::PostingError.new("Error while commenting to post #{@title}: #{resp.}") unless resp.code.eql? '201' comm.parse Atom::Entry.parse(resp.body) end |
#delete ⇒ Object
Deletes the post from your blog.
336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/blogger.rb', line 336 def delete NotLoggedInError.new("You aren't logged into Blogger.").raise unless @blog.account.authenticated? path = "/feeds/#{@blog.id}/posts/default/#{@id}" resp = GoogleAuth.delete(path,@blog.account.auth_token, @etag) raise Blogger::PostingError.new("Error while deleting post \"#{@title}\": #{resp.}") unless resp.code.eql? '200' @blog.posts.delete self self end |
#draft? ⇒ Boolean
Returns whether the post is a draft or not
384 385 386 |
# File 'lib/blogger.rb', line 384 def draft? @draft end |
#inspect ⇒ Object
:nodoc:
403 404 405 |
# File 'lib/blogger.rb', line 403 def inspect #:nodoc: {:title => @title, :content => @content, :categories => @categories, :draft => @draft}.to_yaml end |
#parse(entry) ⇒ Object
:nodoc:
302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/blogger.rb', line 302 def parse(entry) #:nodoc: @atom = entry @full_id = entry.id @id = $2 if entry.id =~ /^tag:blogger\.com,1999:blog\-([0-9]+)\.post\-([0-9]+)$/ @title = entry.title.html.strip @content = entry.content.html @categories = entry.categories.map {|c| c.term} @draft = entry.draft? @etag = entry.etag self end |
#post_to(blog) ⇒ Object
Uploads this post to the provided blog.
397 398 399 |
# File 'lib/blogger.rb', line 397 def post_to(blog) blog.post self end |
#reload ⇒ Object
Reloads the post from blogger.com, using ETags for efficiency.
369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/blogger.rb', line 369 def reload NotLoggedInError.new("You aren't logged into Blogger.").raise unless @blog.account.authenticated? path = "/feeds/#{@blog.id}/posts/default/#{@id}" resp = GoogleAuth.get(path, @blog.account.auth_token, @etag) raise Blogger::RetrievalError.new("Error while reloading post \"#{@title}\": #{resp.}") unless resp.code.eql?('200') || resp.code.eql?('304') unless resp.code.eql? '304' parse Atom::Entry.parse(resp.body) end self end |
#retrieve_comments ⇒ Object
Downloads all comments from the post, and returns them ass Blogger::Comment objects.
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/blogger.rb', line 442 def retrieve_comments NotLoggedInError.new("You aren't logged into Blogger.").raise unless @blog.account.authenticated? path = "/feeds/#{@blog.id}/#{@id}/comments/default" resp = GoogleAuth.get(path, @blog.account.auth_token) raise Blogger::RetrievalError.new("Error while retrieving comments for post id ##{@id}: #{resp.}") unless resp.code.eql? '200' feed = Atom::Feed.parse(resp.body) @comments = [] feed.entries.each do |entry| @comments << Comment.new(:atom => entry, :post => self) end @comments end |
#save ⇒ Object Also known as: push
Saves any local changes to the post, and submits them to blogger.
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/blogger.rb', line 315 def save NotLoggedInError.new("You aren't logged into Blogger.").raise unless @blog.account.authenticated? update_base_atom(@atom) path = "/feeds/#{@blog.id}/posts/default/#{@id}" data = @atom.to_s.clean_atom_junk puts path+"\n\n" puts data+"\n\n" resp = GoogleAuth.put(path,data,@blog.account.auth_token, @etag) raise Blogger::PostingError.new("Error while updating post \"#{@title}\": #{resp.}") unless resp.code.eql? '200' parse Atom::Entry.parse(resp.body) end |
#to_s ⇒ Object
Converts the post to an atom entry in string form. Internally used.
389 390 391 392 393 394 |
# File 'lib/blogger.rb', line 389 def to_s entry = Atom::Entry.new update_base_atom(entry) entry.to_s end |
#update_base_atom(entry) ⇒ Object
:nodoc:
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/blogger.rb', line 348 def update_base_atom(entry) #:nodoc: entry.title = @title @categories.each do |cat| atom_cat = Atom::Category.new atom_cat.term = cat atom_cat.scheme = 'http://www.blogger.com/atom/ns#' entry.categories << atom_cat end content = Atom::Content.new(format_content) content.type = 'xhtml' entry.content = content entry.content.type = 'xhtml' entry.draft = @draft entry end |