Skip to content

Instantly share code, notes, and snippets.

View UsamaAshraf's full-sized avatar

Usama Ashraf UsamaAshraf

View GitHub Profile
public class MultiplyByTenBolt extends BaseRichBolt 
{
  OutputCollector collector;
public void execute(Tuple tuple) 
  {
  // Get 'even-digit' from the tuple.
  int evenDigit = tuple.getInt(0);
collector.emit(new Values(evenDigit * 10));
  }
public class EvenDigitBolt extends BaseRichBolt 
{
  // To output tuples from this bolt to the next bolt.
OutputCollector collector;
public void execute(Tuple tuple) 
  {
  // Get the 1st column 'random-digit' from the tuple
  int randomDigit = tuple.getInt(0);
if (randomDigit % 2 == 0) {
public class RandomDigitSpout extends BaseRichSpout 
{
// To output tuples from spout to the next stage bolt
  SpoutOutputCollector collector;
public void nextTuple() 
  {
  int randomDigit = ThreadLocalRandom.current().nextInt(0, 10);
// Emit the digit to the next stage bolt
  collector.emit(new Values(randomDigit));
// service-worker.js
// Path is relative to the origin.
const OFFLINE_PAGE_URL = 'offline/offline.html';
// We’ll add more URIs to this array later.
const ASSETS_TO_BE_CACHED = [OFFLINE_PAGE_URL];
self.addEventListener('install', event => {
 event.waitUntil(
  // The Cache API is domain specific and allows an app to create & name 
// service-worker.js
// Changing the cache version will cause existing cached resources to be
// deleted the next time the service worker is re-installed and re-activated.
const CACHE_VERSION = 1;
const CURRENT_CACHE = `your-app-name-cache-v-${CACHE_VERSION}`;
const OFFLINE_PAGE_URL = 'offline/offline.html';
const ASSETS_TO_BE_CACHED = ['offline/offline.css', 'offline/offline.jpg', OFFLINE_PAGE_URL];
self.addEventListener('install', event => {
@UsamaAshraf
UsamaAshraf / development.rb
Created May 4, 2018 04:20
Register a custom Rails middleware
# config/environments/development.rb
# …
# Add our own middleware before the ActionDispatch::Static
# middleware and pass it an array of service worker URIs as a
# parameter.
config.middleware.insert_before ActionDispatch::Static, ServiceWorkerManager, ['service-worker.js']
@UsamaAshraf
UsamaAshraf / service_worker_manager.rb
Last active May 5, 2018 15:41
A simple, custom Rails middleware class
# app/middleware/service_worker_manager.rb
class ServiceWorkerManager
# We’ll pass 'service_workers' when we register this middleware.
def initialize(app, service_workers)
@app = app
@service_workers = service_workers
end
def call(env)
@UsamaAshraf
UsamaAshraf / main.go
Last active April 27, 2018 09:59
RabbitMQ subscriber in Go
// main.go
package main
import (
"fmt"
"log"
"github.com/streadway/amqp"
)
@UsamaAshraf
UsamaAshraf / user_event_handler.py
Last active April 27, 2018 09:59
RabbitMQ publisher in Python
# services/user_event_handler.py
import pika
import json
def emit_user_profile_update(user_id, new_data):
# 'rabbitmq-server' is the network reference we have to the broker,
# thanks to Docker Compose.
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq-server'))
channel = connection.channel()
# main.py
from flask import Flask
from flask import request
from flask import jsonify
from services.user_event_handler import emit_user_profile_update
app = Flask(__name__)
@app.route('/users/<int:user_id>', methods=['POST'])