Guest User

Untitled

a guest
Feb 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. class Talkbox
  2.  
  3. USER = ENV['TALKBOX_USER']
  4. PASS = ENV['TALKBOX_PASS']
  5. BASE_URL = ENV['TALKBOX_API_URL']
  6.  
  7. def self.get entity
  8. url = "#{BASE_URL}/#{entity}"
  9. response = RestClient::Request.execute method: :get, url: url, user: USER, password: PASS
  10. JSON.parse(response.body)
  11. end
  12.  
  13.  
  14. def self.update p = {}
  15.  
  16. url = "#{BASE_URL}/#{p[:object]}/#{p[:data][:id]}"
  17.  
  18. begin
  19. json_response = RestClient::Request.execute(method: :put, url: url, payload: p[:data], user: USER, password: PASS)
  20. response = JSON.parse(json_response.body)
  21. response.merge({"success" => success?(response)})
  22. rescue RestClient::ExceptionWithResponse => e
  23. response = {"success" => false, "error" => e, "response" => e.try(:response)}
  24. end
  25. end
  26.  
  27. def self.create p = {}
  28.  
  29. url = "#{BASE_URL}/#{p[:object]}"
  30.  
  31. begin
  32. json_response = RestClient::Request.execute(method: :post, url: url, payload: p[:data], user: USER, password: PASS)
  33. response = JSON.parse(json_response.body)
  34. response.merge({"success" => success?(response)})
  35. rescue RestClient::ExceptionWithResponse => e
  36. response = {"success" => false, "error" => e, "response" => e.try(:response)}
  37. end
  38. end
  39.  
  40.  
  41. private
  42.  
  43. def self.success? response
  44. [nil, 200].include? response.try(:[], "code")
  45. end
  46.  
  47. end
Add Comment
Please, Sign In to add comment