RESTful API

Hey guys, it’s been nearly a month since I’ve written my last post. I’ve been busy with university.

I’ve finished my little project, a recreation of a basic forum. It has user authentication, the abilty to make post entries and also commenting is possible. In the beginning I had alot more ideas for my first project but wasn’t fully convinced to bring them to reality. I wanted to use a payment system using paypal or bitcoin but had to realise I dont fully understood the concept of using Paypal’s API. The intoductions on youtube didnt quite help me through the process of implementing it, probably due to lack of generall knowledge of API’s.

With the following blockpost, I want to teach myself , how to code a RESTful Rails API.

 

What is an API?

 

Application

Programming

Interface

 

We have to reverse the order of the words to get the meaning: It’s an interface that programs use to interact with applications.

APIs expose usefull stuff. We can interact with them via the HTTP protocol.

http://www.programmableweb.com is a huge depository for Web APIs from Google Maps to Facebook.

I was talking about exposing usefull stuff prior. Taking google maps as an example, this stuff would be location information around the globe. We can even customize the look of the google maps theme in our HTML file.

If we want to have the coordinates for a specific adress, let’s say my homeadress, we can interact with the google maps API by typing in:

http://maps.googleapis.com/maps/api/geocode/json?address=23 Badstrasse

what we get is JSON(JavaScriptObjectNotation) code:

 

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "23",
               "short_name" : "23",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Badstraße",
               "short_name" : "Badstraße",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mitte",
               "short_name" : "Mitte",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Berlin",
               "short_name" : "Berlin",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Berlin",
               "short_name" : "Berlin",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Deutschland",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "13357",
               "short_name" : "13357",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Badstraße 23, 13357 Berlin, Deutschland",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 52.55224519999999,
                  "lng" : 13.3825176
               },
               "southwest" : {
                  "lat" : 52.5520732,
                  "lng" : 13.3822212
               }
            },
            "location" : {
               "lat" : 52.5521592,
               "lng" : 13.3823694
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 52.55350818029149,
                  "lng" : 13.3837183802915
               },
               "southwest" : {
                  "lat" : 52.5508102197085,
                  "lng" : 13.3810204197085
               }
            }
         },
         "place_id" : "ChIJx0I9EiRSqEcRzfRY_Gk2c4Q",
         "types" : [ "premise" ]
      }

 

I’ve marked the information we wanted:

"lat" : 52.55224519999999,
"lng" : 13.3825176

 

That was a very quick and easy example of a User using an HTTP request to the google maps API.

A webhook is a way of telling an API to push information to my webapp instead of requesting this information. This way we can heavily reduce server loading time.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s