Lux.Lens behaviour (Lux v0.5.0)
View SourceLenses are used to load data from a source and return it to the calling agent.
Example
defmodule MyApp.Lenses.WeatherLens do
use Lux.Lens,
name: "OpenWeather API",
description: "Fetches weather data from OpenWeather",
url: "https://api.openweathermap.org/data/2.5/weather",
method: :get,
schema: %{
type: :object,
properties: %{
q: %{
type: :string,
description: "City name"
},
units: %{
type: :string,
description: "Temperature units. For temperature in Fahrenheit use units=imperial and for temperature in Celsius use units=metric",
enum: ["metric", "imperial"]
},
appid: %{type: :string, description: "API key"}
},
required: ["q", "appid"]
}
# Optional: Define a custom after_focus function
def after_focus(%{"main" => %{"temp" => temp}} = body) do
{:ok, %{temperature: temp, raw_data: body}}
end
def after_focus(%{"error" => error}) do
{:error, error}
end
end
Summary
Types
@type nullable(t) :: t | nil
An optional type of a type t
is a type that can be either the type t
or nil
.