Class: GoogleAuth

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

Defined Under Namespace

Classes: AuthenticationFailedError, NotLoggedInError

Constant Summary collapse

GA_SOURCE =
'beforefilter.blogspot.com-rubypost'
GA_GOOGLE =
'www.google.com'
GA_SERVICE =
'/accounts/ClientLogin'

Class Method Summary collapse

Class Method Details

.authenticate(username, password, print_debug = false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/google_auth.rb', line 12

def self.authenticate(username, password, print_debug = false)
  http = Net::HTTP.new(GA_GOOGLE, 443)
  http.use_ssl = true
   = GA_SERVICE

  # Setup HTTPS request post data to obtain authentication token.
  data = 'Email=' + username +'&Passwd=' + password + '&source=' + GA_SOURCE + '&service=blogger'
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Submit HTTPS post request
  response, data = http.post(, data, headers)
  pp response.inspect if print_debug
  pp data.inspect     if print_debug
  unless response.code.eql? '200' 
    raise AuthenticationFailedError.new("Error during authentication: #{resp.message}")
  else
    data.split("\n").map {|l| l.split("=")}.assoc("Auth")[1]
  end
end

.default_headers(token) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/google_auth.rb', line 32

def self.default_headers(token)
  {
    'Authorization' => 'GoogleLogin auth=' + token,
    'Content-Type' => 'application/atom+xml',
    'GData-Version' => '2'
  }
end

.delete(path, token, etag = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/google_auth.rb', line 50

def self.delete(path, token, etag = nil)
  headers = self.default_headers(token)
  
  http = Net::HTTP.new('www.blogger.com')
  req  = Net::HTTP::Delete.new(path, headers)
  http.request(req)
end

.get(path, token, etag = nil) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/google_auth.rb', line 40

def self.get(path, token, etag = nil)
  headers = self.default_headers(token)
  headers.merge!('If-None-Match' => "#{etag}") if etag
  
  http = Net::HTTP.new('www.blogger.com')
  
  http.get(path, headers)
end

.post(path, data, token) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/google_auth.rb', line 66

def self.post(path, data, token)
  headers = self.default_headers(token)
  
  http = Net::HTTP.new('www.blogger.com')
  
  http.post(path, data, headers)
end

.put(path, data, token, etag = nil) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/google_auth.rb', line 58

def self.put(path, data, token, etag = nil)
  headers = self.default_headers(token)
  
  http = Net::HTTP.new('www.blogger.com')
  req  = Net::HTTP::Put.new(path, headers)
  http.request(req, data)
end