RosterServer: Ruby

⚠️

Exciting News!

Our transition to a new website is complete. Click here to seamlessly access the latest version of this article.

Kindly update any bookmarked URLs accordingly. The Development Center will no longer be accessible after April 1, 2024. Thank you for your attention to this matter.

📘

GitHub

Click here to go to GitHub

Setup

Insert the file OneRoster.rb into your project;

Usage

First import OneRoster.

require './OneRoster.rb'

Next, create a new instance of the OneRoster object, which takes in two strings,
the client_id and client_secret.

oneRoster = OneRoster.new("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");

You are now able to make requests to a specified URL. This is done using the method make_roster_request(url), which takes in the URL and returns a Hash containing the status code and response.

oneRoster = OneRoster.new("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");
response = oneRoster.make_roster_request("https://example.com/users");

The response is made up of "status_code", which contains the status code, and "response", which contains the JSON response as a string.

puts response["status_code"]  # The status code
puts response["response"] # The JSON Response

Example

📘

Example

Print all students' names

require 'json'

# Import OneRoster
require './OneRoster.rb'
if __FILE__ == $0

    def get_users(url)
      # Create new OneRoster with the client_id and client_secret
      oneRoster = OneRoster.new("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");

        # Make the request to the given url and store the Hash with status_code and response
      response = oneRoster.make_roster_request(url)
        
        # Decode the response
        decoded = JSON.parse(response["response"])
        
        # If status_code is 200, create array of users from response, otherwise print message and return nil
        case response["status_code"]
        when 200
            return decoded["users"]
        when 401
            puts "Unauthorized Request\n" + response["response"]
        when 404
            puts "Not Found\n" + response["response"]
        when 500
            puts "Server Error\n" + response["response"]
        else
            puts "Something Went Wrong, status code " + response["status_code]" + "\n" + response["response"]
        end
        return nil;
    end
    
    users = get_users("https://example.com/users?fields=givenName%2CfamilyName&filter=role%3D'student'")

    # Print users if not nill
    if users != nil
        users.each do |user|
            puts user["givenName"] + " " + user["familyName"]
        end
    end
end