Get content from API when building

Not sure if this is strictly a Middleman question… forgive my ignorance, I’m quite new to Middleman (and am more of a designer than dev).

I’d like to build a site, that pulls some of it’s data from an API, but as this content will not be updated particularly often, I’d like the data to be a static snapshot from the API. Middleman seem like it might be a good solution to do this. So what I’m asking is, is it possible to pull data from an API at the time of building the site (and not have any of the API code in build directory)? So essentially, while developing I’ll be using the API, looping through it in the template etc, but the code on the server will just be the resulting HTML.

The API in question has Javascript and Ruby libraries, amongst others. I’m thinking that if I were to use Javascript, Middleman would want to include this, verbatim in the build (maybe there’s a way to control this?), and so I should be looking at Ruby… can you use Ruby in Middleman templates?

Any advice at this stage would be awesome.

I’d write a Ruby script to pull the data from the API and place it into your MM project like the MM data files feature expects.

Once your API pull code is working well, it would be trivial to run that script at the beginning of the build process via a MM custom extension.

Something simple like this:

class FetchApiData < Middleman::Extension
  def initialize(app, options_hash={}, &block)
    app.before_build do |builder|
      builder.run "ruby fetch_api_data.rb"
    end
  end
end