Module: Rufus::Google

Defined in:
lib/rufus/gcal.rb,
lib/rufus/ahttp.rb,
lib/rufus/google.rb,
lib/rufus/gversion.rb

Defined Under Namespace

Modules: CollectionMixin, EntryMixin Classes: Calendar, Event, Http

Constant Summary collapse

GDATA_VERSION =
'2'
VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.feed_for(feed_uri, options) ⇒ Object

A small method for getting an atom-tools Feed instance. The options hash is a get_auth_token() hash.



109
110
111
112
113
114
# File 'lib/rufus/google.rb', line 109

def self.feed_for (feed_uri, options)

  token = get_auth_token(options)

  Atom::Feed.new(feed_uri, Rufus::Google::Http.new(token))
end

.get_auth_token(options) ⇒ Object

Returns the auth token for a google account.



98
99
100
101
102
103
# File 'lib/rufus/google.rb', line 98

def self.get_auth_token (options)

  return options if options.is_a?(String)

  options[:auth] || get_auth_tokens(options)[:auth]
end

.get_auth_tokens(options) ⇒ Object

Gets an auth token via the Google ClientLogin facility



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rufus/google.rb', line 52

def self.get_auth_tokens (options)

   = options[:account]
   = options[:account_type] || 'GOOGLE'
  password = options[:password]
  service = options[:service] || :cl
  source = options[:source] || "rufus.rubyforge.org-rufus_google-#{VERSION}"

   = "#{}@gmail.com" unless .index('@')

  password = CGI.escape(password)

  data = ''
  data << "accountType=#{}&"
  data << "Email=#{}&"
  data << "Passwd=#{password}&"
  data << "service=#{service}&"
  data << "source=#{source}"

  headers = { 'Content-type' => 'application/x-www-form-urlencoded' }
  headers['GData-Version'] = GDATA_VERSION

  r = Rufus::Verbs.post(
    'https://www.google.com/accounts/ClientLogin',
    :headers => headers,
    :data => data)

  code = r.code.to_i

  raise r.body if code == 403
  raise "not a 200 OK reply : #{code} : #{r.body}" unless code == 200

  tokens = r.body.split.inject({}) { |h, l|
    md = l.match(/^(.*)=(.*$)/)
    h[md[1].downcase.to_sym] = md[2]
    h
  }

  options.merge!(tokens)

  tokens
end