Skip to content

Commit bce8a2c

Browse files
committed
Reorganize file for EPUB export
1 parent c82e398 commit bce8a2c

26 files changed

+267
-347
lines changed

en/api_on_rails.adoc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
= API on Rails 5
22
Alexandre Rousseau <contact@rousseau-alexandre.fr>
33
v1.0, 2019-01-08
4+
:doctype: book
45
:toc:
56
:imagesdir: img
67
:title-logo-image: image:logo.svg[]
@@ -13,46 +14,46 @@ v1.0, 2019-01-08
1314
:author: Alexandre Rousseau
1415
:description: Learn best practice to build an API using Ruby on Rails 5
1516
:front-cover-image: image:cover.svg[]
16-
:revdate: 2019-01-09
17+
:revdate: 2019-01-10
1718

18-
include::./00_before.adoc[]
19+
include::chapter00-before.adoc[]
1920

2021
<<<
2122

22-
include::./01_introduction.adoc[]
23+
include::chapter01-introduction.adoc[]
2324

2425
<<<
2526

26-
include::./02_api.adoc[]
27+
include::chapter02-api.adoc[]
2728

2829
<<<
2930

30-
include::./03_presenting_users.adoc[]
31+
include::chapter03-presenting-users.adoc[]
3132

3233
<<<
3334

34-
include::./04_refactoring_tests.adoc[]
35+
include::chapter04-refactoring-tests.adoc[]
3536

3637
<<<
3738

38-
include::./05_athentification.adoc[]
39+
include::chapter05-athentification.adoc[]
3940

4041
<<<
4142

42-
include::./06_user_products.adoc[]
43+
include::chapter06-user-products.adoc[]
4344

4445
<<<
4546

46-
include::./07_improve_json.adoc[]
47+
include::chapter07-improve-json.adoc[]
4748

4849
<<<
4950

50-
include::./08_placing_orders.adoc[]
51+
include::chapter08-placing-orders.adoc[]
5152

5253
<<<
5354

54-
include::./09_improve_orders.adoc[]
55+
include::chapter09-improve-orders.adoc[]
5556

5657
<<<
5758

58-
include::./10_optimization.adoc[]
59+
include::chapter10-optimization.adoc[]

