Switch between fake and real api depending on environment in rails -
using rails 4.1.4, ruby 2.1.1
so, have rails app needs nail external api. can't run other app locally right create easier. using vcr writing tests, in development created fakemymodel class deed api, have been gradually replacing mymodel calls real api. in development nice maintain using fakemymodel, , have production utilize mymodel. here best solution have far:
# settings.yml api: model_name: mymodel # developmentsettings.yml api: model_name: fakemymodel # config/initializers/local_apis.rb api = settings.api.model_name.constantize # app/controllers/some_controller.rb api.my_method # models/my_model.rb class mymodel def my_method # api phone call here end end # models/fake_my_model.rb class fakemymodel def my_method { some: 'default_data' } end end
this works pretty well, except error:
argumenterror - re-create of mymodel has been removed module tree still active!:
my understanding because initializing our class in config not reloaded during development. within class phone call methods within of it, , since class in models folder dynamically reloaded in development, , causes issue. solution have seen not set code in places don't dynamically reload (like lib, in case config).
so how can this? how can have false , real api class called dependent on env, without wrapping every phone call in check env?
ruby-on-rails
No comments:
Post a Comment