website (3)


How I created an API in less than an HOUR!

Check out the API on https://city-api.herokuapp.com/

Okay, first things first, 2 days back one of my friends asked me to help in building an application where he was using some API to make calls and do some “stuff”. Sure, I helped him. But with that I was wondering what would it take to build an API of my own.

Then, today I was looking for some of the methods available for forms, where you have to first select COUNTRY and then CITIES load asynchronously.

Interestingly, most of the methods that exist require you to store the information in your own database (BUT, this puts extra load on your database, and you don’t even need this data unless user is filling forms). So I though why not create an API for that!

Enough with the reason, for why I created the API!

Let’s start building one.

Environment:

Application Framework: Ruby on Rails

Database: postgreSQL

Hosting Provider: Heroku

Text Editor: Sublime Text 3

  • To start building the application you need to have ruby and rails setup on your computer system. (Google it for help)
  • In the terminal type command rails new cityapi, this will create some files and directory. After finishing move into the directory and open the folder in Sublime.
  • Now download the CSV file for city data from Geolite.(For convenience you can remove columns(except city and country) from the file). Put the file in the root folder of your app.
  • Now create an model called city.

Command – rails g model city name:string country:string

  • Generate controller and views.

Command – rails g controller city 

  • Modify routes for

get ’/’ => ‘city#index’.

  • In the gemfile (for postgreSQL) add
 gem ‘pg’
  • Add some code for seeding the database from CSV file.

require ‘csv’

puts “Importing countries…”
num = 1
CSV.foreach(Rails.root.join(“countries.csv”), headers: true) do |row|
City.create! do |city|
city.id = num
city.country = row[0]
city.name = row[1]
num += 1
end
end

  • Add an index method in city controller, defined as:

The below mentioned content helps the application to generate response in accordance with the API request. SQL query is made depending upon the parameters and a JSON response is generated for the user.

def index
if params[:country].present?
@cities = City.where(‘country ILIKE ?’, params[:country]).pluck(:name)
if @cities.present?
respond_to do |format|
format.html { render json: @cities, status: :ok }
format.json { render json: @cities, status: :ok }
end
else
respond_to do |format|
format.html { render json: [“Error”], status: :not_found }
format.json { render json: [“Error”], status: :not_found }
end
end
end

if params[:city].present?
@country = City.where(‘name ILIKE ?’, params[:city]).pluck(:country)
if @country.present?
respond_to do |format|
format.html { render json: @country, status: :ok }
format.json { render json: @country, status: :ok }
end
else
respond_to do |format|
format.html { render json: [“Error”], status: :not_found }
format.json { render json: [“Error”], status: :not_found }
end
end
end
end

  • We are almost done!
  • Modify the view/city/index.html.erb as you like. It will sever as homepage.
  • Go to heroku.com and signup. Create an application and get application name.
  • Now some commands in sequence to make your application live.

heroku git:remote -a your_heroku_app_name

git init 

git add –all

git commit -m “first commit”

git push heroku master

heroku run rake db:migrate

heroku run rake db:seed

  • What are you waiting for, go check your application/API.

This was one of my fastest developed application. (Writing this blog post took more time than the application :P). If you haven’t checked out the application yet go ahead give it a try https://city-api.herokuapp.com/.

If you want you can check out the git repo too, visit: cityAPI.

P.S.- Do comment for help/queries and suggestions.




ImageScrape – A web based imagescraper

ImageScrape – A web based imagescraper




A quick guide on ´how to make your own website!´

If you want to make a static website for your own purposes (or just for fun). Then you are on right track just follow the basics instructions and will end up having a website of your own!

Okay, let’s get started!

Getting a Domain name

  1. Go to godaddy.comnamecheap.com search for your preferred name, check if available, be creative about it because it’s most likely that you will not find it available.
  2. For personal webpage try getting .me(it’s actually is a country level TLD but still increasingly used for personal use) TLD or if you want to extend your domain to professional use get a .com/.co TLD. 
  3. After you have decided your Domain Name, always visit services like retailmenot.com and find a valid offer for that TLD. If you are lucky you can get discounts upto 50%!
  4. Buy that domain. Also check what free services you will be getting with that domain, like free web-hosting service, free e-mails, website builder, etc.

Get a hosting service!

  1. Free Service. A WEB-hosting service, if want a money saver service, 000webhost.com is a good choice. It’s Free for upto 1.5GB data and 100GB bandwidth.
  2. Paid Service. If you want good, speedy and reliable website then better go for a paid hosting service like hostgator.com or any other service you find cheap (Google for it!). For this you will be charged at about 250/month for the basic plan. With services like unlimited subdomains and emails. With an uptime greater than 99.9%.

Designing the Web-pages!

  1.  For designing you can either use the website builder provided by the hosting service, they are pretty good for personal websites like about pages!
  2. OR you can go for a CMS like joomla or WORDPRESS, wordpress is very famous nowadays for having websites with a blog in it. About 15% of top 1 Million Alexa Websites are built on wordpress. If you choose a CMS then find a nice theme (just Google for it!).

  3. THE BEST WAY. And if you are still not satisfied and a want learn more then go and learn HTML, CSS, JS! And create one of your own! For learning try codecademy.org, they are pretty good!

Make it LIVE!

  1.  If you have used ‘Website-Builder’ then just click on PUBLISH, you are live!
  2. If CMS, then there is specific methods for different CMS. Depending upon the CMS you have used go to their website and follow instructions!
  3. If designed one of your own! (Good for you, it’s the second easiest)

    1. Download filezilla, it’s a freeware software!

    Filezilla Logo

    3. Get all your files ready

    Oh! I missed second! Actually it’s obvious, install filezilla 😛

    4. Log-on to your ftp server using filezilla, you will get the details from your hosting service.

    5. Upload all the files onto your server!

    What are you waiting for, it’s live go check it out on yourdomain.tld!

Hurray!
Glossary & Further reading:
1. TLD: Top Level Domain
2. JS: JavaScript
3. CMS: Content Management System

Thank You!

P.S. – If you have any doubts and need any further guidance drop a mail at me@gauravgoyal.in.