Skip to content

Instantly share code, notes, and snippets.

View chiragtoor's full-sized avatar

Chirag Singh Toor chiragtoor

View GitHub Profile
@chiragtoor
chiragtoor / text_example.ex
Last active January 9, 2020 21:37
Example Elixir code for sending a text message through ExTwilio
def send_text_message(phone_number, message) do
ExTwilio.Api.create(ExTwilio.Message,
[to: phone_number,
from: Application.get_env(:ex_twilio, :send_number),
body: message])
end
def send_text_message(
%Plug.Conn{adapter: {Plug.Adapters.Test.Conn, _}}, _, _), do: nil
def send_text_message(_, phone_number, message) do
ExTwilio.Api.create(ExTwilio.Message,
[to: phone_number,
from: Application.get_env(:ex_twilio, :send_number),
body: message])
end
defmodule Project.Twilio do
def send_text_message(phone_number, message) do
ExTwilio.Api.create(ExTwilio.Message,
[to: phone_number,
from: Application.get_env(:ex_twilio, :send_number),
body: message])
end
end
defmodule Project.FakeTwilio do
def send_text_message(phone_number, message), do: nil
end
# in config.exs
config :project, text_service: Project.Twilio
# in test.exs
config :project, text_service: Project.FakeTwilio
def some_method do
# some code here
text_module.send_text_message(phone_number, message_body)
end
defp text_module, do: Application.get_env(:my_app, :text_service)
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry":{
"type":"Polygon",
"coordinates":[
[[ -118.47000042983923, 34.027237322664057 ],
[ -118.46997469284874, 34.027150122627894 ],
@block_file "data/sm_blocks.geojson"
@zone_file "data/sm_zones.geojson"
defp get_commercial_zones do
%{"features" => zones} = Poison.decode!(File.read!(@zone_file))
zones
end
defp get_blocks do
%{"features" => blocks} = Poison.decode!(File.read!(@block_file))
commercial_zones = get_commercial_zones()
|> Enum.map(fn(%{"geometry" => %{"coordinates" => [coordinates]}}) ->
coordinates
end)
commercial_blocks = get_blocks()
|> Enum.map(fn(geoJson = %{"geometry" => %{"coordinates" => [coordinates]}}) ->
# we need the coordinates to check for intersections and the
# original geoJson because if this block is in a commercial
# zone we will want to output it to a new file
from shapely.geometry import Polygon, Point
def blockInZone(block, zone):
boundaryPoints = []
for pair in block:
# reverse the order because from the file
# coordinates data was read in lat, long
# and for Shapely we need to put the data
# as long, lat
boundaryPoints.append((pair[1], pair[0]))