27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

352 CHAPTER 13 Designing an API<br />

RSpec.configure do |c|<br />

c.include ApiHelper, :type => :api<br />

end<br />

Here you define a module called ApiHelper, which you include into any test marked<br />

as an API test with the :type option. Inside the module, you use the<br />

Rack::Test::Methods module, which provides useful methods that you’ll see<br />

throughout this chapter for making requests to your application, such as the get<br />

method (not yet shown). You define the app method here so that the<br />

Rack::Test::Methods knows which application to act on. With this done, let’s go<br />

back to your test.<br />

Inside the describe block underneath this new method you’re going to want to<br />

create a new user (an admin one at that, because later on you’ll need it for the create<br />

and other actions) whom you’ll use to make this request to the API. You can create this<br />

admin by adding a let inside spec/v1/api/projects_spec.rb:<br />

let(:user) { create_user! }<br />

You’ll need to set up Devise to include the token_authenticatable module so that<br />

you can authenticate API requests from users by using a token they provide with each<br />

request. This is so that you will know what projects to show to your users, as well as any<br />

other authorization criteria that you need to apply to this user’s request. For example,<br />

only users with permission to create projects in the application should be able to do so<br />

through the API.<br />

To implement the change that you need, go into the User model (app/models/<br />

user.rb) and change the devise call to be this:<br />

devise :database_authenticatable, :registerable, :confirmable,<br />

:recoverable, :rememberable, :trackable, :validatable,<br />

:token_authenticatable<br />

Next, generate a migration to add a field called authentication_token to the users<br />

table, which will be used to store this token. You’ll need to add this migration to both<br />

the development and test environments. To do this, run these three commands:<br />

rails g migration add_authentication_token_to_users<br />

➥authentication_token:string<br />

rake db:migrate<br />

rake db:test:prepare<br />

The migration generator is smart here and will know to add the<br />

authentication_token to the users table based on the name you’re passing through.<br />

The additional argument on the end tells Rails what type of field you’d like this to be.<br />

With the migration created and run, you still need to add a callback to your User<br />

model, so that tokens are generated for users when they’re created, or for when users<br />

are updated but don’t have a token. 7 To do this, you’ll put this line in your User model:<br />

before_save :ensure_authentication_token<br />

7 A situation that is unlikely to happen (as you’ve got no serious users currently), but could potentially happen.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!