class RpiUnion::Client

Constants

API_BASE_URI
GET_ORGANIZATION_TASK
GET_USER_ORGS_TASK
GET_USER_TASK
LIST_ALL_ORGANIZATIONS_TASK

Public Class Methods

new(api_key) click to toggle source

Initialize a new Client with the specified API key

# File lib/rpi_union.rb, line 17
def initialize(api_key)
  @api_key = api_key
end

Public Instance Methods

get_organization(id) click to toggle source

Get information about a club

@param id [Int] The ID of the club @return [Hash] The result of the API call. Send an example request to see the format.

# File lib/rpi_union.rb, line 60
def get_organization(id)
  opts = { :query => { :id => id } }
  get_with_key_task(GET_ORGANIZATION_TASK, opts)
end
get_user(options = {}) click to toggle source

Get the properties of a specific user.

@param options [Hash] Must contain one of: ‘:rin`, `:id` (API/database ID), or `rcsid` @return [Hash] The output of the API call. This is of the form:

`{"name": ..., "rin": ..., "phone": ..., "class": ...}`
# File lib/rpi_union.rb, line 26
def get_user(options = {})
  opts = {}
  opts[:query] = {}

  if options.key?(:rin)
    opts[:query][:rin] = options[:rin]
  elsif options.key?(:rcsid)
    opts[:query][:rcsid] = options[:rcsid]
  elsif options.key?(:id)
    opts[:query][:dbid] = options[:id]
  else
    raise "You must pass one of the following as an option to get_user(): "+
      "'rin', 'rcsid', or 'id'"
  end

  # Set middlename if it was pass in options
  if options.key?(:middlename)
    opts[:query][:middlename] = options[:middlename]
  end

  get_with_key_task(GET_USER_TASK, opts)
end
get_user_orgs(rin) click to toggle source

Get the IDs of all the clubs of which the user is an officer

@param rin [Int] The RIN of the user to search for.

# File lib/rpi_union.rb, line 68
def get_user_orgs(rin)
  opts = { :query => { :rin => rin } }
  get_with_key_task(GET_USER_ORGS_TASK, opts)
end
list_all_organizations() click to toggle source

Get all of the Union clubs

@return [Array] An array of hashes of the form ‘{“club_id”: …, “club_name”: …}`

# File lib/rpi_union.rb, line 52
def list_all_organizations
  get_with_key_task(LIST_ALL_ORGANIZATIONS_TASK)
end

Private Instance Methods

get_with_key_task(task, options = {}) click to toggle source
# File lib/rpi_union.rb, line 75
def get_with_key_task(task, options = {})
  # Add the api key to the request
  options[:query] ||= {}
  options[:query].merge!({ :apikey => @api_key, :task => task })

  res = self.class.get('', options)
  data = JSON.parse(res.body)

  raise ApiError.new(data['status']) unless data['status']['statusCode'] == 0

  data['result']
end