google_calendar_api_v2 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,5 +15,10 @@ module GoogleCalendarApiV2
15
15
  false
16
16
  end
17
17
  end
18
+
19
+ def redirect?(response)
20
+ response.is_a? Net::HTTPRedirection
21
+ end
22
+
18
23
  end
19
24
  end
@@ -8,16 +8,22 @@ module GoogleCalendarApiV2
8
8
  @connection = connection
9
9
  end
10
10
 
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"
13
13
  response = @connection.get url, Client::HEADERS
14
+
15
+ raise 'Redirection Loop' if redirect_count > 3
16
+
14
17
  if success? response
15
18
  Response::Calendar.new(response, @connection)
19
+ elsif redirect? response
20
+ find(calendar_token, response['location'], redirect_count += 1)
16
21
  end
17
22
  end
18
23
 
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,
21
27
  {
22
28
  :data => {
23
29
  :title => "Unnamed calendar",
@@ -25,8 +31,12 @@ module GoogleCalendarApiV2
25
31
  }.merge(params)
26
32
  }.to_json, Client::HEADERS
27
33
 
34
+ raise 'Redirection Loop' if redirect_count > 3
35
+
28
36
  if success? response
29
37
  Response::Calendar.new(response, @connection)
38
+ elsif redirect?(response)
39
+ create(params, response['location'], redirect_count += 1)
30
40
  end
31
41
  end
32
42
 
@@ -9,17 +9,23 @@ module GoogleCalendarApiV2
9
9
  @calendar = calendar
10
10
  end
11
11
 
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"
14
14
  response = @connection.get url, Client::HEADERS
15
15
 
16
+ raise 'Redirection Loop' if redirect_count > 3
17
+
16
18
  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)
18
22
  end
19
23
  end
20
24
 
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,
23
29
  {
24
30
  :data => {
25
31
  :title => "Undefined event",
@@ -27,8 +33,12 @@ module GoogleCalendarApiV2
27
33
  }.merge(params)
28
34
  }.to_json, Client::HEADERS
29
35
 
36
+ raise 'Redirection Loop' if redirect_count > 3
37
+
30
38
  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)
32
42
  else
33
43
  false
34
44
  end
@@ -29,21 +29,32 @@ module GoogleCalendarApiV2
29
29
  }.to_json
30
30
  end
31
31
 
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
34
35
 
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
37
40
  true
41
+ elsif redirect? response
42
+ save(response['location'], redirect_count += 1)
38
43
  else
39
44
  false
40
45
  end
41
46
  end
42
47
 
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
46
55
  true
56
+ elsif redirect? response
57
+ destroy(response['location'], redirect_count += 1)
47
58
  else
48
59
  false
49
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_calendar_api_v2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-14 00:00:00.000000000 Z
12
+ date: 2012-02-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth
16
- requirement: &70104510747640 !ruby/object:Gem::Requirement
16
+ requirement: &70224774125760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70104510747640
24
+ version_requirements: *70224774125760
25
25
  description:
26
26
  email: unixcharles@gmail.com
27
27
  executables: []