Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GitHub access tokens and cache API results #337

Merged
merged 2 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ node_modules/
npm-debug.log
.bundle/
assets/
.jekyll_get_cache/
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ source 'https://rubygems.org'
gem 'jekyll'
gem 'json'
gem 'redcarpet'
gem 'hash-joiner'
gem 'open-uri-cached'
gem 'jekyll-redirect-from'

Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ https://federalist.fr.cloud.gov/preview/18f/web-design-standards-docs/develop/

See the [`_posts` directory](_posts/#readme) for instructions on adding updates.

### Dynamic content

Some of the content on the documentation site is dynamically fetched from
GitHub. If you want to ensure that its API won't rate-limit you, you
may want to
[create an access token](https://github.com/blog/1509-personal-api-tokens)
and assign it to your `GITHUB_ACCESS_TOKEN` environment variable.

The dynamic content is stored in the `.jekyll_get_cache` directory and
won't be re-fetched once it's cached there. However, this means that your
data can get stale over time, so if you want to ensure that your site
is using the very latest data, you'll want to clear the cache by running:

```
rm -rf .jekyll_get_cache
```

## Contributing

Please read through our [contributing guidelines](CONTRIBUTING.md). These guidelines are directions for opening issues and submitting pull requests, and they also detail the coding and design standards we follow.
2 changes: 2 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ jekyll_get:
json: 'https://api.github.com/repos/18F/web-design-standards/releases'
- data: contributing
json: 'https://api.github.com/repos/18F/web-design-standards/contents/CONTRIBUTING.md'
decode_content: true
- data: standards-sites
json: 'https://api.github.com/repos/18F/web-design-standards/contents/WHO_IS_USING_USWDS.md'
decode_content: true

repos:
- name: U.S. Web Design Standards
Expand Down
67 changes: 43 additions & 24 deletions _plugins/jekyll_get.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'json'
require 'hash-joiner'
require 'open-uri'
require 'base64'

Expand All @@ -8,6 +7,45 @@ class Generator < Jekyll::Generator
safe true
priority :highest

def decode_content(source)
encoding = source['encoding']
target_content = source['content']
if encoding == 'base64'
source['decoded'] = Base64.decode64(target_content).force_encoding(Encoding::UTF_8)
else
source['decoded'] = target_content
end
end

def get_final_url(url)
if url.start_with? "https://api.github.com/"
access_token = ENV['GITHUB_ACCESS_TOKEN']
if access_token
return "#{url}?access_token=#{access_token}"
end
end
url
end

def load_json(site, d)
name = d['data']
url = d['json']
data_source = '.jekyll_get_cache'
path = "#{data_source}/#{name}.json"
if not File.exists?(path)
FileUtils.mkpath File.dirname(path)
print "Caching #{url} in #{path}...\n"
data = JSON.load(open(get_final_url(url)))
open(path, 'wb') do |file|
file << JSON.pretty_generate(data)
end
end
site.data[name] = JSON.load(open(path))
if d['decode_content']
decode_content site.data[name]
end
end

def generate(site)
config = site.config['jekyll_get']
if !config
Expand All @@ -17,30 +55,11 @@ def generate(site)
config = [config]
end
config.each do |d|
name_of_target = d['data']
url = d['json']
begin
target = site.data[name_of_target]
source = JSON.load(open(d['json']))
if target
HashJoiner.deep_merge target, source
else
site.data[name_of_target] = source
end
encoding = site.data[name_of_target]['encoding']
target_content = site.data[name_of_target]['content']
if encoding == 'base64'
site.data[name_of_target]['decoded'] = Base64.decode64(target_content).force_encoding(Encoding::UTF_8)
else
site.data[name_of_target]['decoded'] = target_content
end
if d['cache']
data_source = (site.config['data_source'] || '_data')
path = "#{data_source}/#{name_of_target}.json"
open(path, 'wb') do |file|
file << JSON.generate(site.data[name_of_target])
end
end
rescue
load_json(site, d)
rescue => e
print "jekyll_get: error fetching #{url}: #{e}\n"
next
end
end
Expand Down