As we’ve discussed at certain points in the past, user experience is extremely important, and back-end developers have a lot to do with that as well. Many users can deal with ‘ugly’ if the interface is clear, but no one on Earth likes slow! We’ll look at how to take needed work out of the request/response cycle in this exercise.
Step 0
We’ll be revisiting the house project with this exercise. I’ve merged the work that we’ve accomplished into master, so that you’ll be able to either update your forks, or pull the changes down locally.
What’s that, you weren’t working on a branch when you made your changes, and you can’t apply my changes quickly?
If you have made changes on master that are now irrelevant, try issuing these commands:
1 2 3 4 |
|
That should get you ready to work again. But create branches when you start working! It’s easy! Here’s the branch I’d start here:
1
|
|
Step 1
Add these files to your Gemfile:
1 2 3 4 5 |
|
Install them. You should know how by now! Ask a neighbor if you don’t.
Step 2
Put these contents in spec/models/social_connection_spec.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
This will give you a spec to pass! Run your specs with either:
1
|
|
or
1
|
|
if you know that guard isn’t working for you.
To pass this test, you’ll need to:
- create a model with the right fields and types
- add the proper associations throughout the domain
- add validations to the class you create
If you’d like a hint, download this file into
spec/factories/social_connections.rb
, or simply refer to it inline.
Our goal with this step is to create a record that allows us to keep track of some basic information for social connections on the part of our users. When you have this spec passing, you have the domain layer ready! Now let’s proceed to the integration.
Step 3
Place this file in spec/support/facebook_interface.rb
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
Place this file in spec/support/sidekiq.rb
1 2 |
|
Finally, place this spec in place in
spec/models/facebook_friend_worker_spec.rb
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
This will give you the next spec to run. This will test that you can progress through the Facebook API response, and save the results to the DB in the appropriate manner.
It’s customary to create an app/workers
directory to place your
FacebookFriendWorker
, as well as any other background workers that
you create. So the code that you write to pass this test should go in
app/workers/facebook_friend_worker.rb
.
When your specs are passing here, we’ve added the ability to defer work outside of the request/response cycle. We’re most of the way to seeing this work!
Step 4
Add this spec to your spec/models/authentication_spec.rb
file:
1 2 3 4 5 6 |
|
Write a lifecycle method on Authentication that will enqueue the worker. We’re definitely getting close now!
Step 5: Putting it all together
Code wise, you are ready to go! We have a few more things to put in place before we can actually run the code and have everything work.
First, sidekiq requires redis, so let’s install that.
1
|
|
We don’t want redis to run all the time— it’s not as cool as postgres. It does
need some configuration to run, so let’s save this file at
config/redis.conf
Finally, add these two lines to your Procfile
:
1 2 |
|
Step 6:
1
|
|
And try it in your web browser!
You should see something in your server window like:
1
|
|
if it worked. You can then use the data in there to print out various things in the rest of your app. Try it out!