en/00_before.adoc renamed to en/chapter00-before.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[#chapter00-before]
2+
= Before
3+
14
== Foreword
25

36
"API on Rails 5" is based on http://apionrails.icalialabs.com/book/["APIs on Rails: Building REST APIs with Rails"]. It was initially published in 2014 by https://twitter.com/kurenn[Abraham Kuri] under the licenses http://opensource.org/licenses/MIT[MIT] and http://people.freebsd.org/~phk/[Beerware].

en/01_introduction.adoc renamed to en/chapter01-introduction.adoc

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
== Introduction
1+
[#chapter01-introduction]
2+
= Introduction
23

34
Welcome to https://github.com/madeindjs/api_on_rails[APIs on Rails] a tutorial on steroids on how to build your next API with Rails. The goal of this book is to provide an answer on how to develop a RESTful API following the best practices out there, along with my own experience. By the time you are done with _API’s on Rails_ you should be able to build your own `API` and integrate it with any clients such as a web browser or your next mobile app. The code generated is built on top of Rails 4 which is the current version, for more information about this check out http://rubyonrails.org/. The most up-to-date version of the _API’s on Rails_ can be found on https://github.com/madeindjs/api_on_rails[APIs on Rails]; don’t forget to update your offline version if that is the case.
45

@@ -23,7 +24,7 @@ By the end or during the process (it really depends on your expertise), you will
2324
* http://codeschool.com/[CodeSchool]
2425
* http://jsonapi.org/format/[JSON API]
2526
26-
=== Conventions on this book
27+
== Conventions on this book
2728

2829
The conventions on this book are based on the ones from http://www.railstutorial.org/book/beginning#sec-conventions[Ruby on Rails Tutorial]. In this section I’ll mention some that may not be so clear.
2930

@@ -43,22 +44,22 @@ I’ll be using some guidelines related to the language, what I mean by this is:
4344

4445
If for any reason you encounter some errors when running a command, rather than trying to explain every possible outcome, I’ll will recommend you to `google it', which I don’t consider a bad practice or whatsoever. But if you feel like want to grab a beer or have troubles with the tutorial you can always mailto:[email protected][email me].
4546

46-
=== Development environments
47+
== Development environments
4748

4849
One of the most painful parts for almost every developer is setting everything up, but as long as you get it done, the next steps should be a piece of cake and well rewarded. So I will guide you to keep you motivated.
4950

50-
==== Text editors and Terminal
51+
=== Text editors and Terminal
5152

5253
There are many cases in which development environments may differ from computer to computer. That is not the case with text editors or IDE’s. I think for Rails development an IDE is way to much, but some other might find that the best way to go, so if that it’s your case I recommend you go with http://www.aptana.com/products/radrails[RadRails] or http://www.jetbrains.com/ruby/index.html[RubyMine], both are well supported and comes with many integrations out of the box.
5354

5455
* *Text editor*: I personally use http://www.vim.org/[vim] as my default editor with https://github.com/carlhuda/janus[janus] which will add and handle many of the plugins you are probably going to use. In case you are not a _vim_ fan like me, there are a lot of other solutions such as http://www.sublimetext.com/[Sublime Text] which is a cross-platform easy to learn and customize (this is probably your best option), it is highly inspired by http://macromates.com/[TextMate] (only available for Mac OS). A third option is to use a more recent text editor from the guys at http://gitub.com[Github] called https://atom.io/[Atom], it’s a promising text editor made with Javascript, it is easy to extend and customize to meet your needs, give it a try. Any of the editors I present will do the job, so I’ll let you decide which one fits your eye.
5556
* *Terminal*: If you decided to go with http://icalialabs.github.io/kaishi/[kaishi] for setting the environment you will notice that it sets the default shell to `zsh`, which I highly recommend. For the terminal, I’m not a fan of the _Terminal_ app that comes out of the box if you are on Mac OS, so check out http://www.iterm2.com/#/section/home[iTerm2], which is a terminal replacement for Mac OS. If you are on Linux you probable have a nice terminal already, but the default should work just fine.
5657

57-
==== Browsers
58+
=== Browsers
5859

5960
When it comes to browsers I would say http://www.mozilla.org/en-US/firefox/new/[Firefox] immediately, but some other developers may say https://www.google.com/intl/en/chrome/browser/[Chrome] or even https://www.apple.com/safari/[Safari]. Any of those will help you build the application you want, they come with nice inspector not just for the DOM but for network analysis and many other features you might know already.
6061

61-
==== Package manager
62+
=== Package manager
6263

6364
* *Mac OS*: There are many options to manage how you install packages on your Mac, such as https://www.macports.org/[Mac Ports] or http://brew.sh/[Homebrew], both are good options but I would choose the last one, I’ve encountered less troubles when installing software and managing it. To install `brew` just run the command below:
6465

@@ -69,14 +70,14 @@ $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/inst
6970

7071
* *Linux*: You are all set!, it really does not matter if you are using `apt`, `pacman`, `yum` as long you feel comfortable with it and know how to install packages so you can keep moving forward.
7172

72-
==== Git
73+
=== Git
7374

7475
We will be using Git a lot, and you should use it too not just for the purpose of this tutorial but for every single project.
7576

7677
* sous Mac OS: `$ brew install git`
7778
* sous Linux: `$ sudo apt-get install git`
7879

79-
==== Ruby
80+
=== Ruby
8081

8182
There are many ways in which you can install and manage ruby, and by now you should probably have some version installed if you are on Mac OS, to see which version you have, just type:
8283

@@ -104,7 +105,7 @@ $ rvm install 2.5
104105

105106
If everything went smooth, it is time to install the rest of the dependencies we will be using.
106107

107-
===== Gems, Rails & Missing libraries
108+
==== Gems, Rails & Missing libraries
108109

109110
First we update the gems on the whole system:
110111

@@ -138,7 +139,7 @@ $ rails -v 5.2
138139
5.2.0
139140
----
140141

141-
===== Database
142+
==== Database
142143

143144
I highly recommend you install http://www.postgresql.org/[Postgresql] to manage your databases, but for simplicity we’ll be using http://www.sqlite.org/[SQlite]. If you are using Mac OS you should be ready to go, in case you are on Linux, don’t worry we have you covered:
144145

@@ -154,7 +155,7 @@ or
154155
$ sudo yum install libxslt-devel libxml2-devel libsqlite3-devel
155156
----
156157

157-
=== Initializing the project
158+
== Initializing the project
158159

159160
Initializing a Rails application must be pretty straightforward for you, if that is not the case, here is a super quick tutorial.
160161

@@ -173,15 +174,15 @@ $ rails new market_place_api --skip-test --api
173174

174175
As you may guess, the commands above will generate the bare bones of your Rails application. The next step is to add some `gems` we’ll be using to build the api.
175176

176-
==== Installing Pow or Prax
177+
=== Installing Pow or Prax
177178

178179
You may ask yourself
179180

180181
> Why in the hell would I want to install this type of package?
181182

182183
and the answer is simple, we will be working with http://en.wikipedia.org/wiki/Subdomain[subdomains], and in this case using services like http://pow.cx/[Pow] or https://github.com/ysbaddaden/prax[Prax] help us achieve that very easily
183184

184-
===== Installing Pow
185+
==== Installing Pow
185186

186187
NOTE: Pow only works on Mac OS, but don’t worry there is an alternative which mimics the functionality on Linux.
187188

@@ -208,7 +209,7 @@ $ ln -s ~/workspace/market_place_api
208209

209210
Remember to change the user directory to the one matches yours. You can now access the application through http://market_place_api.dev/. Your application should be up a running by now.
210211

211-
===== Installing Prax
212+
==== Installing Prax
212213

213214
For linux users only, https://github.com/ysbaddaden/prax.cr[Prax] distribute some Debian/Ubuntu precompiled packages. You only have to download `.deb` and instal with `dpkg`.
214215

@@ -237,7 +238,7 @@ NOTE: When using prax, you have to specify the port for the URL, in this case ht
237238

238239
You should see the application up and running, see image bellow:
239240

240-
=== Gemfile and Bundler
241+
== Gemfile and Bundler
241242

242243
Once the Rails application is created, the next step is adding a simple but very powerful gem to serialize the resources we are going to expose on the api. The gem is called `active_model_serializers` which is an excellent choice to go when building this type of application. It is well maintained and the https://github.com/rails-api/active_model_serializers[documentation] is amazing.
243244

@@ -298,7 +299,7 @@ $ bundle install
298299

299300
After the command finish its execution, it is time to start tracking the project with Git.
300301

301-
=== Versioning
302+
== Versioning
302303

303304
Remember that Git helps you track and maintain history of your code. Keep in mind source code of the application is published on Github. You can follow the repository at https://github.com/madeindjs/api_on_rails[Github]. I’ll assume you have Git already configured and ready to use to start tracking the project. If that is not your case, follow these first-time setup steps:
304305

@@ -375,6 +376,6 @@ $ git push -u origin master
375376

376377
As we move forward with the tutorial, I’ll be using the practices I follow on my daily basis, this includes working with `branches`, `rebasing`, `squash` and some more. For now you don’t have to worry if some of these don’t sound familiar to you, I walk you through them in time.
377378

378-
=== Conclusion
379+
== Conclusion
379380

380381
It’s been a long way through this chapter, if you reach here let me congratulate you and be sure that from this point things will get better. So let’s get our hands dirty and start typing some code!

en/02_api.adoc renamed to en/chapter02-api.adoc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
== The API
1+
[#chapter02-api]
2+
= The API
23

34
In this section I’ll outline the application. By now you should have the bare bones of the application. If you did not read it I recommend you to do it.
45

@@ -13,7 +14,7 @@ $ git checkout -b chapter1 b98a9a7a328017640482af95beebc1d6e612e0ac
1314

1415
And as a quick recap, we really just update the `Gemfile` to add the `active_model_serializers` gem.
1516

16-
=== Planning the application
17+
== Planning the application
1718

1819
As we want to go simple with the application, it consists on five models. Don’t worry if you don’t fully understand what is going on, we will review and build each of these resources as we move on with the tutorial.
1920

@@ -25,7 +26,7 @@ We are not going to build views for displaying or interacting with the API, so n
2526

2627
By this point you must be asking yourself, all right but I need to explore or visualize the api we are going to be building, and that’s fair. Probably if you google something related to api exploring, an application called https://www.getpostman.com/[Postman] will pop. It is a great software but we won’t be using that anyway because we'll use cURL who allow anybody to reproduce request on any computer.
2728

28-
=== Setting the API
29+
== Setting the API
2930

3031
An API is defined by http://en.wikipedia.org/wiki/Application_programming_interface[wikipedia] as _an application programming interface (API) specifies how some software components should interact with each other._ In other words the way systems interact with each other through a common interface, in our case a web service built with JSON. There are other kinds of communication protocols like SOAP, but we are not covering that in here.
3132

@@ -54,7 +55,7 @@ RESTful APIs must follow at least three simple guidelines:
5455

5556
This might not be clear enough or may look like a lot of information to digest but as we move on with the tutorial, hopefully it’ll get a lot easier to understand.
5657

57-
==== Routes, Constraints and Namespaces
58+
=== Routes, Constraints and Namespaces
5859

5960
Before start typing any code, we prepare the code with git, the workflow we’ll be using a branch per chapter, upload it to Github and then merge it with master, so let’s get started open the terminal, `cd` to the `market_place_api` directory and type in the following:
6061

@@ -166,7 +167,7 @@ $ git commit -m "Set the routes contraints for the api"
166167

167168
All right take a deep breath, drink some water, and let’s get going.
168169

169-
=== Api versioning
170+
== Api versioning
170171

171172
At this point we should have a nice routes mapping using a subdomain for name spacing the requests, your `routes.rb` file should look like this:
172173

@@ -368,7 +369,7 @@ Finished in 0.00294 seconds (files took 0.06292 seconds to load)
368369
2 examples, 0 failures
369370
----
370371

371-
=== Conclusion
372+
== Conclusion
372373

373374
It’s been a long way, I know, but you made it, don’t give up this is just our small scaffolding for something big, so keep it up. In the meantime and I you feel curious there are some gems that handle this kind of configuration:
374375

en/03_presenting_users.adoc renamed to en/chapter03-presenting-users.adoc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
== Presenting the users
1+
[#chapter03-presenting-users]
2+
= Presenting the users
23

34
In the last chapter we manage to set up the bare bones for our application endpoints configuration. We even added versioning through headers. In a next chapter we will handle users authentication through authentication tokens as well as setting permissions to limit access for let’s say signed in users. In coming chapters we will relate `products` to users and give them the ability to place orders.
45

@@ -40,7 +41,7 @@ $ git checkout -b chapter3
4041

4142
Just make sure you are on the `master` branch before checking out.
4243

43-
=== User model
44+
== User model
4445

4546
We need to first add the `devise` gem into the `Gemfile`
4647

@@ -161,7 +162,7 @@ $ git add .
161162
$ git commit -m "Adds devise user model"
162163
----
163164

164-
=== First user tests
165+
== First user tests
165166

166167
We will add some specs to make sure the `user` model responds to the `email`, `password` and `password_confirmation` attributes provided by devise, let’s add them. Also for convenience we will modify the `users` factory file to add the corresponding attributes.
167168

@@ -212,7 +213,7 @@ $ git add .
212213
$ git commit -am 'Adds user firsts specs'
213214
----
214215

215-
=== Improving validation tests
216+
== Improving validation tests
216217

217218
It is showtime people, we are building our first endpoint. We are just going to start building the `show` action for the user which is going to expose a `user` record in plain old JSON. We first need to generate the `users_controller`, add the corresponding tests and then build the actual code.
218219

@@ -373,7 +374,7 @@ $ git add .
373374
$ git commit -m "Adds show action the users controller"
374375
----
375376

376-
==== Testing endpoints with CURL
377+
=== Testing endpoints with CURL
377378

378379
So we finally have an endpoint to test. There are plenty of options to start playing with. The first that come to my mind is using http://curl.haxx.se/[cURL] because is built-in on almost any Linux distribution and of course on your Mac OSX. So let’s try it out:
379380

@@ -422,7 +423,7 @@ $ git add .
422423
$ git commit -m "Updates application controller to prevent CSRF exception from being raised"
423424
----
424425

425-
==== Creating users
426+
=== Creating users
426427

427428
Now that we have a better understanding on how to build endpoints and how they work, it’s time to add more abilities to the API. One of the most important is letting the users actually create a profile on our application. As usual we will write tests before implementing our code extending our testing suite.
428429

@@ -546,7 +547,7 @@ $ git add .
546547
$ git commit -m "Adds the user create endpoint"
547548
----
548549

549-
==== Update users
550+
=== Update users
550551

551552
The pattern for *updating* users is very similar as *creating* new ones. If you are an experienced Rails developer you may already know the differences between these two actions:
552553

@@ -657,7 +658,7 @@ $ git add .
657658
$ git commit -m "Adds update action the users controller"
658659
----
659660

660-
==== Destroying users
661+
=== Destroying users
661662

662663
So far we have built a bunch of actions on the users controller along with their tests but we have not ended yet. We are just missing one more which is the destroy action. So let’s do that:
663664

@@ -728,6 +729,6 @@ $ git add .
728729
$ git commit -m "Adds destroy action to the users controller"
729730
----
730731

731-
=== Conclusion
732+
== Conclusion
732733

733734
Oh you are here!, great job! I know it probably was a long way, but don’t give up you are doing it great. Make sure you are understanding every piece of code, things will get better, in next chapter we will refactor our tests to clean our code a bit and make it easy to extend the test suite more. So stay with me guys!

en/04_refactoring_tests.adoc renamed to en/chapter04-refactoring-tests.adoc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
== Refactoring tests
1+
[#chapter04-refactoring-tests]
2+
= Refactoring tests
23

34
In previous chapter we manage to put together some `user` resources endpoints. If you skip it (or simple missed it) I highly recommend you take a look at it. It covers the first test specs and an introduction to JSON responses.
45

@@ -136,7 +137,7 @@ So let’s add a method for handling the JSON response. If you have been followi
136137
$ git checkout -b chapter4
137138
----
138139

139-
=== Refactoring the JSON response
140+
== Refactoring the JSON response
140141

141142
Back to the `refactor`, we will add file under the `spec/support` directory. Currently we don’t have this directory. So let’s add it:
142143

@@ -207,7 +208,7 @@ $ git add .
207208
$ git commit -m "Refactors the json parse method"
208209
----
209210

210-
=== Refactoring the format param
211+
== Refactoring the format param
211212

212213
We want to remove the `format` param sent on every request and instead of that let’s handle the response we are expecting through headers. This is extremely easy just by adding one line to our `users_controller_spec.rb` file:
213214

@@ -248,7 +249,7 @@ As always this is a good time to commit:
248249
$ git commit -am "Factorize format for unit tests"
249250
----
250251

251-
=== Refactor before actions
252+
== Refactor before actions
252253

253254
I’m very happy with the code we got so far but we can still improve it a little bit. The first thing that comes to my mind is to group the three custom headers being added before each request:
254255

@@ -351,6 +352,6 @@ $ git commit -am "Refactors test headers for each request"
351352

352353
Remember you can review the code up to this point at the https://github.com/madeindjs/api_on_rails[Github repository].
353354

354-
=== Conclusion
355+
== Conclusion
355356

356357
Nice job on finishing this chapter. Although it was a short one it was a crucial step as this will help us write better and faster tests. On next chapter we will add the authentication mechanism so we’ll be using across the application as well as limiting the access for certain actions.

0 commit comments

Comments
 (0)