Class: GCal4Ruby::Base

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

Overview

The Base class includes the basic HTTP methods for sending and receiving messages from the Google Calendar API. You shouldn't have to use this class directly, rather access the functionality through the Service subclass.

Direct Known Subclasses

Service

Constant Summary collapse

AUTH_URL =
"https://www.google.com/accounts/ClientLogin"
CALENDAR_LIST_FEED =
"http://www.google.com/calendar/feeds/default/allcalendars/full"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#debugObject

If set to true, debug will dump all raw HTTP requests and responses



105
106
107
# File 'lib/gcal4ruby/base.rb', line 105

def debug
  @debug
end

#proxy_infoObject

Contains the ProxyInfo object for using a proxy server



102
103
104
# File 'lib/gcal4ruby/base.rb', line 102

def proxy_info
  @proxy_info
end

Instance Method Details

#do_delete(url, header) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/gcal4ruby/base.rb', line 270

def do_delete(url, header)
  ret = nil
  if url.is_a?(String)
    location = URI.parse(url)
  else
    location = url
  end
  
  puts "Starting get\nHeader: #{header}\n" if @debug
  if oauth?
    ret = @auth_token.delete(url.to_s, header)
  else
    http = get_http_object(location)
    http.start do |ht|
      ret = ht.delete(location.to_s, header)
    end
  end
  return ret
end

#do_get(url, header) ⇒ Object



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

def do_get(url, header)
  ret = nil
  if url.is_a?(String)
    location = URI.parse(url)
  else
    location = url
  end

  puts "Starting get\nHeader: #{header}\n" if @debug
  if oauth?
    ret = @auth_token.get(url.to_s, header)
  else
    http = get_http_object(location)
    http.start do |ht|
      ret = ht.get(location.to_s, header)
    end
  end
  return ret
end

#do_post(url, header, content) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/gcal4ruby/base.rb', line 135

def do_post(url, header, content)
  ret = nil
  if url.is_a?(String)
    location = URI.parse(url)
  else
    location = url
  end
  
  puts "Starting post\nHeader: #{header}\n" if @debug
  if oauth?
    header ||= {}
    header.merge!({ 'Accept'=>'application/atom+xml', 'Content-Type' => 'application/atom+xml' })
    ret = @auth_token.post(url.to_s, content, header)
  else
    http = get_http_object(location)
    http.start do |ht|
      ret = ht.post(location.to_s, content, header)
    end
  end
  return ret
end

#do_put(url, header, content) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/gcal4ruby/base.rb', line 179

def do_put(url, header, content)
  ret = nil
  if url.is_a?(String)
    location = URI.parse(url)
  else
    location = url
  end
  
  puts "Starting put\nHeader: #{header}\n" if @debug
  if oauth?
    header ||= {}
    header.merge!({ 'Accept'=>'application/atom+xml', 'Content-Type' => 'application/atom+xml' })
    ret = @auth_token.put(url.to_s, content, header)
  else
    http = get_http_object(location)
    http.start do |ht|
      ret = ht.put(location.to_s, content, header)
    end
  end
  return ret
end

#send_delete(url, header = nil) ⇒ Object

Sends an HTTP DELETE request. The header should be a hash of name/value pairs.
Returns the Net::HTTPResponse object on succces, or raises the appropriate error if a non 20x response code is received.



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/gcal4ruby/base.rb', line 251

def send_delete(url, header = nil)
  header = auth_header(header)
  ret = nil
  location = URI.parse(url)
  puts "url = "+url if @debug
  ret = do_delete(location, header)
  while ret.is_a?(Net::HTTPRedirection)
    puts "Redirect received, resending post" if @debug
    ret = do_delete(ret['location'], header)
  end
  if ret.is_a?(Net::HTTPSuccess)
    puts "20x response received\nResponse: \n"+ret.read_body if @debug
    return true
  else
    puts "invalid response received: "+ret.code if @debug
    raise HTTPDeleteFailed, ret.body
  end
end

#send_get(url, header = nil) ⇒ Object

Sends an HTTP GET request. The header should be a hash of name/value pairs.
Returns the Net::HTTPResponse object on succces, or raises the appropriate error if a non 20x response code is received.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/gcal4ruby/base.rb', line 204

def send_get(url, header = nil)
  header = auth_header(header)
  ret = nil
  location = URI.parse(url)
  puts "url = "+url if @debug
  ret = do_get(location, header)

  while ret.is_a?(Net::HTTPRedirection)
    puts "Redirect received from #{location.to_s}, resending get to #{ret['location']}" if @debug
    if oauth?
      header ||= {}
      header['Cookie'] = ret['set-cookie'] if ret['set-cookie']
    end
    ret = do_get(ret['location'], header)
  end
  if ret.is_a?(Net::HTTPSuccess)
    puts "20x response received\nResponse: \n"+ret.read_body if @debug
    return ret
  else
    puts "Error received, resending get" if @debug
    raise HTTPGetFailed, ret.body
  end
end

#send_post(url, content, header = nil) ⇒ Object

Sends an HTTP POST request. The header should be a hash of name/value pairs.
Returns the Net::HTTPResponse object on succces, or raises the appropriate error if a non 20x response code is received.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/gcal4ruby/base.rb', line 110

def send_post(url, content, header=nil)
  header = auth_header(header)
  ret = nil
  location = URI.parse(url)
  puts "url = "+url if @debug
  ret = do_post(location, header, content)
  while ret.is_a?(Net::HTTPRedirection)
    puts "Redirect received, resending post" if @debug
    if oauth?
      header ||= {}
      header['Cookie'] = ret['set-cookie'] if ret['set-cookie']
    end
    ret = do_post(ret['location'], header, content)
  end
  if ret.is_a?(Net::HTTPSuccess)
    puts "20x response received\nResponse: \n"+ret.read_body if @debug
    return ret
  else
    puts "invalid response received: "+ret.code if @debug
    puts "error: "+ret.body.inspect if @debug
    puts ret.inspect if @debug
    raise HTTPPostFailed, ret.body
  end
end

#send_put(url, content, header = nil) ⇒ Object

Sends an HTTP PUT request. The header should be a hash of name/value pairs.
Returns the Net::HTTPResponse object on succces, or raises the appropriate error if a non 20x response code is received.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/gcal4ruby/base.rb', line 160

def send_put(url, content, header=nil)
  header = auth_header(header)
  ret = nil
  location = URI.parse(url)
  puts "url = "+url if @debug
  ret = do_put(location, header, content)
  while ret.is_a?(Net::HTTPRedirection)
    puts "Redirect received, resending post" if @debug
    ret = do_put(ret['location'], header, content)
  end
  if ret.is_a?(Net::HTTPSuccess)
    puts "20x response received\nResponse: \n"+ret.read_body if @debug
    return ret
  else
    puts "invalid response received: "+ret.code if @debug
    raise HTTPPutFailed, ret.body
  end
end