Created
February 14, 2018 23:43
-
-
Save mattfitzgerald/78885e127e2f0781bc06739419251b1f to your computer and use it in GitHub Desktop.
TalkBox api with RestClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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