Skip to content

Commit a4ef03f

Browse files
committed
Implement index query for calendars and events
1 parent fe27be4 commit a4ef03f

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

lib/google_calendar_api_v2/calendar.rb

+21-3
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,35 @@ def initialize(connection)
99
end
1010

1111
def find(calendar_token, url = nil, redirect_count = 0)
12-
url ||= "/calendar/feeds/default/allcalendars/full/#{calendar_token}?alt=jsonc"
12+
url ||= "https://www.google.com/calendar/feeds/default/allcalendars/full/#{calendar_token}?alt=jsonc"
1313
response = @connection.get url, Client::HEADERS
1414

1515
raise 'Redirection Loop' if redirect_count > 3
1616

1717
if success? response
18-
Response::Calendar.new(response, @connection)
18+
item = JSON.parse(response.body)['data']
19+
Response::Calendar.new(item, @connection)
1920
elsif redirect? response
2021
find(calendar_token, response['location'], redirect_count += 1)
2122
end
2223
end
2324

25+
def all(url = nil, redirect_count = 0)
26+
url ||= "https://www.google.com/calendar/feeds/default/allcalendars/full?alt=jsonc"
27+
response = @connection.get url, Client::HEADERS
28+
29+
if success? response
30+
# Response::Event.new(response, @connection, @calendar)
31+
if items = JSON.parse(response.body)['data']['items']
32+
items.map {|item| Response::Calendar.new(item, @connection, @calendar) }
33+
else
34+
[]
35+
end
36+
elsif redirect? response
37+
all(response['location'], redirect_count += 1)
38+
end
39+
end
40+
2441
def create(params = {}, url = nil, redirect_count = 0)
2542
url ||= '/calendar/feeds/default/owncalendars/full?alt=jsonc'
2643
response = @connection.post url,
@@ -34,7 +51,8 @@ def create(params = {}, url = nil, redirect_count = 0)
3451
raise 'Redirection Loop' if redirect_count > 3
3552

3653
if success? response
37-
Response::Calendar.new(response, @connection)
54+
item = JSON.parse(response.body)['data']
55+
Response::Calendar.new(item, @connection)
3856
elsif redirect?(response)
3957
create(params, response['location'], redirect_count += 1)
4058
end

lib/google_calendar_api_v2/event.rb

+20-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,29 @@ def find(event_token, url = nil, redirect_count = 0)
1616
raise 'Redirection Loop' if redirect_count > 3
1717

1818
if success? response
19-
Response::Event.new(response, @connection, @calendar)
19+
item = JSON.parse(response.body)['data']
20+
Response::Event.new(item, @connection, @calendar)
2021
elsif redirect? response
2122
find(event_token, response['location'], redirect_count += 1)
2223
end
2324
end
2425

26+
def all(url = nil, redirect_count = 0)
27+
url ||= "https://www.google.com/calendar/feeds/#{@calendar.token}/private/full?alt=jsonc&futureevents=false"
28+
response = @connection.get url, Client::HEADERS
29+
30+
if success? response
31+
# Response::Event.new(response, @connection, @calendar)
32+
if items = JSON.parse(response.body)['data']['items']
33+
items.map {|item| Response::Event.new(item, @connection, @calendar) }
34+
else
35+
[]
36+
end
37+
elsif redirect? response
38+
all(response['location'], redirect_count += 1)
39+
end
40+
end
41+
2542
def create(params = {}, url = nil, redirect_count = 0)
2643
url ||= "/calendar/feeds/#{@calendar.token}/private/full?alt=jsonc"
2744

@@ -36,7 +53,8 @@ def create(params = {}, url = nil, redirect_count = 0)
3653
raise 'Redirection Loop' if redirect_count > 3
3754

3855
if success? response
39-
Response::Event.new(response, @connection, @calendar)
56+
item = JSON.parse(response.body)['data']
57+
Response::Event.new(item, @connection, @calendar)
4058
elsif redirect? response
4159
create(params, response['location'], redirect_count += 1)
4260
else

lib/google_calendar_api_v2/response/base.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ module Response
55
module Base
66
attr_reader :response, :attributes
77

8-
def initialize(response, connection, parent = nil)
8+
def initialize(item, connection, parent = nil)
99
@response, @connection = response, connection
10-
@attributes = JSON.parse(response.body)['data'] rescue {}
10+
@attributes = item
1111
end
1212

1313
def update_attributes(attributes)

0 commit comments

Comments
 (0)