concept request in category ruby

This is an excerpt from Manning's book Rails 4 in Action: Revised Edition of Rails 3 in Action.
The config/routes.rb file of every Rails application is where the application routes are defined in succinct Ruby syntax. The methods used in this file define the pathways from requests to controllers. If you look in your config/routes.rb file, ignoring the commented-out lines for now, you’ll see what’s shown in the following listing.
Rack is the underlying web server framework that powers the request/response cycle found in Rails, but it isn’t a part of Rails itself. It’s completely separate, with Rails requiring the parts of Rack it needs. When your application runs, it’s run through a web server. When your web server receives a request, it will pass it off to Rack, as shown in figure 15.1.
Rack then determines where to route this request, and in this case it’s chosen to route to a specific application stack. The request passes through a series of pieces called middleware (covered in the final section of this chapter) before arriving at the application itself. The application will then generate a response and pass it back up through the stack to Rack, and then Rack will pass it back to the server, which will finally pass it back to the browser. All of this happens in lightning-quick fashion.
This means that requests are able to get to your Rack app, and that the response you’ve declared is being served successfully. Now you need to fill this response with meaningful data.
Listing 15.14. Adding API request specs for error handling
describe API::V2::Tickets do ... context "unsuccessful requests" do it "doesn't allow requests that don't pass through an API key" do get url expect(response.status).to eq 401 expect(response.body).to include "Unauthenticated" end it "doesn't allow requests that pass an invalid API key" do get url, {}, { "HTTP_AUTHORIZATION" => "Token token=notavalidkey" } expect(response.status).to eql 401 expect(response.body).to include "Unauthenticated" end it "doesn't allow access to a ticket that the user doesn't have permission to read" project.roles.delete_all get url, {}, headers expect(response.status).to eq 404 end end end

This is an excerpt from Manning's book Rails 3 in Action.
The config/routes.rb file of every Rails application is where the application routes are defined in a succinct Ruby syntax. The methods used in this file define the pathways from requests to controllers. If you look in your config/routes.rb while ignoring the commented-out lines for now, you’ll see what’s shown in the following listing.
When learning about these actions, you’ll see a lot of reference to HTTP status codes, which are the standard for all pages of the web. These status codes play a critical role in an API, providing key information such as if the request was successful (the 200 status code) or if the user is unauthorized (the 401 status code) to perform the request. These are standardized ways of quickly informing people of the result of their requests.
With the callback to create the token in place, let’s jump back to your spec and write a test to make a request with this token. Directly underneath the let(:user) in spec/api/v1/projects_spec.rb, you’ll put the code from the following listing.
You’re using another let to define a token method that, when called, will return the authentication_token for the current user. You’ll use this later for authenticating requests for your API. The get method
you use here is provided by Rack::Test::Methods and simply makes a GET request with the provided URL. You put the URL in a let because you don’t want to repeat the URL too many times if you have multiple tests, and the let stops you from repeating yourself in your code.
After the request is done in the test, you ensure that the last_response.status returns 200, which is the HTTP status code for OK and means the request was successful. The rest of this spec tests that the data contained within last_response.body contains the appropriate data. This to_json method will take the attributes for each project returned and turn them into JSON, resulting in an output such as
you use here is provided by Rack::Test::Methods and simply makes a GET request with the provided URL. You put the URL in a let because you don’t want to repeat the URL too many times if you have multiple tests, and the let stops you from repeating yourself in your code.