Skip to content

Instantly share code, notes, and snippets.

View mkhuda's full-sized avatar
🎯
Focusing

Lek Huda mkhuda

🎯
Focusing
View GitHub Profile
@mkhuda
mkhuda / app.rb
Created October 12, 2017 20:17
APP Class for Simple API using Ruby and Sinatra
require "sinatra"
require_relative "my_db"
class App < Sinatra::Base
get '/' do
return "Hello from Sinatra.rb"
end
get '/show_komedian' do
content_type :json
@mkhuda
mkhuda / my_db.rb
Created October 12, 2017 20:07
MySQL Database Class for Ruby Using Mysql2
require "mysql2"
class MyDatabase
def initialize
@client = Mysql2::Client.new(
:host => "localhost",
:username => "root",
:password => "yourpassword",
:database => "sinatra"
@mkhuda
mkhuda / config.ru
Created October 12, 2017 20:02
config.ru Ruby Sinatra API
require './app'
run App
@mkhuda
mkhuda / Gemfile
Last active October 12, 2017 19:44
Gemfile for Sinatra and Mysql
source 'http://rubygems.org'
# main gems
gem 'sinatra'
gem 'mysql2'
@mkhuda
mkhuda / obat.rb
Created July 6, 2017 13:05
Obat Mujarab
case @obat
when "sering sakit"
"puasa sunnah"
when "wajah terlihat murung tidak bercahaya"
"shalat malam tahajud"
when "hati sempit"
"baca Al-Quran dan maknanya"
when "tidak bahagia"
"shalat diawal waktu"
@mkhuda
mkhuda / redis_hset.rb
Created June 3, 2017 01:33
Redis MULTI and HSET on Rails
module RedisProjectHelper
# set hash
def set_project!(sample_store_id, sample_store_name)
$redis.multi do
$redis.hset(self.redis_new_project_key(), 'sample_id', sample_store_id)
$redis.hset(self.redis_new_project_key(), 'sample_name', sample_store_name)
end
end
@mkhuda
mkhuda / includes_where_not_in.rb
Created June 3, 2017 01:29
Using INCLUDES and WHERE NOT IN on ActiveRecord
# select user where id not in and where translator category is
def search
User.includes(:levels, :translator_categories).where('levels.name = ? AND translator_categories.name = ?', 'translator', 'Umum').where('users.id NOT IN (?)', [1,5,3,2]).references(:levels, :translator_categories)
end
@mkhuda
mkhuda / array_of_hashes.rb
Last active June 3, 2017 01:25
Creating Array of Hashes with Ruby
def self.create_aoh
aoh_hash = { project: [] }
self.all.each do |order|
aoh_hash[:orders] << {
text: order.texts.pluck(:id),
text_count: order.texts.pluck(:words),
doc: order.documents.pluck(:id),
doc_count: order.documents.pluck(:words)
}
end
@mkhuda
mkhuda / sidekiq.service
Last active June 28, 2022 23:15
Sidekiq service auto start for Ubuntu 16.04 using Systemd
#
# Sidekiq auto start using systemd unit file for Ubuntu 16.04
#
# Put this in /lib/systemd/system (Ubuntu).
# Run:
# 1. systemctl enable sidekiq (to enable sidekiq service)
# 2. systemctl {start,stop,restart} sidekiq (to start sidekiq service)
#
# This file corresponds to a single Sidekiq process. Add multiple copies
# to run multiple processes (sidekiq-1, sidekiq-2, etc).
@mkhuda
mkhuda / sidekiq
Last active October 23, 2016 09:07
Sidekiq init.d configuration with .rbenv
#!/bin/sh
# File: /etc/init.d/sidekiq
# thanks to http://tsaith.github.io/install-sidekiq-and-redis-on-ubuntu.html
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5