Guest User

Untitled

a guest
Jan 15th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1.  
  2. # I'm having problem to setup a nice approach to config login credentials
  3. # for different sites. I have different classes/strategies to login on
  4. # sites and I wanna config them at the same place (seems wise).
  5.  
  6. # The sites can have different variables needed for the login. Some needs
  7. # username/password and some uses api-keys only.
  8.  
  9. # This is the first approach but I don't like it. Any suggestions?
  10.  
  11. module Foobar
  12.  
  13. # Helper method from active support to set module attributes
  14. mattr_accessor :credentials
  15. @@credentials = {}
  16.  
  17. def self.setup
  18. yield self
  19. end
  20.  
  21. end
  22.  
  23. Foobar.setup do |config|
  24.  
  25. config.credentials = {
  26. :my_site = {
  27. :username => 'foo',
  28. :password => 'bar'
  29. }
  30. :my_other_site = {
  31. :user_id => '123',
  32. :api_key => 'abc123'
  33. }
  34. }
  35.  
  36. end
  37.  
  38. class MySite
  39. def credentials
  40. Foobar.credentials[:my_site]
  41. end
  42. end
  43.  
  44. puts MySite.new.credentials
Add Comment
Please, Sign In to add comment