Class: Blogger::Blog

Inherits:
Object
  • Object
show all
Defined in:
lib/blogger.rb

Overview

Blog

Encapsulates a Blog retrieved from your user account. This class can be used for searching for posts, or for uploading posts via the post method. Blog objects are only safely retrieved via the Blogger::Account class, either via Account#blogs or Account#blog_for_id.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Blog

:nodoc:



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/blogger.rb', line 198

def initialize(opts = {}) #:nodoc:
  entry = opts[:atom]
  @authors = []
  @title = entry.title.html.strip
  @id = $2 if entry.id =~ /tag:blogger\.com,1999:user\-([0-9]+)\.blog\-([0-9]+)$/
  entry.authors.each do |author|
    @authors << author
  end
  @updated = entry.updated
  @published = entry.published
  @account = opts[:account] if opts[:account]
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



197
198
199
# File 'lib/blogger.rb', line 197

def 
  @account
end

#authorsObject

Returns the value of attribute authors.



194
195
196
# File 'lib/blogger.rb', line 194

def authors
  @authors
end

#idObject

Returns the value of attribute id.



193
194
195
# File 'lib/blogger.rb', line 193

def id
  @id
end

#publishedObject

Returns the value of attribute published.



195
196
197
# File 'lib/blogger.rb', line 195

def published
  @published
end

#titleObject

Returns the value of attribute title.



192
193
194
# File 'lib/blogger.rb', line 192

def title
  @title
end

#updatedObject

Returns the value of attribute updated.



196
197
198
# File 'lib/blogger.rb', line 196

def updated
  @updated
end

Instance Method Details

#post(post) ⇒ Object

Uploads the provided post to this blog. Requires that you be logged into your blogger account via the Blogger::Account class.



213
214
215
216
217
218
219
220
221
222
223
# File 'lib/blogger.rb', line 213

def post(post)
  NotLoggedInError.new("You aren't logged into Blogger.").raise unless @account.authenticated?

  path = "/feeds/#{@id}/posts/default"
  data = post.to_s
  
  resp = GoogleAuth.post(path, data, @account.auth_token)
  
  raise Blogger::PostingError.new("Error while posting to blog_id #{@id}: #{resp.message}") unless resp.code.eql? '201'
  post.parse Atom::Entry.parse(resp.body)
end

#posts(force_reload = false) ⇒ Object



225
226
227
228
# File 'lib/blogger.rb', line 225

def posts(force_reload = false)
  return @posts if @posts && !(force_reload)
  retrieve_posts
end

#retrieve_postsObject

Downloads all the posts for this blog.

Raises:

  • (Blogger::RetrievalError)


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/blogger.rb', line 231

def retrieve_posts
  NotLoggedInError.new("You aren't logged into Blogger.").raise unless @account.authenticated?
  
  path = "/feeds/#{@id}/posts/default"
  
  resp = GoogleAuth.get(path, @account.auth_token)
  
  raise Blogger::RetrievalError.new("Error while retrieving posts for blog id ##{@id}: #{resp.message}") unless resp.code.eql? '200'
  feed = Atom::Feed.parse(resp.body)
  
  @posts = []
  feed.entries.each do |entry|
    @posts << Post.new(:atom => entry, :blog => self)
  end
  @posts
end