Class: Blogger::Account

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

Overview

Account

The Account class is how you interface with your Blogger.com account. You just need to know your username (google email address), password, and blog ID, and you’re good to go.

Connect by creating a new Account as such:

 = Blogger::Account.new('username','password')

You can make sure you’re authenticated by calling account.authenticated?

Example usage:

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)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Account

Creates a new Account object, and authenticates if the usename and password are provided.



124
125
126
127
128
129
130
# File 'lib/blogger.rb', line 124

def initialize(*args)
  @user_id, @username, @password = args[0], "", ""  if args.size == 1 && args[0] =~ /^[0-9]+$/
  @username, @password           = args[0], args[1] if args.size == 2
  @user_id, @username, @password = args[0], args[1], args[2] if args.size == 3
  authenticate unless @username.empty? || @password.empty?
  self
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



108
109
110
# File 'lib/blogger.rb', line 108

def auth_token
  @auth_token
end

#passwordObject

Returns the value of attribute password.



107
108
109
# File 'lib/blogger.rb', line 107

def password
  @password
end

#user_idObject

Returns the value of attribute user_id.



109
110
111
# File 'lib/blogger.rb', line 109

def user_id
  @user_id
end

#usernameObject

Returns the value of attribute username.



106
107
108
# File 'lib/blogger.rb', line 106

def username
  @username
end

Instance Method Details

#authenticate(_username = "", _password = "") ⇒ Object

Re-authenticates (or authenticates if we didn’t provide the user/pass earlier)



151
152
153
154
155
156
157
# File 'lib/blogger.rb', line 151

def authenticate(_username = "", _password = "")
  username = (_username.empty?) ? @username : _username
  password = (_password.empty?) ? @password : _password
  
  @auth_token = GoogleAuth::authenticate(username, password)
  @authenticated = !( @auth_token.nil? )
end

#authenticated?Boolean

Are we authenticated successfully? This method won’t detect timeouts.

Returns:

  • (Boolean)


160
161
162
# File 'lib/blogger.rb', line 160

def authenticated?
  @authenticated
end

#blog_for_id(id) ⇒ Object

Returns the blog with the given ID



118
119
120
# File 'lib/blogger.rb', line 118

def blog_for_id(id)
  blogs.select{|b| b.id.eql? id}.first
end

#blogs(force_reload = false) ⇒ Object

Returns the blogs in this account. pass true to force a reload.



112
113
114
115
# File 'lib/blogger.rb', line 112

def blogs(force_reload=false)
  return @blogs if @blogs && !force_reload
  retrieve_blogs
end

#post(blog_id, post) ⇒ Object

Posts the provided post to the blog with the given ID. You can find your blog id by going to your blogger dashboard and selecting your blog - you’ll find an address such as this in your bar:

http://www.blogger.com/posts.g?blogID=6600774877855692384 <-- your blog id

Then just create a Blogger::Post, pass that in as well, and you’re done!



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/blogger.rb', line 171

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

  path = "/feeds/#{blog_id}/posts/default"
  data = post.to_s
  
  resp = GoogleAuth.post(path, data, @auth_token)
  
  raise Blogger::PostingError.new("Error while posting to blog_id #{blog_id}: #{resp.message}") unless resp.code.eql? '201'
  # Expect resp.code == 200 and resp.message == 'OK' for a successful.
  Post.new(:atom => Atom::Entry.parse(resp.body), :blog => blog_for_id(blog_id)) if @user_id
end

#retrieve_blogs(user_id = "") ⇒ Object

Downloads the list of all the user’s blogs and stores the relevant information.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/blogger.rb', line 133

def retrieve_blogs(user_id="")
  NotLoggedInError.new("You aren't logged into Blogger.").raise unless authenticated?
  
  user_id = (user_id.empty?) ? @user_id : user_id
  
  path = "/feeds/#{user_id}/blogs"   
  resp = GoogleAuth.get(path, @auth_token)
  feed = Atom::Feed.parse resp.body
  
  @blogs = []
  feed.entries.each do |entry|
    blog = Blogger::Blog.new(:atom => entry, :account => self)
    @blogs << blog
  end
  @blogs
end