Skip to content

Instantly share code, notes, and snippets.

@InsilicoSoft
InsilicoSoft / stream-pdf-from-nodejs.js
Last active January 25, 2024 10:42
Stream local pdf file from nodejs (restify) server to client
server.get('/downloadPdf/:fileData', function (req, res) {
// config
var fileData = Buffer.from(req.params.fileData, 'base64');
var menuData = JSON.parse(fileData.toString());
var userName = menuData.userName;
var menuName = slug(menuData.menuName);
var fileName = userName + "-" + menuName + PDF_EXT;
var filePath = PDF_PATH + fileName;
// process headers
@InsilicoSoft
InsilicoSoft / social-auth-nodejs-couchdb-passportjs.js
Last active October 10, 2017 12:40
Social auth using nodejs & couchdb & restify & passportjs
function passportVerify(provider) {
return function(req, accessToken, refreshToken, profile, cb) {
users.view('xxx', 'by_auth_provider_id', { key: [ provider, profile.id ] }, function(err, socialUsers) {
if(!err) {
if (0 < socialUsers.rows.length) {
// found connected user
cb(null, socialUsers.rows[0].value);
} else {
@InsilicoSoft
InsilicoSoft / client-screen-service.ts
Created August 3, 2017 08:16
Allows to figure out the client screen size and use it within the logic.
import { Injectable } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { InputMask } from 'primeng/primeng';
@Injectable()
export class ClientScreenService {
inputMaskStream: Subscription;
isAndroid = typeof navigator !== 'undefined' && /Android/i.test(navigator.userAgent);
isDesktop: boolean = false;
@InsilicoSoft
InsilicoSoft / api-service.ts
Created August 3, 2017 08:12
Angular API Wrapper Service
import { Injectable, isDevMode } from '@angular/core';
import { ConnectionBackend, Http, URLSearchParams, Headers, RequestOptions,
RequestOptionsArgs, Response, RequestMethod } from '@angular/http';
import { ToastrService } from 'ngx-toastr';
import { Observable } from 'rxjs/Observable';
import * as _ from 'lodash';
import { StorageService } from './storage.service';
import { TokenService } from './token.service';
import { TranslateService } from '../translations/translate.service';
@InsilicoSoft
InsilicoSoft / payment_spec.rb
Created August 3, 2017 08:05
Specs for payment model
describe Payment do
let(:payment) { create(:payment) }
let(:payment_with_order) { create(:payment, :with_future_order) }
context 'state machine' do
it 'is in pending state on creation' do
expect(payment.pending?).to eq true
end
it 'transfers to completed state with #complete' do
@InsilicoSoft
InsilicoSoft / order.rb
Created August 3, 2017 08:03
The order model
# == Schema Information
#
# Table name: orders
#
# id :integer not null, primary key
# user_id :integer
# product_id :integer
# quantity :integer
# payment_kind :string
# aasm_state :string
@InsilicoSoft
InsilicoSoft / jwt_crypto.rb
Created August 3, 2017 07:55
Custom Jwt Token encoder/decoder that takes care of confirmations and resets too.
require 'base64'
##
# A class for encoding and decoding auth information in form of JWT token. Note that only sensitive auth information
# such as user id and/or email should be encrypted in token, not any possible api payload
# @example Encrypt user's id and email
# JwtCrypto.encode({ user: { id: 1, email: 'hello@hello.com' }}) # => returns JWT token
# @example Decode payload from provided JWT token
# JwtCrypto.decode('hhjkasdhkja.hdjsakdhak.hasjdkhasjkd') # => returns decoded payload or fails with DecodeError
class JwtCrypto