class Google::Freebusy

Freebusy returns free/busy information for a set of calendars

Attributes

connection[R]

Public Class Methods

new(params={}, connection=nil) click to toggle source

Setup and query the free/busy status of a collection of calendars.

The params parameter accepts

  • :client_id => the client ID that you received from Google after registering your application with them (console.developers.google.com/). REQUIRED

  • :client_secret => the client secret you received from Google after registering your application with them. REQUIRED

  • :redirect_url => the url where your users will be redirected to after they have successfully permitted access to their calendars. Use 'urn:ietf:wg:oauth:2.0:oob' if you are using an 'application'“ REQUIRED

  • :refresh_token => if a user has already given you access to their calendars, you can specify their refresh token here and you will be 'logged on' automatically (i.e. they don't need to authorize access again). OPTIONAL

See Readme.rdoc or readme_code.rb for an explication on the OAuth2 authorization process.

# File lib/google/freebusy.rb, line 24
def initialize(params={}, connection=nil)
  @connection = connection || Connection.factory(params)
end

Public Instance Methods

query(calendar_ids, start_time, end_time) click to toggle source

Find the busy times of the supplied calendar IDs, within the boundaries of the supplied start_time and end_time

The arguments supplied are

  • calendar_ids => array of Google calendar IDs as strings

  • start_time => a Time object, the start of the interval for the query.

  • end_time => a Time object, the end of the interval for the query.

# File lib/google/freebusy.rb, line 37
def query(calendar_ids, start_time, end_time)
  query_content = json_for_query(calendar_ids, start_time, end_time)
  response = @connection.send("/freeBusy", :post, query_content)

  return nil if response.status != 200 || response.body.empty?

  parse_freebusy_response(response.body)
end

Private Instance Methods

json_for_query(calendar_ids, start_time, end_time) click to toggle source

Prepare the JSON

# File lib/google/freebusy.rb, line 51
def json_for_query(calendar_ids, start_time, end_time)
  {}.tap{ |obj|
    obj[:items] = calendar_ids.map {|id| Hash[:id, id] }
    obj[:timeMin] = start_time.utc.iso8601
    obj[:timeMax] = end_time.utc.iso8601
  }.to_json
end
parse_freebusy_response(response_body) click to toggle source
# File lib/google/freebusy.rb, line 59
def parse_freebusy_response(response_body)
  query_result = JSON.parse(response_body)

  return nil unless query_result['calendars'].is_a? Hash

  query_result['calendars'].each_with_object({}) do |(calendar_id, value), result|
    result[calendar_id] = value['busy'] || []
  end
end