Last active
August 29, 2015 14:12
-
-
Save kahunacohen/c269dd365298befc4428 to your computer and use it in GitHub Desktop.
Gets comments from a pooleapp (http://www.pooleapp.com) form, writing the results to a YAML file. Reorganizes data structure indexing by the path the comment was submitted from.
This file contains 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
#!/usr/bin/env ruby | |
# Gets comments from a pooleapp (http://www.pooleapp.com) form and writes | |
# the results to a YAML file indexed by the path the comment was submitted from. | |
# This allows you to more easily associate a comment with the page it was submitted | |
# from. | |
# | |
# Given a pooleapp form created on pooleapp.com with an API KEY of "MY_API_KEY" and the following | |
# form in a Jekyll template: | |
# | |
# <form action="http://pooleapp.com/stash/MY_API_KEY/" method="post"> | |
# <input type="hidden" name="redirect_to" value="/my-thank-you-page/" /> | |
# <input type="hidden" name="path" value="{{page.url}}" /> | |
# <input placeholder="Your name (required)" required type="text" name="name"/> | |
# <input placeholder="Your website" type="text" name="url"/> | |
# <textarea placeholder="Your comments (required)" required type="text" name="comment"></textarea> | |
# <input type="submit" value="submit" /> | |
# </form> | |
# | |
# ...and an exported environment variable of $POOLEAPP_API_KEY containing | |
# the API KEY for the pooleapp form you are using to store your comments, | |
# running $ pooleapp-comments.rb _data/comments.yml | |
# will result in a yaml file of your posted comments indexed by the path | |
# the comment was submitted from. | |
# | |
# The following named input fields are required on your HTML form: | |
# - path: The path the comment was submitted from. This is set by having | |
# <input type="hidden" name="path" value="{{ page.url }}"/> | |
# on your HTML form (using liquid templates in Jekyll). | |
# - url: The URL for the submitter (either a URL to their website or email). | |
# This field should be a named input in your field, but is not required. | |
# - name: The name of the comment submitter. | |
# - comment: The actual comment text. | |
# | |
# Usage: | |
# No argument, writes to ./comments.yml | |
# $ ruby comments.rb | |
# With argument, writes to specified file: | |
# $ ruby comments.rb _data/comments.yml | |
require 'open-uri' | |
require 'yaml' | |
api_key = ENV['POOLE_API_KEY'] || nil | |
if api_key | |
out_file = ARGV[0] || "./comments.yml" | |
url = 'http://pooleapp.com/data/871b8373-89da-4a06-b2d8-dd30de8327a1.yml' | |
yml_str = open(url) { |io| io.read } | |
thing = YAML.load(yml_str) | |
newhash = {'comments'=> {}} | |
last_path = nil | |
thing['sessions'].each_with_index {|key, index | | |
thispath = key['path'] | |
if index != 0 | |
last_path = thing['sessions'][index-1]['path'] | |
end | |
if ! newhash['comments'].has_key?(thispath) | |
newhash['comments'][thispath] = [] | |
end | |
newob = {} | |
newob['comment'] = key['comment'] | |
newob['name'] = key['name'] | |
newob['created'] = key['created'] | |
newob['url'] = key['url'] | |
newhash['comments'][thispath].push(newob) | |
} | |
File.open(out_file, 'w') {|f| f.write newhash.to_yaml } | |
else | |
abort("Must define $POOLEAPP_API_KEY variable") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a broader explanation of why I wrote this: http://www.kahunacohen.com/2015/01/07/adding-dynamic-data-to-jekyll/