Skip to content

Instantly share code, notes, and snippets.

@mattfitzgerald
Created February 14, 2018 23:43
Show Gist options
  • Save mattfitzgerald/78885e127e2f0781bc06739419251b1f to your computer and use it in GitHub Desktop.
Save mattfitzgerald/78885e127e2f0781bc06739419251b1f to your computer and use it in GitHub Desktop.
TalkBox api with RestClient
class Talkbox
USER = ENV['TALKBOX_USER']
PASS = ENV['TALKBOX_PASS']
BASE_URL = ENV['TALKBOX_API_URL']
def self.get entity
url = "#{BASE_URL}/#{entity}"
response = RestClient::Request.execute method: :get, url: url, user: USER, password: PASS
JSON.parse(response.body)
end
def self.update p = {}
url = "#{BASE_URL}/#{p[:object]}/#{p[:data][:id]}"
begin
json_response = RestClient::Request.execute(method: :put, url: url, payload: p[:data], user: USER, password: PASS)
response = JSON.parse(json_response.body)
response.merge({"success" => success?(response)})
rescue RestClient::ExceptionWithResponse => e
response = {"success" => false, "error" => e, "response" => e.try(:response)}
end
end
def self.create p = {}
url = "#{BASE_URL}/#{p[:object]}"
begin
json_response = RestClient::Request.execute(method: :post, url: url, payload: p[:data], user: USER, password: PASS)
response = JSON.parse(json_response.body)
response.merge({"success" => success?(response)})
rescue RestClient::ExceptionWithResponse => e
response = {"success" => false, "error" => e, "response" => e.try(:response)}
end
end
private
def self.success? response
[nil, 200].include? response.try(:[], "code")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment