Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment