https://github.com/michaelcurrin/static-site-api-request-demo
Demo static site which serves data fetched from an API at build time
https://github.com/michaelcurrin/static-site-api-request-demo
api demo netlify request ruby static-site
Last synced: about 1 month ago
JSON representation
Demo static site which serves data fetched from an API at build time
- Host: GitHub
- URL: https://github.com/michaelcurrin/static-site-api-request-demo
- Owner: MichaelCurrin
- License: mit
- Created: 2020-01-25T10:25:54.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-23T10:13:38.000Z (over 5 years ago)
- Last Synced: 2025-01-14T20:52:16.484Z (over 1 year ago)
- Topics: api, demo, netlify, request, ruby, static-site
- Language: Ruby
- Homepage: https://static-site-api-request-demo.netlify.com
- Size: 21.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Static Site API Request Demo
> Demo static site which serves data fetched from an API at build time
[](https://app.netlify.com/sites/static-site-api-request-demo/deploys)
[](https://jekyllrb.com)
[](https://www.ruby-lang.org)
[](https://github.com/MichaelCurrin/static-site-api-request-demo/blob/master/LICENSE)
The API request is done during a remote build or locally. The data is written to a file in the served directory.
This works on Netlify as it allows custom build commands. This will not work on Github Pages though.
Ruby requests are based on tutorials:
- https://lostisland.github.io/faraday/usage/
- http://zetcode.com/web/rubyfaraday/
**Table of contents**
- [Deploy to Netlify](#deploy-to-netlify)
- [Install](#install)
- [System dependencies](#system-dependencies)
- [Project dependencies.](#project-dependencies)
- [Run](#run)
- [Development](#development)
## Deploy to Netlify
1. Login to [app.netlify.com](https://app.netlify.com)
1. Create a new site.
1. Point it at this repo on Github.
1. Optionally rename the site.
1. Wait for the site to build.
1. View the URL.
If you want to use the a secret token in your build, set the following in the Environment section of your Netlify site.
| Key | Value |
| ----- | --------- |
| TOKEN | secret123 |
When you view the `api_data.json` file on the website, the Secret value will be set.
## Install
Setup this project locally - instructions are for Unix-like (_Linux_ or _macOS_) systems.
### System dependencies
Install [Ruby](https://www.ruby-lang.org/en/documentation/installation/#package-management-systems) using a command appropriate for your OS.
Install the [Jekyll](https://jekyllrb.com/) and [Bundler](https://bundler.io/) globally using the command below.
```bash
$ gem install jekyll bundler
```
_Jekyll_ is used to build static sites. _Bundler_ is used to manage Ruby gem dependencies in an isolated directory, to preserve your global gems.
### Project dependencies.
Install gems.
```bash
$ make install
```
in future, run this command to upgrade to the latest gems.
```bash
$ make upgrade
```
When you upgrade, the [Gemfile.lock](/Gemfile.lock) file will be updated, if there are any changes. You can commit this to your own repo. This file is optional for _Github Pages_ but required if you want to deploy to _Netlify_.
## Run
```sh
$ make serve-all
```
That will command will:
1. Clear the public `_site` directory (or create it if it does not exist).
2. Build the Jekyll site.
3. Fetch API data and store create new data files in the public directory.
4. Start a web serve in the public directory.
Open in the browser:
- http://localhost:4000/
If you make any changes to the project, you need to stop and start the server.
### Use token
Part of this project includes a demo of using a secret token value, such as one for doing API requests.
Set the token using the command below. When you view the `api_data.json` file, you should see the secret value set with the value.
```sh
$ TOKEN=secret123 make serve-all
```
## Development
See the [Makefile](/Makefile) commands:
```sh
$ make help
```
Note that for commands that start as `bundle exec ruby`, this was recommended to avoid errors on Netlify when running ruby. Also, when running locally, ruby can only local gems in the vendor directory if this command is used.