Skip to content

Instantly share code, notes, and snippets.

View anaumov's full-sized avatar
🚀
Looking for an interesting project related to real life.

Alexey Naumov anaumov

🚀
Looking for an interesting project related to real life.
View GitHub Profile
@anaumov
anaumov / is_string.js
Created October 5, 2018 08:26
isString javascript
function isString(sample) {
return typeof(sample) === 'string' || sample instanceof String;
}
function isString(sample) {
return sample.constructor == String;
}
@anaumov
anaumov / strings_objects_and_literals.js
Created October 5, 2018 07:52
JavaScript strings objects and literals
var literalString = 'wazzup';
console.log(typeof literalString); // string
console.log(literalString instanceof String); // false
console.log(literalString.constructor === String); // true
var constructedString = new String('wazzup');
console.log(typeof constructedString); // Object
console.log(constructedString instanceof String); // true
console.log(constructedString.constructor === String); // true
@anaumov
anaumov / lock.rb
Created September 14, 2018 10:05
Транзакция с локом
vasia.with_lock do
petya.withdrawal(100)
vasya.deposit(100)
end
@anaumov
anaumov / deadlock.rb
Created September 14, 2018 09:34
Дедлок в rails
# две транзакции ниже выполняются через разные подключени к базе
ActiveRecord::Base.transaction do
petya.withdrawal(100)
vasya.deposit(100)
end
ActiveRecord::Base.transaction do
vasya.withdrawal(50)
petya.deposit(50)
@anaumov
anaumov / transaction.rb
Last active September 14, 2018 09:15
Транзакция в Rails
# Если в кошельке Пети нет денег, то Вася все равно полчит свои 100р
class Account < ApplicationRecord
validates :balance, numericality: { greater_than_or_equal_to: 0 }
end
petya.withdrawal(100)
vasya.deposit(100)
# Если в кошельке Пети нет денег, то Вася уже ничего не получит
@anaumov
anaumov / hash_to_html_form.rb
Created August 10, 2018 11:34
Convert ruby hash to html form
def hash_to_form(hash)
form = ''
form << "<form method='#{hash[:method]}' action='#{hash[:url]}'>\n"
hash[:inputs].each do |key, value|
form << " <input type='hidden' name='#{key}' value='#{value}' />\n"
end
form << " <input type='submit' value='Submit' />\n"
form << "</form>"
puts form
end
@anaumov
anaumov / rbk_money_sign_validation.rb
Created May 18, 2018 09:17
RBK money signature validation
signature = (/digest=(.+)/).match(headers['Content-Signature'])[1]
decoded_signature = Base64.urlsafe_decode64(signature)
content = request.body.string
pubilc_key = OpenSSL::PKey::RSA.new(YOUR_PUBLIC_KEY)
digest = OpenSSL::Digest::SHA256.new
pubilc_key.verify(digest, decoded_signature, content)
@anaumov
anaumov / grape_stub.rb
Created May 16, 2018 09:22
Stub current user Rspec Grape
describe SomeAPI, type: :request do
describe 'GET /api/some' do
context "user is signed in" do
before do
Grape::Endpoint.before_each do |endpoint|
endpoint.stub(:current_user).and_return(user)
end
end
after { Grape::Endpoint.before_each nil }
@anaumov
anaumov / rspec_matcher.rb
Created April 2, 2018 13:42
Rspec custom matcher
RSpec::Matchers.define :have_index_page_content do |expected|
match do |actual|
have_content('Welcome to Website')
end
end
@anaumov
anaumov / cert_convert.sh
Created May 29, 2017 09:52 — forked from jmervine/cert_convert.sh
openssl: convert cert from p7b to crt (or cer)
openssl pkcs7 -print_certs -in old.p7b -out new.crt
# openssl pkcs7 -print_certs -in old.p7b -out new.cer