Building Kele API Client | Retrieve Mentor Availability

Stacie Taylor-Cima
Apr 26, 2018 · 4 min read
Photo by Estée Janssens on Unsplash
get_mentor_availability
end


Request the mentor availability data from Bloc’s API

def api_url(endpoint)
"https://www.bloc.io/api/v1/#{endpoint}"
end
GET https://www.bloc.io/api/v1/mentors/id/student_availability
response = self.class.get(api_url(“/mentors/#{mentor_id}/student_availability”), headers: { “authorization” => @auth_token })

Pass auth_token to the request to properly authenticate and get the mentor’s availability

# Retrieve the availability of the current user’s mentor
def get_mentor_availability(mentor_id)
# Point the HTTParty GET method at the mentors/mentor_id/student_availability endpoint of Bloc’s API.
# Use HTTParty’s header option to pass the auth_token.
response = self.class.get(api_url(“/mentors/#{mentor_id}/student_availability”), headers: { “authorization” => @auth_token })
end

Convert the JSON response into a Ruby Hash

JSON.parse(response.body)

Loop through all the mentor’s time slots and return an array that contains only the available time slots.

#Retrieve the availability of the current user’s mentor
def get_mentor_availability(mentor_id)
# Point the HTTParty GET method at the mentors/mentor_id/student_availability endpoint of Bloc’s API.
# Use HTTParty’s header option to pass the auth_token.
response = self.class.get(“/mentors/#{mentor_id}/student_availability”, headers: { “authorization” => @auth_token })
# This is the array that will hold all of the time slots that are not booked.
available = []
# Parse the JSON document returned in the response into a Ruby hash.
# Loop through each of the mentor’s time slots. If the booked attribute is null, or the time slot is available, add that time slot to the available array.
JSON.parse(response.body).each do |time_slot|
if time_slot[“booked”].nil?
available << time_slot
end
end

# Return all of the available time slots.
available
end

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade