Conversation
This extends the sources/[programmatic content](https://perron.railsdesigner.com/docs/programmatic-content-creation/) feature First off lamda filtering for sources, like so: ```ruby class Content::Product sources, :countries, products: -> (products) { products.select(&:featured?) } end ``` Then pulling data from external APIs. This has been on my list almost since I first launched Perron. 😊 Initially I thought of adding first-party support for API handling. But instead I opted for a more maintainable and flexible option of simply support adding a class to the sources feature. This is as simple as it is elegant (at least I think it is). Syntax looks like this: ```ruby class Content::Product sources repos: { class: GitHubRepo, primary_key: :name } end ``` More complete example implementation that pulls GitHub repositories (using the [Active Resource gem](https://github.com/rails/activeresource): ```ruby class GitHubRepo < ActiveResource::Base self.site = "https://api.github.com/" def self.all find(:all, from: "/users/Rails-Designer/repos", params: { per_page: 5 }) end end Then the typical template setup: ```ruby class Content::Project < Perron::Resource sources repos: { class: GitHubRepo, primary_key: :name } def self.source_template(sources) <<~TEMPLATE --- title: #{sources.repos.name} description: #{sources.repos.description} language: #{sources.repos.language} stars: #{sources.repos.stargazers_count} --- #{sources.repos.description} TEMPLATE end end ``` Above would pull the repo data and create (and update if changes) erb files into `app/content/projects` just like it does with local files. ✨
I think this reads better instead of `sources.products`:
```
class Content::Product < Perron::Resource
sources :countries, :products
def self.source_template(source)
<<~TEMPLATE
---
product_code: #{source.products.code}
country_id: #{source.countries.id}
title: #{source.products.name} in #{source.countries.name}
slug: #{source.products.slug}-#{source.countries.code.downcase}
---
# …
end
```
Aliased to `sources` so you could still use if you like that syntax
better.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This extends the sources/programmatic content feature.
First off lamda filtering for sources, like so:
Then support for class for sources, for which pulling data from external APIs is the most obvious use case. This has been on my list since I first launched Perron. 😊
Initially I thought of adding first-party support for API handling. But instead I opted for a more maintainable and flexible option of simply support adding a class to the sources feature. This is as simple as it is elegant (at least I think it is).
Syntax looks like this:
A complete example implementation that pulls GitHub repositories data could look like this (using the Active Resource gem):
Then the typical template setup:
Above would pull the repo data and create (and update if changes) erb files into
app/content/projectsjust like it does with local files. ✨