Skip to content

Commit 80b7bc4

Browse files
committed
Deal with 301 redirections
It seem like google calendar api will randomly redirect you to the same URL with an extra 'gsessionid'
1 parent 517f690 commit 80b7bc4

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

lib/google_calendar_api_v2/base.rb

+5
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@ def success?(response)
1515
false
1616
end
1717
end
18+
19+
def redirect?(response)
20+
response.is_a? Net::HTTPRedirection
21+
end
22+
1823
end
1924
end

lib/google_calendar_api_v2/calendar.rb

+14-4
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,35 @@ def initialize(connection)
88
@connection = connection
99
end
1010

11-
def find(calendar_token)
12-
url = "/calendar/feeds/default/allcalendars/full/#{calendar_token}?alt=jsonc"
11+
def find(calendar_token, url = nil, redirect_count = 0)
12+
url ||= "/calendar/feeds/default/allcalendars/full/#{calendar_token}?alt=jsonc"
1313
response = @connection.get url, Client::HEADERS
14+
15+
raise 'Redirection Loop' if redirect_count > 3
16+
1417
if success? response
1518
Response::Calendar.new(response, @connection)
19+
elsif redirect? response
20+
find(calendar_token, response['location'], redirect_count += 1)
1621
end
1722
end
1823

19-
def create(params = {})
20-
response = @connection.post '/calendar/feeds/default/owncalendars/full?alt=jsonc',
24+
def create(params = {}, url = nil, redirect_count = 0)
25+
url ||= '/calendar/feeds/default/owncalendars/full?alt=jsonc'
26+
response = @connection.post url,
2127
{
2228
:data => {
2329
:title => "Unnamed calendar",
2430
:hidden => false
2531
}.merge(params)
2632
}.to_json, Client::HEADERS
2733

34+
raise 'Redirection Loop' if redirect_count > 3
35+
2836
if success? response
2937
Response::Calendar.new(response, @connection)
38+
elsif redirect?(response)
39+
create(params, response['location'], redirect_count += 1)
3040
end
3141
end
3242

lib/google_calendar_api_v2/event.rb

+16-6
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,36 @@ def initialize(connection, calendar)
99
@calendar = calendar
1010
end
1111

12-
def find(event_token)
13-
url = "https://www.google.com/calendar/feeds/#{@calendar.token}/private/full/#{event_token}?alt=jsonc"
12+
def find(event_token, url = nil, redirect_count = 0)
13+
url ||= "https://www.google.com/calendar/feeds/#{@calendar.token}/private/full/#{event_token}?alt=jsonc"
1414
response = @connection.get url, Client::HEADERS
1515

16+
raise 'Redirection Loop' if redirect_count > 3
17+
1618
if success? response
17-
Response::Calendar.new(response, @connection)
19+
Response::Event.new(response, @connection, @calendar)
20+
elsif redirect? response
21+
find(event_token, response['location'], redirect_count += 1)
1822
end
1923
end
2024

21-
def create(params = {})
22-
response = @connection.post "/calendar/feeds/#{@calendar.token}/private/full?alt=jsonc",
25+
def create(params = {}, url = nil, redirect_count = 0)
26+
url ||= "/calendar/feeds/#{@calendar.token}/private/full?alt=jsonc"
27+
28+
response = @connection.post url,
2329
{
2430
:data => {
2531
:title => "Undefined event",
2632
:hidden => false
2733
}.merge(params)
2834
}.to_json, Client::HEADERS
2935

36+
raise 'Redirection Loop' if redirect_count > 3
37+
3038
if success? response
31-
Response::Calendar.new(response, @connection)
39+
Response::Event.new(response, @connection, @calendar)
40+
elsif redirect? response
41+
create(params, response['location'], redirect_count += 1)
3242
else
3343
false
3444
end

lib/google_calendar_api_v2/response/base.rb

+18-7
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,32 @@ def to_json
2929
}.to_json
3030
end
3131

32-
def save
33-
res = @connection.put @attributes['selfLink'], self.to_json, GoogleCalendarApiV2::Client::HEADERS
32+
def save(url = nil, redirect_count = 0)
33+
url ||= @attributes['selfLink']
34+
response = @connection.put url, self.to_json, GoogleCalendarApiV2::Client::HEADERS
3435

35-
@attributes = JSON.parse(res.body)['data'] rescue @attributes
36-
if success? res
36+
raise 'Redirection Loop' if redirect_count > 3
37+
38+
if success? response
39+
@attributes = JSON.parse(res.body)['data'] rescue @attributes
3740
true
41+
elsif redirect? response
42+
save(response['location'], redirect_count += 1)
3843
else
3944
false
4045
end
4146
end
4247

43-
def destroy
44-
res = @connection.delete @attributes['selfLink'], GoogleCalendarApiV2::Client::HEADERS.merge({ 'If-Match' => '*' })
45-
if success? res
48+
def destroy(url = nil, redirect_count = 0)
49+
url ||= @attributes['selfLink']
50+
response = @connection.delete url, GoogleCalendarApiV2::Client::HEADERS.merge({ 'If-Match' => '*' })
51+
52+
raise 'Redirection Loop' if redirect_count > 3
53+
54+
if success? response
4655
true
56+
elsif redirect? response
57+
destroy(response['location'], redirect_count += 1)
4758
else
4859
false
4960
end

0 commit comments

Comments
 (0)