This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Atlas.Mapping.CSVUtil.csv_row_to_table_record("priv/repo/data/destinations.csv") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Atlas.Mapping.CSVUtil do | |
| @moduledoc """ | |
| Utility module to ingest `destination.csv` | |
| """ | |
| alias NimbleCSV.RFC4180, as: CSV | |
| alias Atlas.{Mapping, Mapping.Destination, Repo} | |
| def csv_row_to_table_record(file) do | |
| column_names = get_column_names(file) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| row["longitude"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defp create_or_skip(row) do | |
| case Repo.get_by(Destination, | |
| latitude: row["latitude"], | |
| longitude: row["longitude"] | |
| ) do | |
| nil -> | |
| Mapping.create_destination(%{ | |
| longitude: Decimal.new(row["longitude"]), | |
| latitude: Decimal.new(row["latitude"]), | |
| name: row["name"], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |> create_or_skip() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %{ | |
| "allows_dogs" => "true", | |
| "backpack_camp" => "true", | |
| "car_camp" => "false", | |
| "car_friendly" => "true", | |
| "description" => "Rumors of big fish, probably early in season. ~ 2.5 hr hike. Latest notes: 20190909", | |
| "dogs_off_leash" => "true", | |
| "fee" => "false", | |
| "greater_than_three_hours" => "false", | |
| "hike_in" => "true", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |> Map.new(fn {val, num} -> {column_names[num], val} end) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| {"-105.6929", 0}, | |
| {"39.8763", 1}, | |
| {"Heart Lake", 2}, | |
| {"Description goes here", 3}, | |
| {"true", 4}, | |
| {"true", 5}, | |
| {"true", 6}, | |
| {"false", 7}, | |
| {"false", 8}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Enum.map(fn row -> | |
| row | |
| |> Enum.with_index | |
| ... | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def csv_row_to_table_record(file) do | |
| column_names = get_column_names(file) | |
| file | |
| |> File.stream!() | |
| |> CSV.parse_stream(skip_headers: true) | |
| |> Enum.map(fn row -> | |
| row | |
| |> Enum.with_index() | |
| |> Map.new(fn {val, num} -> {column_names[num], val} end) |