Skip to content

Instantly share code, notes, and snippets.

@bih
Last active November 10, 2018 22:52
Show Gist options
  • Save bih/d43f8cd7f7a8b578ae7a77659709e4ab to your computer and use it in GitHub Desktop.
Save bih/d43f8cd7f7a8b578ae7a77659709e4ab to your computer and use it in GitHub Desktop.
Building API's with Jekyll

Building API's in Jekyll

This is part of a blog post series that I'm writing called Advanced Jekyll which you can find on my website, bilaw.al.

You can read the blog post Building API's in Jekyll by clicking here.

Installation

All the code in this gist is what I use to build API's with in Jekyll.

Step 1: Copy all of the files (except README.md, of course) into your existing Jekyll project (the first line of each file contains the directory the file should belong in, as well as the filename itself) Step 2: Alter to taste. The blog post goes into more detail on this. Step 3: Win!

If you haven't installed Jekyll before, do this:

$ gem install jekyll -v 3.4.0
$ jekyll new my-site
$ cd my-site

Help

Need help? Feel free to reach out by emailing hi@bilaw.al - I'd love to help!

module Jekyll
module Filters
module ApiFilter
# This will take multi-dimensional hash and \
# convert it into a flattened array of hashes.
def flatten_hash(input)
all_values = input.to_a.flatten
hash_values = all_values.select { |value| value.class == Hash }
most_nested_values = []
if hash_values.count > 0
hash_values.each do |hash_value|
most_nested_values << flatten_hash(hash_value)
end
most_nested_values.flatten
else
return input
end
end
# This will take an array of hashes and \
# only show columns that have been explicitly approved
def filter_fields(input, fields)
downcased_fields = fields
.split(",")
.map { |field| field.strip.downcase }
input.map do |entry|
entry.select do |key, value|
downcased_fields.include?(key.downcase)
end
end
end
# This will take an input and wrap a \
# hash around it with a designated key with \
# a timestamp
def wrap_with_key(input, key)
{
key => input,
:status => "OK",
:last_updated => Date.today
}
end
end
end
end
Liquid::Template.register_filter(Jekyll::Filters::ApiFilter)
layout
home

Random Monthly Meetup

Every month, we host a meetup talking about random stuff. We'll provide some snacks and other goodies. Just come by and say hi!

All of our meetups are below:

{% assign meetups = (site.data.meetups | flatten_hash) %} {% for meetup in meetups %}

{{ meetup.name }} {{ meetup.city }}

{{ meetup.description }}

When: {{ meetup.datetime | date_to_long_string }}

Location: {{ meetup.venue }}


{% endfor %}
name: "Random Meetup April 2017"
description: "Come join us for some snacks and chat about random stuff."
city: "London, UK"
datetime: 2017-04-04 20:00:00
venue: "TBD"
---
permalink: "api/v1/meetups.json"
---
{{ site.data.meetups |
flatten_hash |
filter_fields: 'name,description,city,datetime' |
wrap_with_key: 'meetups' |
jsonify }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment