Ruby framework samples

32 views
Skip to first unread message

yis...@codemonkey.co.il

unread,
Aug 20, 2015, 7:26:10 PM8/20/15
to Clever Developer Program: Discussions
Hi Clever Team,

I've started development with clever sandbox on a ruby on rails framework using the omniauth-clever gem.

I'm able to authenticate successfully end-to-end and even get a student id, so I'm concluding the call to api.clever.com/me worked fine. The info I have is the following:
{"user_type":"student","id":"55d6530fb178640d00e955d5","district":"55d645ed7e90f501000021dc","type":"student","name":null}

Next I'd like to make a call to
https://api.clever.com/v1.1/student/[the student's id]
So of course I need the bearer token.

Since the call to /me was done explicitly by the gem, I don't know where the bearer token is hiding.

Do you guys have any sample ruby code that uses this gem where I can have a look on how they did the next API calls? Or can you otherwise advise on how to get that bearer and perform the next call correctly?

Thanks!
Yishai




Taylor Singletary

unread,
Aug 21, 2015, 10:41:55 AM8/21/15
to Clever Developer Program: Discussions, yis...@codemonkey.co.il
Hi Yishai,

Thanks for writing in!

The omniauth framework has a common way to retrieve the results of a successful authentication sequence.

In whatever controller action you use to complete the authentication flow, you can grab the current state of authentication by retrieving a hash from the request environment:

@omniauth = request.env['omniauth.auth'].to_hash

This hash is going to look something like this, and includes details omniauth found by requesting /me:

{
  "provider" => "clever",
  "uid" => "551333766c0a330e003f4b0c",
  "info" => {
    "user_type" => "teacher",
    "id" => "551333766c0a330e003f4b0c",
    "district" => "551331b0ed3308010000030f",
    "type" => "teacher",
    "name" => nil
  },
  "credentials" => {
    "token" => "BEARER_TOKEN_STRING",
    "expires" => false
  },
  "extra" => {
    "raw_info" => {
      "type" => "teacher",
      "data" => {
        "id" => "551333766c0a330e003f4b0c",
        "district" => "551331b0ed3308010000030f",
        "type" => "teacher"
      },
      "links" => [
        [0] { "rel" => "self", "uri" => "/me" },
        [1] { "rel" => "canonical", "uri" => "/v1.1/teachers/551333766c0a330e003f4b0c" },
        [2] { "rel" => "district", "uri" => "/v1.1/districts/551331b0ed3308010000030f" }
      ]
    }
  }
}

From this point, you have a few choices on what you might want to do. You may want to store the bearer token string and clever ID and user type in your database. You may want to just put this information in the session.

clever_user_id = @omniauth["uid"] # => "551331b0ed3308010000030f"
clever_user_type = @omniauth["info"]["user_type"] # => "teacher"
clever_bearer_token = @omniauth["credentials"]["token"] # => "BEARER_TOKEN_STRING"
clever_district_id = @omniauth["info"]["district"] # => "551331b0ed3308010000030f"

To make the next API call, in this case to /v1.1/teachers/551331b0ed3308010000030f, I need to use the clever_bearer_token value.

Omniauth doesn't provide a general purpose HTTP client for you, which leaves you to choose the one you're already familiar with or appeals to you.

I like using RestClient (https://github.com/rest-client/rest-client) and if I were to make the next request, it'd look something like:

teacher_response = RestClient.get 'https://api.clever.com/v1.1/teachers/551331b0ed3308010000030f', { 'Authorization' => "Bearer #{clever_bearer_token}" }

if teacher_response.code == 200
  json_response = JSON.parse(teacher_response.body)
  teacher_name = json_response["data"]["name"]
  puts "Hello, #{teacher_name.first} #{teacher_name.last}!"
else
  # Couldn't retrieve teacher record...
end

Another HTTP library you may want to use is HTTPparty (https://github.com/jnunemaker/httparty), which handles some of the JSON parsing for you.

I hope this is helpful!

Taylor Singletary
Clever developer relations
Reply all
Reply to author
Forward
0 new messages