Skip to content

Instantly share code, notes, and snippets.

View sheharyarn's full-sized avatar
🕶️
Working

Sheharyar Naseer sheharyarn

🕶️
Working
View GitHub Profile
@sheharyarn
sheharyarn / flatten.rb
Last active April 20, 2017 01:32
Ruby implementation of Array#flatten
class Array
## Returns a new flattened array
def custom_flatten
new_array = []
# Recursively iterate over sub-arrays and other
# elements and concat them to the new array
self.each do |obj|
if obj.is_a?(Array)
@sheharyarn
sheharyarn / SimpleRVAdapter.java
Last active February 9, 2021 10:11
Simple Recycler View adapter (without XML layout)
/**
* 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
@sheharyarn
sheharyarn / RVFragment.java
Last active December 18, 2019 06:39
A simple Fragment with RecyclerView
/**
* RVFragment - Fragment with a simple RecyclerView that
* only takes Strings
*
* Usage:
* RVFragment rvf = new RVFragment();
*
* @author Sheharyar Naseer
*/
@sheharyarn
sheharyarn / validate_credit_card.js
Created September 20, 2016 20:57 — forked from DiegoSalazar/validate_credit_card.js
Luhn algorithm in Javascript. Check valid credit card numbers
// 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 / bash-boilerplate.sh
Created October 29, 2016 21:51
Bash Script Boilerplate code
#!/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 / deploy.sh
Last active February 20, 2018 14:38
Static Website Bash Deployment Script
#!/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 / request.js
Last active August 24, 2023 14:55
Axios Request Wrapper for React (https://to.shyr.io/axios-requests)
/**
* Axios Request Wrapper
* ---------------------
*
* @author Sheharyar Naseer (@sheharyarn)
* @license MIT
*
*/
import axios from 'axios'
@sheharyarn
sheharyarn / create_image_attachment.rb
Last active January 18, 2021 15:13
Polymorphic Paperclip class with unique partial ActiveRecord index on :default_image
# 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
@sheharyarn
sheharyarn / serialize_query.js
Created March 5, 2017 05:20
Serialize an Object into URI Query in Javascript
/**
* 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 / api_controller.rb
Last active January 18, 2021 15:13
API Authentication with Devise in Rails
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)