Skip to content

Allow optional attributes in relation schemas #46

Open
@pmackay

Description

@pmackay

REST APIs come in many flavours, some not always purely RESTful. It would be helpful if schemas for rom-http relations could support optional attributes. This is for a scenario where an API provides a limited set of attributes from a search or collection API endpoint. Then the full details of the resource must be retrieved from a REST endpoint that represents that resource.

The specific example that triggered this is using rom-http with the Spoonacular API. This has a "Search Recipes" endpoint that returns recipes with about 8 attributes, then the "Get Recipe Information" endpoint returns a full recipe object with much more data.

Examples

This might look like:

module Spoonacular
  class Recipes < ROM::Relation[:http]
    schema(:recipes) do
      attribute :id, Types::Integer.meta(primary_key: true)
      attribute :title, Types::String
      attribute :image, Types::String

      attribute :readyInMinutes, Types::Integer.optional
      attribute :servings, Types::Integer.optional
      attribute :sourceUrl, Types::String.optional
      attribute :extendedIngredients, Types::Array.optional
    end

    struct_namespace Spoonacular
    auto_struct true

    ....
  end
end

NOTE: Some attributes are camelCase because currently I'm not sure of the best approach to map them to ruby snake case. If there is a known pattern for this, tips welcome :)

Resources

First queried here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions