Skip to content

Instantly share code, notes, and snippets.

View bluzky's full-sized avatar

Dung Nguyen bluzky

View GitHub Profile
@bluzky
bluzky / base2.ex
Created September 14, 2021 05:13
Parse without using defparsec
defmodule Solid.Parser.Base do
defmacro __using__(opts) do
custom_tags = Keyword.get(opts, :custom_tags, [])
custom_tag_modules = Enum.filter(custom_tags, &is_tuple(&1))
custom_tag_names = custom_tags -- custom_tag_modules
quote location: :keep do
import NimbleParsec
alias Solid.Parser.{Literal, Variable, Argument}
@bluzky
bluzky / redis_lock.ex
Created September 24, 2021 11:58
Elixir Redis Lock
defmodule Toolkit.RedisLock do
@moduledoc """
Provide mechanism to lock and check locking of a given key.
When set lock on a key, must set expiration time to make sure no lock live forever
"""
@redix_conn :octosells_cache
@doc """
Lock given key for `lock_duration` in second. Default lock duration is 5 minute
This function make sure only one process can acquire lock. We use `INCR` command to guarantee this.
@bluzky
bluzky / challenge.md
Last active October 12, 2021 09:33
Coding challenge Onpoint

Viết 1 module với 1 public function parse để parse chuỗi thời gian về struct DateTime hỗ trợ một số selector cơ bản của strptime

Support format

format description value example
H 24 hour 00 - 23
I 12 hour 00 - 12
m minute 00 - 59
S second 00 - 59
d day 01 - 31
@bluzky
bluzky / image.ex
Created November 9, 2021 09:09
hash image file name arc uploader
defmodule HeraCore.Uploader.AttachmentImage do
use Arc.Definition.Versioning
use Arc.Definition.Storage
# use Arc.Actions.Store
use Arc.Actions.Delete
use Arc.Actions.Url
use Arc.Ecto.Definition
@bluzky
bluzky / cache.ex
Created November 12, 2021 13:39
Elixir redis cache
defmodule Toolkit.Cache do
@moduledoc """
Cache provide some helper method to do the cache thing
Backend could be any. Currently using Redis via :redix library
"""
# TODO: move this to config
@redix_conn :octosells_cache
# 512kb
@bluzky
bluzky / datetime_parser.ex
Created December 11, 2021 16:19
Elixir datetime parser with regex
defmodule DateTimeParser do
@mapping %{
"H" => "(?<hour>\\d{2})",
"I" => "(?<hour12>\\d{2})",
"M" => "(?<minute>\\d{2})",
"S" => "(?<second>\\d{2})",
"d" => "(?<day>\\d{2})",
"m" => "(?<month>\\d{2})",
"y" => "(?<year2>\\d{2})",
"Y" => "(?<year>-?\\d{4})",
@bluzky
bluzky / stream_download.ex
Created January 3, 2022 14:47
Stream download large file from URL in Elixir
defmodule Toolkit.DownloadHelper do
@moduledoc """
Stream download large file from url
"""
require Logger
def stream_download(url, save_path) do
Stream.resource(
fn -> begin_download(url) end,
&continue_download/1,
@bluzky
bluzky / request_helper.ex
Last active July 21, 2023 05:01
Elixir download/stream large file with hackney
defmodule RequestHelper do
@moduledoc """
Reference from https://gist.github.com/avdi/7990684
Stream download large file from url
"""
require Logger
@doc """
Get stream data from url
mode could be `:binary` or `:line`
@bluzky
bluzky / github_export.exs
Last active March 30, 2022 08:56
Export your github pull request with Elixir
Mix.install([
{:tesla, "~> 1.4.3"},
{:csv, "~> 2.4"},
{:jason, "~> 1.3"}
])
defmodule GithubPR do
require Logger
def exec(repo, token) do
@bluzky
bluzky / features.org
Created May 30, 2022 01:37
Web exercise

Bài tập extra improve khả năng thiết kế hệ thống

Xây dựng một website bán hàng cơ bản với các chức năng

Chức năng phần admin:

1. Quản lý người dùng

Người dùng yêu cầu bắt buộc email, password, các trường thông tin còn lại tùy define

  • Đăng nhập