Skip to content

Instantly share code, notes, and snippets.

View AnkurVyas-BTC's full-sized avatar
🎯
Karma rules world... You get whatever you give!

Ankur Vyas AnkurVyas-BTC

🎯
Karma rules world... You get whatever you give!
View GitHub Profile
@AnkurVyas-BTC
AnkurVyas-BTC / mock_delivery.rb
Created September 12, 2023 09:23
Mocking tracking requests
require 'net/http'
require 'uri'
require 'benchmark'
require 'async'
# Function to make a network request
def tracking_request(tracking_id)
uri = URI.parse("https://jsonplaceholder.typicode.com/posts/#{tracking_id}")
response = Net::HTTP.get_response(uri)
response.body if response.is_a?(Net::HTTPSuccess)
require 'rails_helper'
RSpec.describe TodosController, :type => :controller do
describe "GET #index" do
#describe "POST #create" do
#describe "GET #show" do
#describe "PATCH #update" do (or PUT #update)
#describe "DELETE #destroy" do
#describe "GET #new" do
@AnkurVyas-BTC
AnkurVyas-BTC / rspec_model_testing_template.rb
Created September 15, 2021 17:00 — forked from SabretWoW/rspec_model_testing_template.rb
Rails Rspec model testing skeleton & cheat sheet using rspec-rails, shoulda-matchers, shoulda-callbacks, and factory_girl_rails. Pretty much a brain dump of examples of what you can (should?) test in a model. Pick & choose what you like, and please let me know if there are any errors or new/changed features out there. Reddit comment thread: http…
# This is a skeleton for testing models including examples of validations, callbacks,
# scopes, instance & class methods, associations, and more.
# Pick and choose what you want, as all models don't NEED to be tested at this depth.
#
# I'm always eager to hear new tips & suggestions as I'm still new to testing,
# so if you have any, please share!
#
# @kyletcarlson
#
# This skeleton also assumes you're using the following gems:
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")
const { environment } = require('@rails/webpacker')
const webpack = require('webpack')
environment.plugins.prepend('Provide',
new webpack.ProvidePlugin({
$: 'jquery/src/jquery',
jQuery: 'jquery/src/jquery'
})
)
@AnkurVyas-BTC
AnkurVyas-BTC / candies_children.rb
Created December 27, 2019 06:08
Distribute N candies among K children
candies = 10
no_of_children = 3
children = Array.new(no_of_children, 0) # [0,0,0, ...n]
main_index = 0
while(candies > 0)
if candies > main_index
children[main_index % no_of_children] += main_index + 1
candies = candies - (main_index + 1)
else
@AnkurVyas-BTC
AnkurVyas-BTC / product_index.html.erb
Created August 29, 2019 14:30
paste below content at app/views/spree/admin/products/index.html.erb
# paste below content at app/views/spree/admin/products/index.html.erb
<% content_for :page_title do %>
<%= plural_resource_name(Spree::Product) %>
<% end %>
<% content_for :page_actions do %>
<%= button_link_to Spree.t(:new_product), new_object_url, { class: "btn-success", icon: 'add', id: 'admin_new_product' } %>
<% end if can?(:create, Spree::Product) %>
import React, { Component } from 'react';
class Calculator extends Component {
sleep = (milliseconds) => {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds) {
break;
}
@AnkurVyas-BTC
AnkurVyas-BTC / react_loadable_cool_options.js
Created April 28, 2019 05:04
React Loadable cool options!
function Loading(props) {
if (props.error) {
return <div>Something went wrong! <button onClick={ props.retry }>Retry</button></div>;
} else if (props.timedOut) {
return <div>Seems like your net is slow... <button onClick={ props.retry }>Retry</button></div>;
} else if (props.pastDelay) {
return <div>Loading...</div>;
} else {
return null;
}
@AnkurVyas-BTC
AnkurVyas-BTC / react_loadable_big_component.js
Last active April 28, 2019 04:49
React Loadble big component!
import Loadable from 'react-loadable';
function Loading() {
return <div>Loading...</div>;
}
const LoadableBigComponent = Loadable({
loader: () => import('../components/BigComponent'),
loading: Loading
});