Skip to content

Instantly share code, notes, and snippets.

Avatar
🕶️
Working on @slab, @sturama & @gdg-x

Sheharyar Naseer sheharyarn

🕶️
Working on @slab, @sturama & @gdg-x
View GitHub Profile
@sheharyarn
sheharyarn / RVEmptyObserver.java
Created May 13, 2017
Set Empty Layout for RecyclerViews in Android
View RVEmptyObserver.java
/**
* Custom implementation of AdapterDataObserver to show empty layouts
* for RecyclerView when there's no data
*
* Usage:
*
* adapter.registerAdapterDataObserver(new RVEmptyObserver(recyclerView, emptyView));
*/
public class RVEmptyObserver extends RecyclerView.AdapterDataObserver {
private View emptyView;
@sheharyarn
sheharyarn / api_controller.rb
Last active Jan 18, 2021
API Authentication with Devise in Rails
View api_controller.rb
class API::BaseController < ApplicationController
def index
render json: { active: true }
end
def authenticate
if user = User.authenticate(request.headers['X-AUTH-TOKEN'])
sign_in(user, store: false)
@sheharyarn
sheharyarn / serialize_query.js
Created Mar 5, 2017
Serialize an Object into URI Query in Javascript
View serialize_query.js
/**
* This method converts a Javascript object into
* a URI query string. Also handles nested arrays
* and objects (in Rails / PHP syntax)
*
* @author Sheharyar Naseer (@sheharyarn)
* @license MIT
*
* @example
*
@sheharyarn
sheharyarn / create_image_attachment.rb
Last active Jan 18, 2021
Polymorphic Paperclip class with unique partial ActiveRecord index on :default_image
View create_image_attachment.rb
# db/migrations/xxxxxxxxxxxxxx_create_image_attachment.rb
class CreateImageAttachments < ActiveRecord::Migration[5.0]
def change
create_table :image_attachments do |t|
t.belongs_to :imageable, polymorphic: true
t.attachment :data
t.boolean :default, default: false
t.timestamps
end
View request.js
/**
* Axios Request Wrapper
* ---------------------
*
* @author Sheharyar Naseer (@sheharyarn)
* @license MIT
*
*/
import axios from 'axios'
@sheharyarn
sheharyarn / deploy.sh
Last active Feb 20, 2018
Static Website Bash Deployment Script
View deploy.sh
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
#set -o xtrace
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
@sheharyarn
sheharyarn / bash-boilerplate.sh
Created Oct 29, 2016
Bash Script Boilerplate code
View bash-boilerplate.sh
#!/usr/bin/env bash
# Bash3 Boilerplate. Copyright (c) 2014, kvz.io
set -o errexit
set -o pipefail
set -o nounset
# set -o xtrace
# Set magic variables for current file & dir
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@sheharyarn
sheharyarn / validate_credit_card.js
Created Sep 20, 2016 — forked from DiegoSalazar/validate_credit_card.js
Luhn algorithm in Javascript. Check valid credit card numbers
View validate_credit_card.js
// takes the form field value and returns true on valid number
function valid_credit_card(value) {
// accept only digits, dashes or spaces
if (/[^0-9-\s]+/.test(value)) return false;
// The Luhn Algorithm. It's so pretty.
var nCheck = 0, nDigit = 0, bEven = false;
value = value.replace(/\D/g, "");
for (var n = value.length - 1; n >= 0; n--) {
@sheharyarn
sheharyarn / RVFragment.java
Last active Dec 18, 2019
A simple Fragment with RecyclerView
View RVFragment.java
/**
* RVFragment - Fragment with a simple RecyclerView that
* only takes Strings
*
* Usage:
* RVFragment rvf = new RVFragment();
*
* @author Sheharyar Naseer
*/
@sheharyarn
sheharyarn / SimpleRVAdapter.java
Last active Feb 9, 2021
Simple Recycler View adapter (without XML layout)
View SimpleRVAdapter.java
/**
* SimpleRVAdapter to quickly get started with simple Lists in Recyclerview
*
* Usage:
*
* RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
* rv.setLayoutManager(new LinearLayoutManager(getContext()));
* rv.setAdapter(new SimpleRVAdapter(new String[] {"1", "2", "3", "4", "5", "6", "7"}));
*
* @author Sheharyar Naseer