Ruby net http



Keywords: ruby net http
Description: In this article, we’re going to take a look at the Ruby’s Net::HTTP library, focusing on REST API implementation.

Every so often, we find ourselves in need of accessing REST APIs. There are some great libraries out there for this, but what about the library behind these libraries? In this article, we’re going to take a look at the Ruby’s Net::HTTP library. We’ll go through simple REST API principals like:

REST stands for RE presentational S tate T ransfer. It was theorized by Roy Fielding in his doctoral dissertation: Architectural Styles and the Design of Network-based Software Architectures. then later implemented by the World Wide Web Consortium (W3C) for HTTP 1.1. It’s basically a software (mainly web) architecture style that allows for easy API access, along with some other helpful goals.

REST APIs focuses on four main verbs: GET. POST. PUT. and DELETE. We can use Ruby’s Net::HTTP library to access REST API’s. In this article, we’ll learn how to do this, as well as some other goodies. If you want full definitions of all HTTP 1.1 methods, then you should visit this W3C page .

Let’s use Sinatra as our little API server. If you’re unfamiliar with Sinatra, you can learn the basics in 10 minutes. Here’s our server:

A GET request gets. or reads information from a server. This request is quite common. When you visited this page, your browser made a GET request which returned a hypertext document from the server. You can do this same thing in Ruby. In our example, we’re going to be getting data from our server.

This simple example pulls data from our server and returns it. Elegant, right? Here it is in ugly one-liner form:

As an intelligent SitePoint Ruby reader like you can obviously see, this code has some issues. Don’t worry, we’ll get to most of these later in the advanced Net::HTTP bits.

A POST request posts. or sends data/parameters to a server. APIs use this for recieving data from a user. This request is also quite common. Every time you send a tweet, you’re sending a POST request to Twitter with your tweet as parameters. In this example, we’re going to be send our server a POST request.

A PUT request puts. or updates something on a server. APIs use this mainly when updating data on the server. For example, if you edit a pull request on GitHub, you’re sending a PUT request with your data. Unfortunately, PUT is not very well documented in the Ruby Net::HTTP library, so there is no elegant solution to it.

I’m not going to show this as a one liner because it would be over 80 characters, which would not only be unreadable, but also goes against the Ruby Style Guide .

A DELETE request deletes something from the server. If you were to delete a tweet, you would be sending a delete request to Twitter. This example is very similar to the last.

Great. We’ve gone through the basics and REST verbs using Ruby. Keep in mind, there are multiple ways of doing most of these HTTP requests. We’ll go through some of these in the next section.

For the advanced section, our server is going to have to be more complicated in order to return and recieve data like the API we are trying to mimic in each example. So, the server will need to be changed in each example.

From now on, we will be focussing on the GET and POST HTTP methods. If you need to apply the techniques below to the other methods, just mix the simple version of the request with the advanced technique.

In the principle of DRY. HTTP blocks are important. HTTP blocks are simply Net::HTTP blocks. This may seem a little confusing, but it’s quite a simple concept.

We’ll be using these in the next examples, so get familiar. Just keep in mind that http_request is the same thing as Net::HTTP.start. it’s just a shortcut.

Hmm… that gave us nothing. But, being the smart people that we are, we try a second simple method.

That gave us more information, but still no 200 OK. What can we do to follow these redirects without getting lost? There are two methods.

As you can see, this method is very similar to the other one. It all depends on preference and how much control you want over the request.

In the previous examples, we used headers to find the 301/302 redirect location. In the next example, we’ll isolate this ability so we can access any part of the headers.

First, let’s make a request and see what headers we get. We’ll be using the first, simple server in this example.

Pretty sweet, huh? If you don’t need the all the headers, you can just use response['something'] as a shortcut.

HTTP headers are quite useful. You can get all sorts of information about the data source and leverage this information to your advantage.

Here’s a less common situation, but equally as useful as the last one. Sometimes APIs offer basic http authentication. Let’s simulate that in our server:

Of course, this type of authentication is not very common with OAuth being in existance, but it’s always good to know.

Sometimes as developers, we need a programmatic way to download files. This is as easy as making a GET request and piping each line into a new file. For this example, we’re going to use a real-world file from the internet. There’s no point in rewriting our server for this situation.

Simple right? But we’re savvy programmers. What about live streaming? Let’s have some fun.

With small files, this second method is just several lines of code longer, but on larger files, this can speed up downloads greatly.

For this next example, we’ll be using Google because it uses SSL for its connection. We won’t be using Sinatra because it’s out of the scope of this tutorial to configure it.

You can do so many things with the Net::HTTP library. It is extensible, but also simple to use. I suggest that you also check out the HTTParty. Rest-Client. and Curb gems. They provide a very simple way to access these same methods in a better way. Whatever you choose, remember that anything your browser can do Ruby can do as well.




Photogallery Ruby net http:


RWBY - Ruby http://www.pixiv.net/member_illust.php?mode ...


Spring 2014 Migration of Ruby-throated Hummingbirds ~ Ruby ...


Image from http://static.zerochan.net/Ruby.Rose.full.1694495.jpg ...


Ruby Rose by asukaziye (rwby) | Today I'm Fine Without You. | Pintere


rwyb___ruby_rose_render_by_ ...


Serebii.net on Twitter: "Serebii Picture: Special artwork for Mega ...


Forum


Item Requests: RWBY weapons - Other Help - Video/Series Help ...


Forum


AMD nueva mascota Ruby - Tasha Chin Spiralcats - Taringa!


It's a One-Hit KO!


Artwork by Black Box http://www.pixiv.net/member_illust.php?mode ...


rwby___ruby_rose_by_o121do1-d6 ...


KH13 BATTLE ROYALE (Summer) 2013 - Page 55 - Forum Games - KH13 ...


Forum


Ella's Garden cubit: Group Buys forum: Closed...ORDERS for ...


Pokemon Omega Ruby & Alpha Sapphire Review: Blast from the Past ...


Ruby x Sapphire by Yamino on DeviantArt


Happy 15th anniversary, Final Fantasy VIII!


New Starter Decks, will they have outfit rewards? - Page 2