diff --git a/Gemfile b/Gemfile index d8777ee27..79dace120 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,7 @@ source "https://rubygems.org" gem "jekyll" -gem "html-proofer" \ No newline at end of file +gem "html-proofer" +gem 'json' +gem 'hash-joiner' +gem 'open-uri-cached' diff --git a/Gemfile.lock b/Gemfile.lock index 6a9e84229..02cc62b69 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -13,6 +13,8 @@ GEM ethon (0.8.1) ffi (>= 1.3.0) ffi (1.9.10) + hash-joiner (0.0.7) + safe_yaml html-proofer (2.6.4) activesupport (~> 4.2) addressable (~> 2.3) @@ -47,6 +49,7 @@ GEM minitest (5.8.4) nokogiri (1.6.7.2) mini_portile2 (~> 2.0.0.rc2) + open-uri-cached (0.0.5) parallel (1.6.2) rb-fsevent (0.9.7) rb-inotify (0.9.7) @@ -65,8 +68,11 @@ PLATFORMS ruby DEPENDENCIES + hash-joiner html-proofer jekyll + json + open-uri-cached BUNDLED WITH 1.11.2 diff --git a/_config.yml b/_config.yml index 5fb91e3bb..8633ad882 100644 --- a/_config.yml +++ b/_config.yml @@ -10,9 +10,6 @@ gatekeeper_host: # Dataset schema schema: default -# Dataset schema -schema: default - # Categories category_list: - Arts / Culture / History diff --git a/_data/cache b/_data/cache new file mode 100644 index 000000000..e69de29bb diff --git a/_plugins/jekyll_get.rb b/_plugins/jekyll_get.rb new file mode 100644 index 000000000..2d468d53c --- /dev/null +++ b/_plugins/jekyll_get.rb @@ -0,0 +1,40 @@ +require 'json' +require 'hash-joiner' +require 'open-uri' + +module Jekyll_Get + class Generator < Jekyll::Generator + safe true + priority :highest + + def generate(site) + config = site.config['jekyll_get'] + if !config + return + end + if !config.kind_of?(Array) + config = [config] + end + config.each do |d| + begin + target = site.data[d['data']] + source = JSON.load(open(d['json'])) + if target + HashJoiner.deep_merge target, source + else + site.data[d['data']] = source + end + if d['cache'] + data_source = (site.config['data_source'] || '_data') + path = "#{data_source}/#{d['data']}.json" + open(path, 'wb') do |file| + file << JSON.generate(site.data.cache[d['data']]) + end + end + rescue + next + end + end + end + end +end diff --git a/_plugins/readme.md b/_plugins/readme.md new file mode 100644 index 000000000..500863f1b --- /dev/null +++ b/_plugins/readme.md @@ -0,0 +1,72 @@ +# Install + +Add this file to `_plugins` in the root of your Jekyll site. + +Add dependencies these dependencie to your sites Gemfile + +``` +gem 'json' +gem 'hash-joiner' +gem 'open-uri-cached' +``` + +# Run +`bundle install` +`jekyll build` +`jekyll + +# Configure + +This plugin reads settings from the `_config.yml` file. Add settings as attributes or an array of attributes for multiple files. + +## Example + +```yml +jekyll_get: + data: team + json: 'https://18f.gsa.gov/hub/api/team/' +``` + +Or + +```yml +jekyll_get: + - data: team + json: 'https://18f.gsa.gov/hub/api/team/' + - data: projects + json: 'https://18f.gsa.gov/hub/api/projects/' +``` + +Use in a liquid template as if it were a local data file: + +```liquid +{% for member in site.data.team %} + Hello {{member[1].first_name}} +{% endfor %} +``` + +Optionally, set a cache attribute to save a local copy of the data in the `_data` directory: + +```yml +jekyll_get: + - data: team + json: 'https://18f.gsa.gov/hub/api/team/' + cache: true + - data: projects + json: 'https://18f.gsa.gov/hub/api/projects/' + cache: true + - data: pif_team + json: 'https://18f.gsa.gov/hub/api/pif_team/' + cache: true + - data: pif_projects + json: 'https://18f.gsa.gov/hub/api/pif_projects/' + cache: true +``` + +## Public domain + +This project is in the worldwide [public domain](LICENSE.md). As stated in [CONTRIBUTING](CONTRIBUTING.md): + +> This project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the [CC0 1.0 Universal public domain dedication](https://creativecommons.org/publicdomain/zero/1.0/). +> +> All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest.