Skip to content

Instantly share code, notes, and snippets.

View elct9620's full-sized avatar
💦
Level up!!!

蒼時弦や elct9620

💦
Level up!!!
View GitHub Profile
@elct9620
elct9620 / README.md
Created February 11, 2024 14:35
Hugo to Embedding Example

Hugo to Embedding Example

Convert Hugo articles to Embendding for Cloudflare Vectorize Store

Add JSON output to Hugo

The single.json is a example to add JSON outptu for your theme.

Setup a new config ai.toml to extend output

@elct9620
elct9620 / README.md
Last active January 12, 2024 05:35
Example of Domain-Driven Design in Rails

Example of Domain-Driven Design in Rails

Object

Name Layer Type Description
app/models/visitor_pass.rb Domain Aggregate Pass-related domain
app/services/tracker_service.rb Domain Domain Service Logic between door and visitor interaction
app/controllers/passes_controller.rb Application Use Case The user flow of "pass" a door
@elct9620
elct9620 / app-tinygo.js
Last active November 12, 2023 18:42
Define JavaScript class inside Golang (WebAssembly)
import 'vendor/tinygo'
const go = new Go();
go.importObject.env['main.defineClass'] = function(namePtr, nameLen, cPtr, cGcPtr, pPtr/*, pGcPtr*/) {
const mem = new DataView(go._inst.exports.memory.buffer)
const decoder = new TextDecoder("utf-8");
const name = decoder.decode(new DataView(go._inst.exports.memory.buffer, namePtr, nameLen));
const constructorID = mem.getUint32(cPtr, true)
@elct9620
elct9620 / demo.rb
Created December 21, 2022 14:09
Use Ruby Enumerator to create infinite API loader
paginator = InfinitePaginator.new(
'https://jsonplaceholder.typicode.com/users/%<page>d'
)
pp paginator.take(12)
# =>
# [{"id"=>1, "name"=>"Leanne Graham"},
# {"id"=>2, "name"=>"Ervin Howell"},
# {"id"=>3, "name"=>"Clementine Bauch"},
# {"id"=>4, "name"=>"Patricia Lebsack"},
@elct9620
elct9620 / README.md
Last active October 20, 2022 09:55
The Data Context Interaction (DCI) in Ruby

The Data Context Interaction in Ruby

Concept

Under Domain-Driven Design, we are trying to "Model" the real world to the virtual world. However, it cannot clearly describe the context/interaction with the domain.

Data

The raw data is a "number" or "string" and cannot explain the meaning in specifying the domain. Therefore we have to create a "value object" to assign the domain meaning and use "entity" to compose mapping a real-world object.

@elct9620
elct9620 / bootstrap.rb
Created July 23, 2022 09:00
Ruby's IoC with dry-container and dry-auto_inject
require 'dry-contaienr'
require 'dry-auto_inject'
# Singleton style container
class Container
extend Dry::Container::Mixin
namespace :repositories do
register(:games) { GameRepository.new }
register(:players) { PlayerRepository.new }
@elct9620
elct9620 / settings.rb
Created March 12, 2020 05:15
Simple settings model for Rails
# frozen_string_literal: true
require "singleton"
class Settings
include Singleton
class << self
delegate_missing_to :instance
end
@elct9620
elct9620 / api_spec.rb
Created February 24, 2022 08:40
RSpec have_attributes example
# API
class FakeResponse < Struct.new(:status, :code)
def success?
code == 200
end
end
class FakeAPI
def call
FakeResponse.new('Success', 200)
@elct9620
elct9620 / middleware.rb
Created February 17, 2022 07:11
Use Ruby's SimpleDelegator to implement middleware
# frozen_string_literal: true
require 'delegate'
require 'json'
class Middleware < SimpleDelegator
end
class CacheMiddleware < Middleware
def initialize(object)
@elct9620
elct9620 / README.md
Last active January 28, 2022 17:48
CloudFlare Workers Status API

CloudFlare Workers as Status API

In most case, the CloudFlare SLA is greater than our server. So, we can use it as a simple uptime checker.

Usage

  1. Deploy index.js to your CloudFlare Workers, and bind KV which you save the status.
  2. Add updater.rb to your server, and setup cron job to run it
  3. Get node status on any static page with https://your-worker.workers.dev/?node[]=name to fetch node status