Skip to content

Instantly share code, notes, and snippets.

@dwilkie
dwilkie / unicef_metrics.rb
Last active November 2, 2021 08:34
UNICEF Metrics
start_time = Time.new(2020, 10, 1).beginning_of_day
end_time = Time.new(2020, 12, 31).end_of_day
completed_calls = PhoneCall.completed.where(created_at: start_time..end_time).joins(:call_data_record)
number_of_unpaid_users = User.count # SCFM only for now
number_of_calls = completed_calls.count
number_of_minutes = completed_calls.sum("call_data_records.bill_sec") / 60
number_of_users = (completed_calls.pluck(:to) + completed_calls.pluck(:from)).uniq.count
@dwilkie
dwilkie / scfm_paginate_contacts_example.rb
Created February 21, 2020 01:48
SCFM Paginate Contacts Example
require "net/http"
require "uri"
require "pry"
require "link-header-parser" # https://github.com/jgarber623/link-header-parser-ruby
require "time"
SOURCE_HOST = "https://dashboard.ews1294.info"
SOURCE_API_KEY = "[your API key]"
DESTINATION_HOST = "https://scfm.somleng.org"
@dwilkie
dwilkie / modify_contacts_metadata.rb
Created November 28, 2018 09:15
SCFM Modify Contacts
require "csv"
require "httparty"
DATA_FILE = "tmp/phone_test.csv".freeze
API_ENDPOINT = "https://scfm.somleng.org/api/contact_data".freeze
API_KEY = "REPLACE_ME".freeze
def update_contact!(row)
response = HTTParty.post(
API_ENDPOINT,
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="Polly.Ricardo" language="pt-BR">Hello World</Say>
<Say voice="Polly.Vitoria" language="pt-BR">Hello World</Say>
</Response>
@dwilkie
dwilkie / import_avf.rb
Created November 10, 2018 03:45
SCFM contact import (Africa's Voices)
require "csv"
require "httparty"
DATA_FILE = "tmp/maap_numbers.csv".freeze
API_ENDPOINT = "https://scfm.somleng.org/api/contact_data".freeze
API_KEY = "REPLACE_ME_WITH_SCFM_API_KEY".freeze
def create_contact!(row)
response = HTTParty.post(
API_ENDPOINT,
p SensorEvent.where("payload->>'created_at' = '0000-00-00 00:00:00 UTC+7'").delete_all
p SensorEvent.where("payload->'created_at' is not null").update_all("created_at = (payload->>'created_at')::timestamp without time zone")
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Play>https://s3.ap-southeast-1.amazonaws.com/audio.ews1294.info/9a7fee16-9d09-48fd-a25e-d0ade953d947-SampleAudio.mp3</Play>
</Response>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather action='https://requestb.in/15x8rjs1' timeout='60'>
<Say>Please press 1 or say sales for sales.</Say>
</Gather>
</Response>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Please record your message after the tone</Say>
<Record action="https://requestb.in/1m351lo1" recordingStatusCallback="https://requestb.in/ysgqk6ys"/>
</Response>
@dwilkie
dwilkie / somleng_io_example.rb
Last active July 25, 2017 08:12
somleng_example.rb
#!/usr/bin/env ruby
RESULTS_FILE = "test_mt_results.json"
ACCOUNT_SID = "REPLACE_ME"
AUTH_TOKEN = "REPLACE ME"
CALLER_ID = "345"
require 'httparty'
results = {}