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.

Middleware<br />

To make your first test pass you’ll need to check that your find_user method actually<br />

returns a valid user; otherwise you’ll return this 404 response. The best place to do<br />

this would be inside the find_user method itself, turning it into this:<br />

def find_user<br />

@user = User.find_by_authentication_token(params[:token])<br />

halt 401, "Token is invalid." unless @user<br />

end<br />

The halt method here will stop a request dead in its tracks. In this case, it will return<br />

a 401 status code with the body being the string specified. When you run your tests<br />

again the first two should be passing, with the third one still failing:<br />

3 examples, 1 failure<br />

Alright, so now if an invalid token is passed, you’re throwing exactly the same error as<br />

the last two iterations of your API did—good progress! Finally, you’ll need to send a<br />

404 response when a project cannot be found within the scope for the current user.<br />

To do this, change the find_project method in your app to this:<br />

def find_project<br />

@project = Project.for(@user).find(params[:project_id])<br />

rescue ActiveRecord::RecordNotFound<br />

halt 404, "The project you were looking for could not be found."<br />

end<br />

When you run your tests for a final time with bundle exec rspec spec/api/v3/<br />

tickets_spec.rb, they should all pass:<br />

3 examples, 0 failures<br />

Awesome! This should give you a clear idea of how you could implement an API similar<br />

to the one you created back in chapter 13 by using the lightweight framework of<br />

Sinatra. All of this is possible because Rails provides an easy way to mount Rack-based<br />

applications inside your Rails applications. You could go further with this API, but this<br />

is probably another exercise for you later on if you wish to undertake it.<br />

You’ve learned how you can use Rack applications to serve as endpoints of<br />

requests, but you can also create pieces that hook into the middle of the request cycle<br />

called middleware. Rails has a few of these already, and you saw the effects of one of<br />

them when you were able to access the env["action_dispatch.request.path<br />

_parameters"] key inside your Sinatra application. Without the middleware of the<br />

Rails stack, this parameter would be unavailable. In the next section, we look at the<br />

middleware examples in the real world, including some found in the Rails stack, as<br />

well as how you can build and use your own.<br />

18.4 Middleware<br />

When a request comes into a Rack application, it doesn’t go straight to a single place<br />

that serves the request. Instead, it goes through a series of pieces known as middleware,<br />

which may process the request before it gets to the end of the stack (your application)<br />

or modify it and pass it onward, as shown in figure 18.3.<br />

533

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

Saved successfully!

Ooh no, something went wrong!