Il più semplice è memorizzare le informazioni come costanti nei vari file dell'ambiente. In questo modo è possibile utilizzare account diversi per lo sviluppo, la produzione, ecc è
# Eg
# development/environment.rb
....
API_1_USER = "user101"
API_1_PW = "secret!"
alternativa per creare un file YAML, poi leggerlo quando i segni app in un api. Questo è lo stile utilizzato dalle rotaie se stesso con la config/databse.yml file di
AGGIUNTO
È anche possibile memorizzare come costante usando un hash o di hash nidificato.
# Eg
# development/environment.rb
....
API_1 = {"user" => "user101", "pw" => "secret!"}
API_2 = {"user" => "user102", "pw" => "double_secret"}
# or nested hashes
API_KEYS = {
"api_1" => {"user" => "user101", "pw" => "secret!"},
"api_2" => {"user" => "user102", "pw" => "double_secret"}}
# using them in another file:
foo.signin(API_1['user'], API_1['pw'])
# or
foo.signin(API_KEYS["api_1"]['user'], API_KEYS["api_1"]['pw'])
# note, I use string constants instead of symbols to save vm (since the hash is
# not referenced more than once or twice). You could also use
# symbols as the keys, especially if the hash will be referenced often:
API_1 = {:user => "user101", :pw => "secret!"}
fonte
2010-07-21 02:49:27
Se si segue questo approccio, io voto per gli hash annidati, piuttosto che per le costanti sottolineate. – Matchu
Che cos'è un esempio di hash annidato? E poi come lo chiamerei nel controller che ha bisogno dell'accesso API, proprio come una variabile ALL CAP? – Angela
Ho aggiunto un esempio di utilizzo degli hash come costanti –