From a6f2a0c0bcf56cba71413f7b1389125f3eb462ae Mon Sep 17 00:00:00 2001 From: Albert Liu Date: Mon, 15 Oct 2018 13:47:13 -0400 Subject: [PATCH 1/5] added some stuff to adapters.md --- architecture/adapters.md | 62 ++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/architecture/adapters.md b/architecture/adapters.md index 1c7cf98..87d2d56 100644 --- a/architecture/adapters.md +++ b/architecture/adapters.md @@ -1,4 +1,4 @@ - + - \ No newline at end of file + From cebb7bdc546b7a25198df2378bd4b5dd0f7851d4 Mon Sep 17 00:00:00 2001 From: Albert Liu Date: Mon, 15 Oct 2018 15:12:09 -0400 Subject: [PATCH 2/5] added more content --- architecture/adapters.md | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/architecture/adapters.md b/architecture/adapters.md index 87d2d56..13bab4c 100644 --- a/architecture/adapters.md +++ b/architecture/adapters.md @@ -39,7 +39,7 @@ end ``` ## Data Source -The first thing your adapter needs is a place to get data from. In the `yaml-rpi` adapter, that place is a file called `schools-and-subjects.yml`. +The first thing your adapter needs is a place to get data from. In the `yaml-rpi` adapter, that place is a file called `schools-and-subjects.yml`: ```yml @@ -63,13 +63,18 @@ Now that we have a place to get information from, we need to load it into the ad ``` ruby +# ... require 'yaml' ENV['YAML_SOURCE'] ||= 'schools-and-subjects.yml' +# ... + file = open(ENV['YAML_SOURCE']) + yaml_content = YAML.load(file) + # ... ``` -Here, `ENV` is just a container for the environment variables in Ruby, and `'YAML_SOURCE'` is the name of the environment variable. We're setting its value to the name of the file, `'schools-and-subjects.yml'`, and essentially that 'loads' it into the adapter. +Here, `ENV` is just a container for the environment variables in Ruby, and `'YAML_SOURCE'` is the name of the environment variable. We're setting its value to the name of the file, `'schools-and-subjects.yml'`. Then, when the adapter is asked for information, it first opens a file at the location, then uses a function from the `'yaml'` package to convert the file's contents to a ruby object. ## JSON API and REST Your adapter then needs to convert the data that it loaded into a format that Yacs can read - that's what the JSON API is for. This document will give a brief explanation of the API - a more in-depth explanation can be found [here][json-api]. @@ -94,33 +99,37 @@ In the `yaml-rpi` adapter, this is done through these lines in `app.rb`: ```ruby -require 'sinatra' -require 'sinatra/json' # Package that makes it easy to write information to JSON files +require 'sinatra/json' require 'oj' -json YAML.load(open(ENV['YAML_SOURCE'])) +# ... + json yaml_content ``` - +`require 'sinatra/json'` and `require 'oj'` tell ruby to load the Sinatra JSON helper package and the Optimized JSON package respectively. Then when the adapter needs to convert the content to JSON, the `json` function from `'sinatra/json'` is called, converting `yaml_content` to the JSON format. ## HTTP Protocol ```ruby +# ... require 'sinatra' set :bind, '0.0.0.0' set :port, 4600 +# ... + get "/:term_shortname" do - # json YAML.load(open(ENV['YAML_SOURCE'])) + # ... end ``` ## Implementation Agnostic +Notice that the only parts of the adapter that actually interact with YACS itself are format of the data (JSON) and the data transfer protocol (HTTP). Since both of these systems are implementation agnostic, you can write your adapter in whatever language you want! As long as it responds to HTTP requests with a JSON object, it's good to go! [yacs-adapters]: https://github.com/YACS-RCOS/yacs/tree/master/adapters [yaml-rpi-adapter]: https://github.com/YACS-RCOS/yacs/tree/master/adapters/yaml-rpi From a13f8e0255fa30cf68de354b37105357a4c56714 Mon Sep 17 00:00:00 2001 From: Albert Liu Date: Mon, 15 Oct 2018 23:36:17 -0400 Subject: [PATCH 3/5] added some more stuff to adapters.md --- architecture/adapters.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/architecture/adapters.md b/architecture/adapters.md index 13bab4c..9c234cb 100644 --- a/architecture/adapters.md +++ b/architecture/adapters.md @@ -1,5 +1,5 @@ -