Class: Google::Connection

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

Overview

This is a utility class that performs all of the communication with the google calendar api.

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Connection

set the username, password, auth_url, app_name, and login.



12
13
14
15
16
17
18
19
# File 'lib/google/connection.rb', line 12

def initialize(params)
  @username = params[:username]
  @password = params[:password]
  @auth_url = params[:auth_url] || "https://www.google.com/accounts/ClientLogin"
  @app_name = params[:app_name] || "northworld.com-googlecalendar-integration"

  ()
end

Instance Method Details

#loginObject

login to the google calendar and grab an auth token.

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/google/connection.rb', line 23

def ()
  content = {
    'Email' => @username,
    'Passwd' => @password,
    'source' => @app_name,
    'accountType' => 'HOSTED_OR_GOOGLE',
    'service' => 'cl'}

  response = send(Addressable::URI.parse(@auth_url), :post_form, content)

  raise HTTPRequestFailed unless response.kind_of? Net::HTTPSuccess

  @token = response.body.split('=').last
  @headers = {
     'Authorization' => "GoogleLogin auth=#{@token}",
     'Content-Type'  => 'application/atom+xml'
   }
   @update_header = @headers.clone
   @update_header["If-Match"] = "*"
end

#send(uri, method, content = '', redirect_count = 10) ⇒ Object

send a request to google.



46
47
48
49
50
51
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/google/connection.rb', line 46

def send(uri, method, content = '', redirect_count = 10)
  raise HTTPTooManyRedirections if redirect_count == 0

  set_session_if_necessary(uri)

  http = (uri.scheme == 'https' ? Net::HTTPS.new(uri.host, uri.inferred_port) : Net::HTTP.new(uri.host, uri.inferred_port))

  case method
  when :delete
    # puts "in delete: #{uri.to_s}"
    request = Net::HTTP::Delete.new(uri.to_s, @update_header)
  when :get
    # puts "in get: #{uri.to_s}"
    request = Net::HTTP::Get.new(uri.to_s, @headers)
  when :post_form
    # puts "in post_form: #{uri.to_s}"
    request = Net::HTTP::Post.new(uri.to_s, @headers)
    request.set_form_data(content)
  when :post
    # puts "in post: #{uri.to_s}\n#{content}"
    request = Net::HTTP::Post.new(uri.to_s, @headers)
    request.body = content
  when :put
    # puts "in put: #{uri.to_s}\n#{content}"
    request = Net::HTTP::Put.new(uri.to_s, @update_header)
    request.body = content
  end

  response =  http.request(request)

  # recurse if necessary.
  if response.kind_of? Net::HTTPRedirection
    response = send(Addressable::URI.parse(response['location']), method, content, redirect_count - 1)
  end

  # Raise the appropiate error if necessary.
  if response.kind_of? Net::HTTPForbidden
    raise HTTPAuthorizationFailed, response.body

  elsif response.kind_of? Net::HTTPBadRequest
    raise HTTPRequestFailed, response.body

  elsif response.kind_of? Net::HTTPNotFound
    raise HTTPNotFound, response.body
  end

  return response
end