Git Pre-Commit
With the new Subaru UK site in development we’ve been expanding on the functionality of the API. Unfortunately, this means updating documentation… boo.. As we’ve been making many changes with several more to come, I needed something to fetch and update the example files…
Firstly, I’m using yaml because the documentation site is running on github pages and I can therefore use a global _config.yml file and have access to the same data within the actual pages.
This makes it easier for me, as I can just reference the config array within the site pages; so no need to remember the paths etc.
Normally for such a script I would write it in bash shell, but I needed to do it quickly and a commonly used language with built in yaml parsing would be needed. Python came close, but the library isn’t install by default, where as the Ruby equivalent does; so I’m using Ruby for it.
The Code
The code is fairly basic, but hopefully others might find it useful. The Ruby file reads in a local _config.yml file and uses the resulting config array to fetch and save files to relative directories.
The Config
Just to keep the page short, I’ve only put a couple of pages to fetch, but you can use as many as you want.
key: "1234567890"
base_url: "http://1234567890.api.co.uk/"
pages:
news:
url: "/news/all.xml"
filename: "news.xml"
offers:
url: "/offers/all.xml"
filename: "offers.xml"
The Ruby
The files that are downloaded are saved within a local folder called results; as this was a quick script I left it hard coded, feel free to change it.
#!/usr/bin/ruby
require "yaml"
require "open-uri"
def fetch(xmlfile, localname, replace)
writeOut = open(localname, "wb")
writeOut.write(open(xmlfile).read)
writeOut.close
end
def download(examples, base_url, replace)
examples.each do |name, info|
puts " #{name} \n"
fetch("#{base_url}#{info['url']}", "./results/#{info['filename']}", replace)
end
end
config = YAML::load_file("./_config.yml");
examples = config['pages'].sort
puts "-- fetching examples\n"
download(examples, config['base_url'], config['key'])
puts "-- done\n"
The Files
All the files that are used…
blog comments powered by Disqus


