Skip to content

Instantly share code, notes, and snippets.

View thisismydesign's full-sized avatar

Csaba Apagyi thisismydesign

View GitHub Profile
@thisismydesign
thisismydesign / xyz_service_spec.rb
Created September 2, 2021 22:08
Ruby: Use factories for your webmocks /3
allow(XyzService).to receive(:get_entities).and_return(
build(:xyz_service_get_entities_response, body: { data: ... })
)
@thisismydesign
thisismydesign / xyz_service_spec.rb
Last active September 2, 2021 22:05
Ruby: Use factories for your webmocks /2
stub_request(:get, %r{#{ENV['XYZ_SERVICE_SERVER']}/api/v2/get_entities*})
.to_return(build(:xyz_service_get_entities_response))
allow(XyzService).to receive(:get_entities).and_return(build(:xyz_service_get_entities_response))
@thisismydesign
thisismydesign / xyz_service_get_entities_responses.rb
Last active September 2, 2021 21:44
Ruby: Use factories for your webmocks /1
FactoryBot.define do
factory :xyz_service_get_entities_response, class: Hash do
skip_create
initialize_with { { body: attributes[:body].to_json }.stringify_keys }
body do
{
data: [
{ key: SecureRandom.hex, name: Faker::Lorem.word },
{ key: SecureRandom.hex, name: Faker::Lorem.word }
@thisismydesign
thisismydesign / cognito-oauth.controller.ts
Last active June 18, 2021 11:30
Cognito via OAuth2 in NestJS: keep user data in your app, let Cognito handle passwords and social login /1
import { Controller, Get, Req, Res, UseGuards } from '@nestjs/common';
import { Response, Request } from 'express';
import { CognitoOauthGuard } from './cognito-oauth.guard';
@Controller('auth/cognito')
export class CognitoOauthController {
constructor(private jwtAuthService: JwtAuthService) {}
@Get()
@UseGuards(CognitoOauthGuard)
@thisismydesign
thisismydesign / README.md
Created December 11, 2019 13:20
Ruby docker image and bundler version

Ruby's docker images (e.g. ruby:2.6.3) use bundler v1 (e.g. 1.17.3), see: docker-library/ruby#268 (comment)

When bundling with bundler v2 locally it can lead to the the following exception:

Could not find 'bundler' (2.0.2) required by your /app/Gemfile.lock. (Gem::GemNotFoundException)

Solve this by using the correct bundle version locally:

gem install bundler -v 1.17.3
@thisismydesign
thisismydesign / main.dart
Created November 24, 2019 00:39
Dart TIL
void main() {
// Typed variables raise type errors
var typed1 = 1;
// typed1 = '2';
int typed2;
// typed2 = '2';
// Dynamic types don't raise type errors
var a;
a = 1;
@thisismydesign
thisismydesign / ruby_is_very_easy.rb
Created June 28, 2019 12:11
Debugging journal #1 - This will obviously not work. Right?.. /2
h.select(&:matcher)
# => {:a=>"this"}
# Sorry what?
@thisismydesign
thisismydesign / ruby_is_easy.rb
Last active June 28, 2019 12:10
Debugging journal #1 - This will obviously not work. Right?.. /1
# irb / rails c / whatever
h = { a: "this", b: "that" }
h.select{ |k, v| v.match?("this") }
# => {:a=>"this"}
def matcher(string)
string.match?("this")
end
@thisismydesign
thisismydesign / association_loading.rb
Last active June 12, 2019 12:12
Load first records of ordered association for a collection without N+1 queries
# https://github.com/rails/rails/issues/6769
# https://github.com/rails/rails/issues/10621
# https://stackoverflow.com/questions/19353507/eager-loading-the-first-record-of-an-association
# https://stackoverflow.com/questions/29142478/eager-load-only-first-associations-with-activerecord
# https://stackoverflow.com/questions/46415817/eager-loading-not-working-with-order-clause-in-rails
# https://stackoverflow.com/questions/52889750/eager-loading-with-scope-in-rails
# https://stackoverflow.com/questions/30056163/eager-loading-in-deep-level-nested-association
# https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Eager+loading+of+associations
# https://stackoverflow.com/questions/29804377/rails-4-eager-load-limit-subquery
# https://stackoverflow.com/questions/6477614/how-do-you-do-eager-loading-with-limits
@thisismydesign
thisismydesign / settings.js
Last active May 4, 2019 13:17
VSCode config
{
// [OPTIONAL]
// https://code.visualstudio.com/docs/editor/integrated-terminal#_changing-how-the-terminal-is-rendered
// "terminal.integrated.rendererType": "dom",
// options for JS code formatting with Prettier
"editor.formatOnSave": false,
"eslint.autoFixOnSave": true,