Class: GCal::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gcal/client.rb

Constant Summary collapse

BASE_URL =
'https://www.google.com'
API_URL =
"#{BASE_URL}/calendar/feeds"

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, api_secret = nil, token = nil, secret = nil) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
# File 'lib/gcal/client.rb', line 9

def initialize(api_key = nil, api_secret = nil, token = nil, secret = nil)
  @api_key, @api_secret = api_key, api_secret
  @client = ::Typhoeus::Hydra.new
  # if api call protected, create token and consumer
  if protected_api_call?
    @consumer = ::OAuth::Consumer.new(api_key, api_secret, :site => BASE_URL)
    @token = ::OAuth::Token.new(token, secret)
  end
end

Instance Method Details

#all_calendarsObject



27
28
29
30
31
32
33
34
35
# File 'lib/gcal/client.rb', line 27

def all_calendars
  path = "/default/allcalendars/full/"
  xml = call(path)
  calendars = []
  xml['entry'].each do |entry|
    calendars << GCal::Calendar.parse(entry, path)
  end if xml['entry']
  calendars
end

#call(uri) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/gcal/client.rb', line 19

def call(uri)
  request = ::Typhoeus::Request.new(API_URL + uri)
  authorize_request!(request) if protected_api_call?
  @client.queue(request)
  @client.run
  XmlSimple.xml_in(request.response.body)
end

#events(calendar_id, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/gcal/client.rb', line 47

def events(calendar_id, options = {})
  xml = call("/#{calendar_id}/private/full#{GCal::Event.prepare_options(options)}")
  events = []
  xml['entry'].each do |entry|
    events << GCal::Event.parse(calendar_id, entry)
  end if xml['entry']
  events
end

#own_calendarsObject



37
38
39
40
41
42
43
44
45
# File 'lib/gcal/client.rb', line 37

def own_calendars
  path = "/default/owncalendars/full/"
  xml = call(path)
  calendars = []
  xml['entry'].each do |entry|
    calendars << GCal::Calendar.parse(entry, path)
  end if xml['entry']
  calendars
end