Skip to content

Instantly share code, notes, and snippets.

View sulmanweb's full-sized avatar

Sulman Baig sulmanweb

View GitHub Profile
@sulmanweb
sulmanweb / aws_uploader.rb
Created September 15, 2019 20:20
AwsUploader class to directly upload url or file to s3. It Requires HTTParty and aws_sdk gems to be installed.
require 'aws-sdk'
require 'HTTParty'
## This class uploads a file to s3 with public-read access and return the public url
class AwsUploader
# s3 obj after initializing the aws sdk
attr_reader :s3
# initialization requires aws key id, secret, s3 region and s3 bucket data all are env variables
@sulmanweb
sulmanweb / RubygemsScrapper.rb
Last active September 14, 2019 05:13
Web Scraping Script in Ruby
require 'HTTParty'
require 'Nokogiri'
class RubygemsScrapper
attr_accessor :parse_page
# initialize repo for ruby gems requires query string
def initialize(q)
doc = HTTParty.get("https://rubygems.org/search?query=#{q}")
@parse_page ||= Nokogiri::HTML(doc)
@sulmanweb
sulmanweb / settings.json
Created March 18, 2019 13:04
VS Code Ruby Settings
{
"rubocop": {
"lint": true, //enable all lint cops.
"only": [/* array: Run only the specified cop(s) and/or cops in the specified departments. */],
"except": [/* array: Run all cops enabled by configuration except the specified cop(s) and/or departments. */],
"forceExclusion": true, //Add --force-exclusion option
"require": [/* array: Require Ruby files. */],
"rails": true //Run extra rails cops
},
"ruby.lint": {
@sulmanweb
sulmanweb / find_fixed_point_spec.rb
Created May 20, 2018 05:55
Find fixed point algorithm spec file
RSpec.describe BinarySearch, "#find_fixed_point" do
context "should have a sorted array" do
it "searches for the fixed point" do
binary_search = BinarySearch.new
expect(binary_search.find_fixed_point([0, 2, 3, 4, 5, 6, 7, 8])).to eql 0
expect(binary_search.find_fixed_point([0, 1, 2, 4, 5, 6, 7, 8])).to eql 2
end
it "returns -1 if not found" do
binary_search = BinarySearch.new
expect(binary_search.find_fixed_point([1, 2, 3, 4, 5, 6, 7, 8])).to eql -1
@sulmanweb
sulmanweb / find_fixed_point.rb
Created May 20, 2018 05:52
Find Fixed Point in Sorted Array Algorithm
def find_fixed_point(array)
n = array.length
high = n - 1
low = 0
while low <= high
mid = low + (high - low / 2)
if array[mid] == mid
return mid
@sulmanweb
sulmanweb / binary_search_spec.rb
Created May 19, 2018 21:00
RSpec file for binary search
require 'binary_search'
RSpec.describe BinarySearch, "#sort_func" do
context "with array searches the required" do
it "searches!" do
binary_search = BinarySearch.new
expect(binary_search.search_func([1, 2, 3, 4, 5, 6, 7, 8], 5)).to eql 4
end
it "return string if not found" do
binary_search = BinarySearch.new
@sulmanweb
sulmanweb / binary_search.rb
Created May 19, 2018 20:47
Binary Search
# Binary Search Algorithm
# Time Complexity = o(log n + 1)
# Precondition: List should be sorted
class BinarySearch
def search_func (array, to_search)
low = 0
high = array.length - 1
while low <= high
@sulmanweb
sulmanweb / _document.json.jbuilder
Created May 13, 2018 08:04
Active Storage as Attachment in Rails API with base64 decoding
json.extract! document, :id, :documentable_type, :documentable_id, :created_at
json.url rails_blob_url(document.doc)
@sulmanweb
sulmanweb / terminal.bash
Created January 24, 2018 05:46
Gems to install in each rbenv
gem install rubocop
gem install htmlbeautifier