Initialize and Authorize API Client

Stacie Taylor-Cima
Apr 25, 2018 · 2 min read


class Kele
include HTTParty
# Creates a new Kele client authorized with a username and password
# Params: username = string, password = string
def initialize(email, password)
# Use the httparty class method .post to send a post request to the sessions endpoint of Bloc’s API with the email and password in the body of the request.
response = self.class.post(api_url("sessions"), body: {"email": email, "password": password})
# The @auth_token instance variable holds the authorization token provided by Bloc's API upon verifying successful username and password.
@auth_token = response["auth_token"]
# If the username and password are invalid, Bloc's API will not return an authorization token.
puts "There was a problem authorizing those credentials. Please try again." if @auth_token.nil?
end
# Bloc's API URL
# Params: endpoint = string

def api_url(endpoint)
"https://www.bloc.io/api/v1/#{endpoint}"
end
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