☒ I agree on the response.code. I think that as I was speaking it and typing... It got typed out. 😀
I think that a full solution around this would be to have a background job which gets triggered on create and whenever the email address is updated to do the gravatar_exists? functionality and then update the user to populate a gravatar_url attribute on the user. Then we could check if the user.gravatar_url? and simply return the image_tag with the user.gravatar_url or display the initials.
I added this feature to a Rails 4.2.5 app. In development, all worked at first attempt. But in production, gravatar never was returned. I realized explicit require is needed. So, I had to add this line at top of app/presenters/avatar_presenter.rb
require 'net/http'
Found it, why not, in SO: https://stackoverflow.com/questions/42803138/why-rails-app-need-to-require-net-http-after-deploy-to-a-new-server
Great episode. I think an interesting follow-up to this episode would be to implement it with the Null Object, Factory, or Adapter patterns. Then add a third option, like an avatar uploaded to the app. Or maybe even an avatar provided through a social media account if the app supports signing in with Facebook, Google, etc.
By the way, in order to use fewer resources in the server, something interesting could be to do the same in the front end using Stimulus.
I think that a full solution around this would be to have a background job which gets triggered on create and whenever the email address is updated to do the gravatar_exists? functionality and then update the user to populate a gravatar_url attribute on the user. Then we could check if the user.gravatar_url? and simply return the image_tag with the user.gravatar_url or display the initials.
In development, all worked at first attempt. But in production, gravatar never was returned.
I realized explicit require is needed. So, I had to add this line at top of app/presenters/avatar_presenter.rb
Found it, why not, in SO: https://stackoverflow.com/questions/42803138/why-rails-app-need-to-require-net-http-after-deploy-to-a-new-server