class Aladtec::Client

Aladtec API Client

Attributes

config[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/aladtec/client.rb, line 15
def initialize(args = {})
  @config = Aladtec.config.dup.tap do |c|
    c.update(args)
  end
end

Public Instance Methods

authenticate() click to toggle source
# File lib/aladtec/client.rb, line 25
def authenticate
  if config.client_id.nil? || config.client_secret.nil? 
    raise Aladtec::Error, 'client_id and client_secret are required'
  end
  body = { grant_type: 'client_credentials', client_id: config.client_id,
           client_secret: config.client_secret }
  response = HTTP.post(URI.join(config.endpoint, 'oauth/token'), json: body)
  body = response.parse
  @auth_token = body.fetch('token')
  @auth_expiration = Time.at body.fetch('expires')
  response.status.success?
end
configure() { |config| ... } click to toggle source
# File lib/aladtec/client.rb, line 21
def configure
  yield config
end
events(options = {}) click to toggle source

Public: Get a list of events for a date or range of dates

options - The Hash options used to refine the selection (default: {}):

:begin_time - The begin date to return events for (required).
:end_time   - The end date to return events for (required).
# File lib/aladtec/client.rb, line 43
def events(options = {})
  bd = options.fetch(:begin_time) do
    raise ArgumentError, 'You must supply a :begin_time option!'
  end
  ed = options.fetch(:end_time) do
    raise ArgumentError, 'You must supply a :end_time option!'
  end
  events = request('events', range_start: format_time(bd),
                             range_stop: format_time(ed))
  events.values.flatten.map { |event| Event.new(event) }
end
members() click to toggle source

Public: Get a list of members

# File lib/aladtec/client.rb, line 57
def members
  res = request('members', include_attributes: true)
  res.map { |member| Member.new(member) }
end
scheduled_now(options = {}) click to toggle source

Public: Get list of members scheduled now

# File lib/aladtec/client.rb, line 70
def scheduled_now(options = {})
  res = request('scheduled-time/members-scheduled-now', options)
  res.map { |schedule| ScheduledNow.new(schedule) }
end
scheduled_range(options = {}) click to toggle source

Public: Get list of members scheduled in a time range

options - The Hash options used to refine the selection (default: {}):

:begin_time - The begin time to return events for (required).
:end_time   - The end time to return events for (required).
# File lib/aladtec/client.rb, line 80
def scheduled_range(options = {})
  bt = options.fetch(:begin_time) do
    raise ArgumentError, 'You must supply a :begin_time!'
  end
  et = options.fetch(:end_time) do
    raise ArgumentError, 'You must supply an :end_time!'
  end
  scheduled_time = request('scheduled-time', range_start: format_time(bt),
                                             range_stop: format_time(et))
  scheduled_time.values.flatten.map { |range| Range.new(range) }
end
schedules() click to toggle source

Public: Get a list of schedules

# File lib/aladtec/client.rb, line 64
def schedules
  res = request('schedules')
  res.map { |schedule| Schedule.new(schedule) }
end

Private Instance Methods

auth_expired?() click to toggle source
# File lib/aladtec/client.rb, line 110
def auth_expired?
  !@auth_token || !@auth_expiration || Time.now >= @auth_expiration
end
format_time(time) click to toggle source
# File lib/aladtec/client.rb, line 106
def format_time(time)
  (time.is_a?(Time) ? time : Time.parse(time)).strftime('%FT%R')
end
request(path, options = {}) click to toggle source
# File lib/aladtec/client.rb, line 94
def request(path, options = {})
  if auth_expired? && !authenticate
    raise Aladtec::Error, 'authentication failed'
  end
  res = HTTP[user_agent: config.user_agent]
        .auth("Bearer #{@auth_token}")
        .get(URI.join(config.endpoint, path), params: options)
  raise Aladtec::Error, res.status.reason unless res.status.success?

  res.parse
end