Retrieve User Data from Bloc.io’s API

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

Bloc says: “Retrieve the current user from the Bloc API by defining get_me.”

def get_me
end

Bloc says: “Pass auth_token to the request to properly authenticate against the Bloc API. Pass the auth_token via HTTParty's headers option:"

response = self.class.get(url, headers: { "authorization" => @auth_token })

Bloc says: “HTTParty requests return a response object with the data accessible via the #body method. This information is a JSON String. Add the json gem as a runtime dependency and use the #parse method to convert the user data to a Ruby hash.”

“HTTParty requests return a response object with the data accessible via the #body method. This information is a JSON String.”

“Add the json gem as a runtime dependency”

“Use the #parse method to convert the user data to a Ruby hash.”


require 'httparty'
require 'json'
class Kele
include HTTParty
# Creates a new Kele client authorized with a email and password
# Params: email = 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 email and password.
@auth_token = response["auth_token"]
# If the email 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
# Retrieve's the current user as a JSON blob by passing auth_token to the request to property authenticate against the Bloc API.
# Params: auth_token = string

def get_me
# Use the httparty class method .get to pass the auth_token to Bloc's API with httparty's header option.
response = self.class.get(api_url('users/me'), headers: { "authorization" => @auth_token })
# Parse the JSON document returned in the response into a Ruby hash
JSON.parse(response.body)
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