3.0 KiB · text History 44bc4f7
defmodule CartosnapApp.Photos.Facets do
  @moduledoc """
  The camera details photos can be filtered by: the camera and lens, and
  the aperture, focal length, ISO and shutter speed they were taken at.

  Each is a column on `photo_photos`, filled from the EXIF block when the
  photo is uploaded (`CartosnapApp.Photos.Images.shooting/1`). Filters
  travel in the URL under short names (`?camera=…&f=2.8`); `from_params/1`
  and `to_params/1` convert between those and `[{column, value}]`.
  """

  @facets [
    %{key: :camera, param: "camera", label: "Camera", type: :string},
    %{key: :lens, param: "lens", label: "Lens", type: :string},
    %{key: :f_number, param: "f", label: "Aperture", type: :float},
    %{key: :focal_length, param: "mm", label: "Focal length", type: :float},
    %{key: :iso, param: "iso", label: "ISO", type: :integer},
    %{key: :exposure, param: "shutter", label: "Shutter", type: :float}
  ]

  @keys Enum.map(@facets, & &1.key)

  @doc "Every facet, in the order they're shown."
  def all, do: @facets

  @doc "The facet columns."
  def keys, do: @keys

  @doc "One facet by its column."
  def get(key), do: Enum.find(@facets, &(&1.key == key))

  @doc """
  The filters in URL params, as `[{column, value}]` in facet order.
  Unknown params and values that don't parse are ignored.
  """
  def from_params(params) when is_map(params) do
    for %{key: key, param: param, type: type} <- @facets,
        raw = params[param],
        is_binary(raw),
        {:ok, value} <- [parse(type, raw)],
        do: {key, value}
  end

  @doc "Filters back as URL params."
  def to_params(filters) do
    for {key, value} <- filters, facet = get(key), into: %{} do
      {facet.param, param_value(value)}
    end
  end

  @doc "The URL param value for a facet value."
  def param_value(value) when is_float(value), do: short(value)
  def param_value(value), do: to_string(value)

  defp parse(:string, raw) do
    case String.trim(raw) do
      "" -> :error
      value -> {:ok, value}
    end
  end

  defp parse(:float, raw) do
    case Float.parse(raw) do
      {value, ""} when value > 0 -> {:ok, value}
      _ -> :error
    end
  end

  defp parse(:integer, raw) do
    case Integer.parse(raw) do
      {value, ""} when value > 0 -> {:ok, value}
      _ -> :error
    end
  end

  @doc """
  A facet value the way a photographer would write it: `f/2.8`, `50 mm`,
  `ISO 400`, `1/250 s`.
  """
  def format(:f_number, value), do: "f/#{short(value)}"
  def format(:focal_length, value), do: "#{short(value)} mm"
  def format(:iso, value), do: "ISO #{value}"

  def format(:exposure, value) when value < 0.5 do
    "1/#{round(1 / value)} s"
  end

  def format(:exposure, value), do: "#{short(value)} s"
  def format(_key, value), do: to_string(value)

  # 2.0 -> "2", 2.8 -> "2.8"
  defp short(value) when is_float(value) do
    if value == Float.round(value),
      do: value |> trunc() |> Integer.to_string(),
      else: :erlang.float_to_binary(value, [:short])
  end

  defp short(value), do: to_string(value)
end