You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/chapter00-before.adoc
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,6 @@
1
+
[#chapter00-before]
2
+
= Before
3
+
1
4
== Foreword
2
5
3
6
"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].
Copy file name to clipboardExpand all lines: en/chapter01-introduction.adoc
+18-17Lines changed: 18 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
-
== Introduction
1
+
[#chapter01-introduction]
2
+
= Introduction
2
3
3
4
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.
4
5
@@ -23,7 +24,7 @@ By the end or during the process (it really depends on your expertise), you will
23
24
* http://codeschool.com/[CodeSchool]
24
25
* http://jsonapi.org/format/[JSON API]
25
26
26
-
===Conventions on this book
27
+
== Conventions on this book
27
28
28
29
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.
29
30
@@ -43,22 +44,22 @@ I’ll be using some guidelines related to the language, what I mean by this is:
43
44
44
45
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].
45
46
46
-
=== Development environments
47
+
== Development environments
47
48
48
49
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.
49
50
50
-
==== Text editors and Terminal
51
+
=== Text editors and Terminal
51
52
52
53
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.
53
54
54
55
* *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.
55
56
* *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.
56
57
57
-
==== Browsers
58
+
=== Browsers
58
59
59
60
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.
60
61
61
-
==== Package manager
62
+
=== Package manager
62
63
63
64
* *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:
* *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.
71
72
72
-
==== Git
73
+
=== Git
73
74
74
75
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.
75
76
76
77
* sous Mac OS: `$ brew install git`
77
78
* sous Linux: `$ sudo apt-get install git`
78
79
79
-
==== Ruby
80
+
=== Ruby
80
81
81
82
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:
82
83
@@ -104,7 +105,7 @@ $ rvm install 2.5
104
105
105
106
If everything went smooth, it is time to install the rest of the dependencies we will be using.
106
107
107
-
===== Gems, Rails & Missing libraries
108
+
==== Gems, Rails & Missing libraries
108
109
109
110
First we update the gems on the whole system:
110
111
@@ -138,7 +139,7 @@ $ rails -v 5.2
138
139
5.2.0
139
140
----
140
141
141
-
===== Database
142
+
==== Database
142
143
143
144
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:
Initializing a Rails application must be pretty straightforward for you, if that is not the case, here is a super quick tutorial.
160
161
@@ -173,15 +174,15 @@ $ rails new market_place_api --skip-test --api
173
174
174
175
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.
175
176
176
-
==== Installing Pow or Prax
177
+
=== Installing Pow or Prax
177
178
178
179
You may ask yourself
179
180
180
181
> Why in the hell would I want to install this type of package?
181
182
182
183
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
183
184
184
-
===== Installing Pow
185
+
==== Installing Pow
185
186
186
187
NOTE: Pow only works on Mac OS, but don’t worry there is an alternative which mimics the functionality on Linux.
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.
210
211
211
-
===== Installing Prax
212
+
==== Installing Prax
212
213
213
214
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`.
214
215
@@ -237,7 +238,7 @@ NOTE: When using prax, you have to specify the port for the URL, in this case ht
237
238
238
239
You should see the application up and running, see image bellow:
239
240
240
-
=== Gemfile and Bundler
241
+
== Gemfile and Bundler
241
242
242
243
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.
243
244
@@ -298,7 +299,7 @@ $ bundle install
298
299
299
300
After the command finish its execution, it is time to start tracking the project with Git.
300
301
301
-
=== Versioning
302
+
== Versioning
302
303
303
304
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:
304
305
@@ -375,6 +376,6 @@ $ git push -u origin master
375
376
376
377
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.
377
378
378
-
=== Conclusion
379
+
== Conclusion
379
380
380
381
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!
Copy file name to clipboardExpand all lines: en/chapter02-api.adoc
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
-
== The API
1
+
[#chapter02-api]
2
+
= The API
2
3
3
4
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.
And as a quick recap, we really just update the `Gemfile` to add the `active_model_serializers` gem.
15
16
16
-
===Planning the application
17
+
== Planning the application
17
18
18
19
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.
19
20
@@ -25,7 +26,7 @@ We are not going to build views for displaying or interacting with the API, so n
25
26
26
27
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.
27
28
28
-
=== Setting the API
29
+
== Setting the API
29
30
30
31
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.
31
32
@@ -54,7 +55,7 @@ RESTful APIs must follow at least three simple guidelines:
54
55
55
56
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.
56
57
57
-
==== Routes, Constraints and Namespaces
58
+
=== Routes, Constraints and Namespaces
58
59
59
60
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:
60
61
@@ -166,7 +167,7 @@ $ git commit -m "Set the routes contraints for the api"
166
167
167
168
All right take a deep breath, drink some water, and let’s get going.
168
169
169
-
=== Api versioning
170
+
== Api versioning
170
171
171
172
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:
172
173
@@ -368,7 +369,7 @@ Finished in 0.00294 seconds (files took 0.06292 seconds to load)
368
369
2 examples, 0 failures
369
370
----
370
371
371
-
=== Conclusion
372
+
== Conclusion
372
373
373
374
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:
Copy file name to clipboardExpand all lines: en/chapter03-presenting-users.adoc
+10-9Lines changed: 10 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
-
== Presenting the users
1
+
[#chapter03-presenting-users]
2
+
= Presenting the users
2
3
3
4
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.
4
5
@@ -40,7 +41,7 @@ $ git checkout -b chapter3
40
41
41
42
Just make sure you are on the `master` branch before checking out.
42
43
43
-
===User model
44
+
== User model
44
45
45
46
We need to first add the `devise` gem into the `Gemfile`
46
47
@@ -161,7 +162,7 @@ $ git add .
161
162
$ git commit -m "Adds devise user model"
162
163
----
163
164
164
-
=== First user tests
165
+
== First user tests
165
166
166
167
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.
167
168
@@ -212,7 +213,7 @@ $ git add .
212
213
$ git commit -am 'Adds user firsts specs'
213
214
----
214
215
215
-
=== Improving validation tests
216
+
== Improving validation tests
216
217
217
218
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.
218
219
@@ -373,7 +374,7 @@ $ git add .
373
374
$ git commit -m "Adds show action the users controller"
374
375
----
375
376
376
-
==== Testing endpoints with CURL
377
+
=== Testing endpoints with CURL
377
378
378
379
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:
379
380
@@ -422,7 +423,7 @@ $ git add .
422
423
$ git commit -m "Updates application controller to prevent CSRF exception from being raised"
423
424
----
424
425
425
-
==== Creating users
426
+
=== Creating users
426
427
427
428
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.
428
429
@@ -546,7 +547,7 @@ $ git add .
546
547
$ git commit -m "Adds the user create endpoint"
547
548
----
548
549
549
-
==== Update users
550
+
=== Update users
550
551
551
552
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:
552
553
@@ -657,7 +658,7 @@ $ git add .
657
658
$ git commit -m "Adds update action the users controller"
658
659
----
659
660
660
-
==== Destroying users
661
+
=== Destroying users
661
662
662
663
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:
663
664
@@ -728,6 +729,6 @@ $ git add .
728
729
$ git commit -m "Adds destroy action to the users controller"
729
730
----
730
731
731
-
=== Conclusion
732
+
== Conclusion
732
733
733
734
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!
Copy file name to clipboardExpand all lines: en/chapter04-refactoring-tests.adoc
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
-
== Refactoring tests
1
+
[#chapter04-refactoring-tests]
2
+
= Refactoring tests
2
3
3
4
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.
4
5
@@ -136,7 +137,7 @@ So let’s add a method for handling the JSON response. If you have been followi
136
137
$ git checkout -b chapter4
137
138
----
138
139
139
-
===Refactoring the JSON response
140
+
== Refactoring the JSON response
140
141
141
142
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:
142
143
@@ -207,7 +208,7 @@ $ git add .
207
208
$ git commit -m "Refactors the json parse method"
208
209
----
209
210
210
-
=== Refactoring the format param
211
+
== Refactoring the format param
211
212
212
213
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:
213
214
@@ -248,7 +249,7 @@ As always this is a good time to commit:
248
249
$ git commit -am "Factorize format for unit tests"
249
250
----
250
251
251
-
=== Refactor before actions
252
+
== Refactor before actions
252
253
253
254
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:
254
255
@@ -351,6 +352,6 @@ $ git commit -am "Refactors test headers for each request"
351
352
352
353
Remember you can review the code up to this point at the https://github.com/madeindjs/api_on_rails[Github repository].
353
354
354
-
=== Conclusion
355
+
== Conclusion
355
356
356
357
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