9 Things to keep in mind for APIs development

1. Pagination: Don’t show all data to in one call, use pagination instead.
 
2. Versioning: Whenever there is a change in APIs instead of adding a change in currently stable APIs go for creating another version of APIs. Like if previously you have version 1 (v1) of APIs. Then now you should have version 2 (v2).
 
3. Authentication: Your APIs endpoints should be secure and should not be available for public access.
 
4. Rate Limiting & Throttling: Limit your APIs endpoints hits per user/token. So that no one can exploit your server resources.
 
5. HTTP Caching: Conditional GET:
7. Hypermedia:
 
8. Respond Handling: Different types of responses should be handled properly including success, error, validations, etc.
9. Documentation: is a necessary part for all stakeholders, who can get a quick understanding of using APIs documentation.
Posted in APIs, Best prectices, Ruby on Rails, Uncategorized

Doorkeeper (4.2.6) redirect URI does not match

 ERROR — omniauth: Authentication failure! invalid_credentials: OAuth2::Error, invalid_grant: The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.

add callback_url method in your oauth provider strategy.

module OmniAuth
  module Strategies
    class YourApplicationName < OmniAuth::Strategies::OAuth2       
         option :name, :application_name
         option :client_options, {         
              :authorize_path => "/oauth/authorize"
         }

      uid do
        raw_info["id"]
      end

      info do
        {
          :email => raw_info["email"],
          :first_name => raw_info["first_name"],
          :last_name => raw_info["last_name"]
        }
      end

      def raw_info
        @raw_info ||= access_token.get('/api/v1/me.json').parsed
      end

      def callback_url
        full_host + script_name + callback_path
      end
    end
  end
end

https://github.com/doorkeeper-gem/doorkeeper/wiki/Create-a-OmniAuth-strategy-for-your-provider

Reason of this error in doorkeeper 4.2.6

Solutions: Roll back to 1.3.1 or override the callback_url method in your strategy.

Posted in Uncategorized

Invalid mix of scope block and deprecated finder options on ActiveRecord association

While upgrading from rails 3.2.22.5 to rails 4.2.6

has_many :subitems, through: :child_associations, source: :child, source_type: 'ContentItem', conditions: 'content_items.deleted  1', order: 'content_associations.position ASC'
  1. Replaced conditions with proc
has_many :subitems, ->{where('content_items.deleted  1')}, through: :child_associations, source: :child, source_type: 'ContentItem',  order: 'content_associations.position ASC'

2. Place order in proc

has_many :subitems, ->{where('content_items.deleted  1').order('content_associations.position ASC') }, through: :child_associations, source: :child, source_type: 'ContentItem'
Posted in Uncategorized, upgrade from rails 3 to 4

start delayed job on reboot [sh script]

Short way:
Making script run at boot time at Debian

Manual Way:

cd /etc/init.d && sudo nano delayed_job_script

Paste following code to delayed_job_script:
Note: change RAILS_ROOT and ENV variables according to your setup.


#!/bin/sh
RAILS_ROOT="/home/deploy/apps/ProjectFolder/current"
ENV="production"

case "$1" in
    start)
    echo -n "Starting Delayed Jobs: "
    su - deploy -c "cd $RAILS_ROOT && RAILS_ENV=$ENV ruby script/delayed_job start -n 3"
    echo "done."
    ;;
    stop)
    echo -n "Stopping Delayed Jobs: "
    su - deploy -c "cd $RAILS_ROOT && RAILS_ENV=$ENV ruby script/delayed_job stop -n 3"
    echo "done."
    ;;
    *)
    echo "Usage: $N {start|stop}" >&2
    exit 1
    ;;
esac

exit 0

Make file executable:

sudo chmod +x /etc/init.d/delayed_job_script

Create links in Linux Runlevels for delayed_job_script:


sudo ln -s /etc/init.d/delayed_job_script /etc/rc0.d/K80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc1.d/K80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc1.d/etc/rc2.d/S80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc1.d/etc/rc3.d/S80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc1.d/etc/rc4.d/S80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc1.d/etc/rc5.d/S80delayed_job_script
sudo ln -s /etc/init.d/delayed_job_script /etc/rc6.d/K80delayed_job_script

Naming convention for link file names
In rc0.d, rc1.d and rc6.d link name starts from K which means kill command will execute. In other run levels link file name starts with S which means start process. After S/K there is number 80 which is just process priority number. After that you can write any name of your choice.

Intro to Run Levels

Tagged with: , ,
Posted in Ubuntu

Check if same key pressed twice in 1second


var twice = 0;
var previous_key=null;
$(document).on('keyup', function( e ){
    if(twice===1){
     if(e.which==previous_key)
      console.log(previous_key+'(you pressed twice!)'+e.which);
     else{
     	console.log(previous_key+'-----'+e.which);
    	twice = 0; 
     }
     
    }else{
    	previous_key=e.which;
    }
      
    twice = 1;
    setTimeout(function(){ // ...reset to 0 after 1s
      twice = 0; 
    }, 1000);
      
  
    
});
Posted in Javascript

Check and export Java path and version Ubuntu

Check java version:

java -version

Check path of installed java:

readlink -f $(which java

Note: remove /bin/java from end of path
In ~/.bashrc file add following command at the end:

export JAVA_HOME='/usr/lib/jvm/java-7-openjdk-amd64/jre/'

Now run:

source ~/.bashrc
Tagged with:
Posted in java

Elasticsearch service not starting Ubuntu

Cause: While trying to run elasticsearch in background my elasticsearch user and group was recreated and had a new user and group id

Solution:

 sudo chown -R elasticsearch:elasticsearch /var/lib/elasticsearch/

Now

 
sudo service elasticsearch start
sudo service elasticsearch status

referenced from github

Posted in Elasticsearch

Elasticsearch 1.2.1 ::Transport::Transport::Errors::BadRequest – [400]

This error occurred because index was corrupted or missing

Solution either re-index your documents or copy index from your backup.

In Ubuntu elastic-search data directory path is mentioned below:

/var/lib/elasticsearch/

inside this directory project wise data directories exists. Copy your data directory from here to take backup once in a day or week.

And to fix your index either copy your backup from one system to another.

OR

Use this link StackOverFlow: how to move elasticsearch data from one server to another

Posted in Elasticsearch

Heroku permission denied for database “postgres”

FATAL: permission denied for database “postgres” DETAIL: User does not have CONNECT privilege.

Run this command to solve this error:

heroku addons | grep POSTGRES
Tagged with: , ,
Posted in Heroku, Uncategorized

LaTeX | error algorithm.sty not found [Ubuntu 14.04]

If you use the algorithm package in your LaTeX document, as mentioned below:

\usepackage{algorithm} 

you might get this error on compiling it

LaTeX Error: File `algorithm.sty’ not found.
For Ubuntu, this LaTeX package is bundled in the texlive-science package. To install this package.

Run this command in terminal:

sudo apt-get install texlive-science
Tagged with: ,
Posted in Latex
StackOverFlow
Categories
Archives