Technology (12)


Generate QR code images in Ruby on Rails app

Create a QR code image along with some content written below it.

Sample Image




Automating back-up of your Google Drive on S3

Problem Statement:  I am working with a lot of freelancers for various small creative and content related work but they usually share the completed work as a folder in Google drive. Keep in mind that they are the owner of this folder. After 1 week or so the freelancer is no longer is tasked with us and decides to free up his drive and delete this folder.
I want to mitigate this problem. There are various ways to approach this:
1. Ask the freelancer to transfer ownership and then keep the folder safe.
2.  Download and backup the shared folder whenever you receive a submission.
3. Automate the backing up process as secondary storage.
Of course, ‘automation’ wins, as not only it removes the hassle from both the concerned parties but you have a secondary backup for long-term retrieval and safekeeping as well.

Let’s get to it.

First, set up a VM on Google Cloud, you can use AWS or any other service. I used GC because they have f1.micro(0.6GB Memory, 1 shared vCPU) always free. Not using Google’s Storage because they haven’t added GUI to it, yet.

In the GCP Console, go to the VM Instances page. Launch Instance.
Follow this quickstart guide for starting the VM. https://cloud.google.com/compute/docs/quickstart-linux




Gitlab Runner config to auto deploy your static site on S3

If you didn’t know, you can easily setup your static website and host on S3. Just follow these simple instructions and you are done.
https://docs.aws.amazon.com/AmazonS3/latest/dev/website-hosting-custom-domain-walkthrough.html

Now you might want to version control your hosted website and would want to avoid uploading this content again and again.
With the help of Gitlab Runner you can simply remove the hassle of uploading your updated code.




Setting up Swap Memory in linux server

Clickbait version: Increase your server’s system memory for free! 😀

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space.[src]

Note: Not recommended to be used in production environment. Swap space is very slow compared to physical memory and can seriously affect your application performance.

It’s fairly common to run into “System.OutOfMemoryException” if you are running a memory heavy application or multiple applications on a remote server.  To mitigate this issue you can simply create a Swap space for your server and use your disk space as system memory, this will be a little slower but you wouldn’t have to upgrade your server to higher memory one if you just need some MBs of extra ram or maybe need to perform a single task which needs some extra memory.

To enable 2GB of swap space use the following commands

SWAPFILE=/var/swapfile
SWAP_MEGABYTES=2048

sudo /bin/dd if=/dev/zero of=$SWAPFILE bs=1M count=$SWAP_MEGABYTES
sudo /bin/chmod 600 $SWAPFILE
sudo /sbin/mkswap $SWAPFILE
sudo /sbin/swapon $SWAPFILE



Let’s Encrypt (free SSL) on WordPress running on Debian 8

Let’s Encrypt is an amazing service which allows you to add SSL certificates for free on your website. But the process isn’t straightforward if you are running Debian 8. No worries, you can always use the Let’s Encrypt git repo to install the certificates.

Sidenote: You can use this method to install Let’s Encrypt SSL on any OS and for any application.




S3 as outbound mail content storage for Ruby On Rails

“Information is the oil of the 21st century, and analytics is the combustion engine.”

Since Amazon SES and some other email service providers don’t store show you the email content and history, you are left hanging if you want to some historical email performance and analytics. Keep track of outbound emails for Logging, Analytics and Audit purposes by following this simple setup.




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.




There are 10 types of people in the world: those who understand binary, and those who don’t.

Unknown



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.