Retrieve Messages, Respond to Messages, and Create New Messages by Interacting with Bloc’s API

Stacie Taylor-Cima
May 1, 2018 · 5 min read
Photo by Sanwal Deen on Unsplash

Build a New Messages Module:

module Messages
end
module Messages
def get_messages
end

def create_message
end
end

require ‘./lib/messages’
class Kele

include Messages

end

Retrieving Messages

def get_messages(page_number = 1)
end
response = self.class.get(api_url(“message_threads”), 
headers: { “authorization” => @auth_token },
body: {
“page”: page_number
})
JSON.parse(response.body)
# Retrieves the current user’s message threads.
# Params: page_number = number(optional) If no page is specified via the parameter, the first page of messages will be displayed.
def get_messages(page_number = 1)
# Point the HTTParty GET method at the messages endpoint of Bloc’s API.
# Use HTTParty’s header option to pass the auth_token.
# Use HTTParty’s body option to pass the page_number
response = self.class.get(api_url(“message_threads”), headers: { “authorization” => @auth_token },
body: {
“page”: page_number
})
puts “This is page #{page_number} of your messages” if response.success?
# Parse the JSON document returned in the response into a Ruby hash.
JSON.parse(response.body)
end

Creating Messages

Sender (The sender’s email, string)
Recipient_id (The recipient’s id, number)
Token (The message thread token, string)
Subject (The message subject, string)
Stripped-text (The message body, string)
# Creates a new message and sends a request to Bloc’s API via the HTTParty POST method
# Params: sender = the sender’s email, string | recipient_id = the recipient’s id, number | token = the message thread token, string | subject = the message subject, string | text = the message body, string
def create_message(sender, recipient_id, token, subject, text)
# Point the HTTParty POST method at the messages endpoint of Bloc’s API.

# Use HTTParty’s header option to pass the auth_token.
# Use HTTParty’s body option to pass all of the required parameters
response = self.class.post(api_url(“messages”), headers: { “authorization” => @auth_token },
body: {
“sender” => sender,
“recipient_id” => recipient_id,
“token” => token,
“subject” => subject,
“stripped-text” => text
})
puts “Your message was sent!” if response.success?
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