Skip to content

Instantly share code, notes, and snippets.

View aashish's full-sized avatar

Aashish Kiran aashish

  • India
View GitHub Profile
@aashish
aashish / mars_rover_problem_with_flyweight_pattern.rb
Created August 20, 2020 13:23
Mars Rover Problem with Flyweight pattern
class Plateau
attr_accessor :max_x, :max_y, :direction
def initialize(x, y, direction = :n)
@max_x = x.to_i
@max_y = y.to_i
@direction = direction.downcase.to_sym
end
def land(position)
@aashish
aashish / mars_rover_problem_with_proxy_pattern.rb
Last active August 18, 2020 15:50
Mars Rover Problem with proxy pattern
class Plateau
attr_accessor :max_x, :max_y, :direction
def initialize(x, y, direction = :n)
@max_x = x.to_i
@max_y = y.to_i
@direction = direction.downcase.to_sym
end
end
@aashish
aashish / flattener.rb
Created May 13, 2019 15:24
Array of arrays will be flattened.
class Array
def flattener
arr = []
self.each do |x|
if x.is_a? Array
arr = arr + x.flattener
else
arr << x
end
@aashish
aashish / date_validation.rb
Last active October 25, 2018 12:52
Date validation
date = ARGV[0]
class Date
def self.valid?(date)
y, m, d = date.split '-'
date.match(/^\d{4}-\d{2}-\d{2}$/) && Date.valid_date?(y.to_i, m.to_i, d.to_i)
end
end
@aashish
aashish / show_char_boxes.rb
Last active June 6, 2018 08:50
Process char in pdf
require 'hexapdf'
class ShowTextProcessor < HexaPDF::Content::Processor
def initialize(page)
super()
@canvas = page.canvas(type: :overlay)
end
def show_text(str)
@aashish
aashish / insert_image_on_pdf.rb
Last active February 21, 2024 17:18
Insert image on a existing PDF having content with hexapdf gem
require 'hexapdf'
doc = HexaPDF::Document.open("/home/xxxx/Downloads/OoPdfFormExample.pdf")
page = doc.pages[0]
canvas = page.canvas(type: :overlay)
canvas.translate(0, 20) do
canvas.fill_color(0.3, 0.7, 0.7)
canvas.rectangle(50, 0, 80, 80, radius: 80)
@aashish
aashish / DB backup and store to S3.md
Last active May 9, 2018 19:13
Backup database and store to AWS S3 with fog-aws

Backup database and store to AWS S3 with fog-aws

Today I had a task to take backup of database and upload to S3. Taking the backup to local machine was straight forward job. Uploading to S3 with fog-aws gem was time consuming due to lack of documentation. May be this article may help you with Uploading file to S3 with fog. Since the application is already using fog-aws gem, I preferred to use the same gem over aws-sdk.

Prerequisites

Make sure Gemfile has

gem 'fog-aws'
@aashish
aashish / elasticsearch_neo4j.md
Last active January 31, 2018 15:47
ElasticSearch with Neo4j

ElasticSearch with Neo4j Example test

Configured ElasticSearch with Neo4j as per demo app Search results are not as expected. Search results of ElasticSearch are different with Neo4j.

Example:

After starting and configuring ElasticSearch and Neo4j

Add the following data to Neo4j DB at http://0.0.0.0:7474/browser/

@aashish
aashish / AndroidManifest.xml
Last active September 16, 2017 12:15 — forked from BrandonSmith/AndroidManifest.xml
Quick example of how to schedule a notification(with click listener) in the future using AlarmManager
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cards.notification">
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
@aashish
aashish / array-flatter.rb
Last active September 10, 2017 19:38
Flatten an array of arbitrarily nested arrays of integers into a flat array of integers in ruby
#!/usr/bin/env ruby
class Array
def flatter
if empty? # base case
self
else
tail = pop
if tail.kind_of? Array
flatter + tail.flatter