Guest User

Untitled

a guest
Mar 10th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1.  
  2. module PrivateGroupApi
  3.  
  4. class Request
  5.  
  6. attr_accessor :url,
  7. :username,
  8. :password,
  9. :listeners
  10.  
  11. def initialize(url)
  12. self.url = url
  13. self.listners = {
  14. '200' => lambda {},
  15. '404' => lambda {},
  16. '500' => lambda {}
  17. }
  18. end
  19.  
  20. def authenticate
  21. uri = URI.parse "#{url}?username=#{username}&password=#{password}"
  22. request = Net::HTTP::Get.new uri.path
  23. http = Net::HTTP.new uri.host, uri.port
  24. response = http.request request
  25. listener = listeners[response.code]
  26. listener.call
  27. end
  28.  
  29. def on_failure(&block)
  30. listners['404'] = block
  31. end
  32.  
  33. def on_error(&block)
  34. listners['500'] = block
  35. end
  36.  
  37. end
  38.  
  39. end
Add Comment
Please, Sign In to add comment