Skip to content

Instantly share code, notes, and snippets.

View MeerKatDev's full-sized avatar
🏠
Working from home

MeerKatDev MeerKatDev

🏠
Working from home
View GitHub Profile
@MeerKatDev
MeerKatDev / httpc_start.ex
Created February 24, 2022 22:30
simple starting code for using :httpc
# first, add :inets to :extra_applications or some other similar method
defp simple_http_request(request, http_method) do
Application.ensure_all_started(:inets)
Application.ensure_all_started(:ssl)
ok_block = {'HTTP/1.1', 200, 'OK'}
options = [{:ssl, [{:verify, :verify_none}]}]
with {:ok, {^ok_block, _headers, json_text}} <-
:httpc.request(http_method, request, options, []),
@MeerKatDev
MeerKatDev / rich_model.ex
Last active February 6, 2022 06:19
An Elixir model for basic db functionality, to be brief.
defmodule RichModel do
@moduledoc """
A mini-package that injects basic db functionality in any model.
Good for rapid prototyping.
"""
defmacro __using__([]) do # alternatively, `defmacro __using__([repo: repo]) do ....`
quote do
alias __BASE_MODULE__.Repo # __BASE_MODULE__ is the app root name
@MeerKatDev
MeerKatDev / twilio_send_sms.ex
Created November 24, 2019 12:19
Elixir - send SMS with Twilio through Hackney
text = "sms text"
opts = [follow_redirect: true, basic_auth: {@account_sid, @auth_token}]
base_url = "https://api.twilio.com/2010-04-01/Accounts/#{@account_sid}/Messages.json"
payload = "Body=#{text}&From=#{@number_from}&To=#{number_to}"
headers = ["Content-Type": "application/x-www-form-urlencoded"]
case :hackney.request(:post, base_url, headers, payload, opts) do
{:ok, 201, _hs, _ref} ->
json(conn, %{message: "Message sent successfully!"})
{:error, reason} ->
send_resp(conn, 500, Jason.encode!(%{message: reason}))
@MeerKatDev
MeerKatDev / make_signature.ex
Last active November 16, 2019 17:34
Sign a request for Aliyun OSS (i.e. how to create the Authorization header) in Elixir
#first, we need to obtain a converter to GMT, needed for the Date header
# GMT format: "Sun, 22 Nov 2015 08:16:38 GMT"
defp utc_to_gmt(utc_datetime) do
# format = "{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT"
days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
day_of_week = Date.day_of_week(utc_datetime)
month = Enum.at(months,utc_datetime.month-1)
year = utc_datetime.year
time = to_string(Time.truncate(DateTime.to_time(utc_datetime),:second))
# app deps
sudo yum install git
# erlang deps
sudo yum groupinstall "Development Tools"
sudo yum install ncurses-devel openssl-devel
# erlang
wget http://www.erlang.org/download/otp_src_19.2.tar.gz
tar -zxvf otp_src_19.2.tar.gz