beatbox-puredata/purest_json/manual/Basic-Usage.html

248 lines
12 KiB
HTML
Raw Normal View History

2024-09-18 21:11:11 -03:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>
Basic Usage
</title>
<style>
/* Page tweaks */
.preview-page {
margin-top: 64px;
margin-bottom: 21px;
}
/* User-content tweaks */
.timeline-comment-wrapper > .timeline-comment:after,
.timeline-comment-wrapper > .timeline-comment:before {
content: none;
}
/* User-content overrides */
.discussion-timeline.wide {
width: 920px;
}
</style>
<link href="style.css" rel="stylesheet"/>
</head>
<body>
<div class="page">
<div class="preview-page" data-autorefresh-url="" id="preview-page">
<main id="js-repo-pjax-container">
<div class="clearfix new-discussion-timeline container-xl px-3 px-md-4 px-lg-5">
<div class="repository-content">
<div class="clearfix">
<div class="Layout Layout--flowRow-until-md Layout--sidebarPosition-end Layout--sidebarPosition-flowRow-end">
<div class="Layout-main">
<div class="Box md Box--responsive" id="readme">
<div class="Box-header d-flex border-bottom-0 flex-items-center flex-justify-between color-bg-default rounded-top-2">
<div class="d-flex flex-items-center">
<h2 class="Box-title">
Basic Usage
</h2>
</div>
</div>
<div class="Box-body px-5 pb-5">
<article class="markdown-body entry-content container-lg" id="grip-content">
<p>
The following examples are in the patch
<code>
examples/purest-json-test.pd
</code>
.
</p>
<h2>
<a aria-hidden="true" class="anchor" href="#querying-webservices" id="user-content-querying-webservices">
<span aria-hidden="true" class="octicon octicon-link">
</span>
</a>
Querying Webservices
</h2>
<p>
You can use
<code>
[rest]
</code>
to query webservices. I will explain this querying the history of the Makefile in the repository. You will see the handling of arrays in this example.
</p>
<p>
<a rel="noopener noreferrer nofollow" target="_blank">
<img alt="Webservice Example" src="webservice-example.png" style="max-width: 100%;"/>
</a>
</p>
<p>
As you can see, the webservice from Github returns an object with only one array. This array is then parsed by an instance of
<code>
[json-decode]
</code>
.
</p>
<p>
<code>
data: symbol [{"sha":"c505f1bbc72a73ceff447686db4c3c33f3bf3488"\,"node_id":"C_kwDOACK909oAKGM1MDVmMWJiYzcyYTczY2VmZjQ0NzY4NmRiNGMzYzMzZjNiZjM0ODg"\,"commit":{"author":{"name":"Thomas\ Mayer"\,"email":"thomas@residuum.org"\,"date":"2022-05-06T00:31:32Z"}\,"committer":{"name":"Thomas\ Mayer"\,"email":"thomas@residuum.org"\,"date":"2022-05-06T00:31:32Z"}\,"message":"Replace\ [import]\ with\ [declare]\ in\ examples.\\n\\nFixes\ #61"\,"tree":{"sha":"1a3c399407bb25f3976166e13f996b930df3b531"\,"url":"https://api.github.com/repos/residuum/PuRestJson/git/trees/1a3c399407bb25f3976166e13f996b930df3b531"}\,"url":"https://api.github.com/repos/residuum/PuRestJson/git/commits/c505f1bbc72a73ceff447686db4c3c33f3bf3488"\,"comment_count":0\,"verification":{"verified":false\,"reason":"unsigned"\,"signature":null\,"payload":null}}\,"url":"https://api.github.com/repos/residuum/PuRestJson/commits/c505f1bbc72a73ceff447686db4c3c33f3bf3488"\,"html_url":"https://github.com/residuum/PuRestJson/commit/c505f1bbc72a73ceff4476
</code>
</p>
<p>
Each array member is then parsed individually by
<code>
[json-decode]
</code>
, i.e. each member of the object is then output as list on the middle outlet, followed by a bang on the left outlet.
</p>
<pre><code>array-decoded: sha c505f1bbc72a73ceff447686db4c3c33f3bf3488
array-decoded: node_id C_kwDOACK909oAKGM1MDVmMWJiYzcyYTczY2VmZjQ0NzY4NmRiNGMzYzMzZjNiZjM0ODg
array-decoded: commit {\ "author":\ {\ "name":\ "Thomas\ Mayer"\,\ "email":\ "thomas@residuum.org"\,\ "date":\ "2022-05-06T00:31:32Z"\ }\,\ "committer":\ {\ "name":\ "Thomas\ Mayer"\,\ "email":\ "thomas@residuum.org"\,\ "date":\ "2022-05-06T00:31:32Z"\ }\,\ "message":\ "Replace\ [import]\ with\ [declare]\ in\ examples.\\n\\nFixes\ #61"\,\ "tree":\ {\ "sha":\ "1a3c399407bb25f3976166e13f996b930df3b531"\,\ "url":\ "https:\\/\\/api.github.com\\/repos\\/residuum\\/PuRestJson\\/git\\/trees\\/1a3c399407bb25f3976166e13f996b930df3b531"\ }\,\ "url":\ "https:\\/\\/api.github.com\\/repos\\/residuum\\/PuRestJson\\/git\\/commits\\/c505f1bbc72a73ceff447686db4c3c33f3bf3488"\,\ "comment_count":\ 0\,\ "verification":\ {\ "verified":\ false\,\ "reason":\ "unsigned"\,\ "signature":\ null\,\ "payload":\ null\ }\ }
array-decoded: url https://api.github.com/repos/residuum/PuRestJson/commits/c505f1bbc72a73ceff447686db4c3c33f3bf3488
array-decoded: html_url https://github.com/residuum/PuRestJson/commit/c505f1bbc72a73ceff447686db4c3c33f3bf3488
array-decoded: comments_url https://api.github.com/repos/residuum/PuRestJson/commits/c505f1bbc72a73ceff447686db4c3c33f3bf3488/comments
array-decoded: author {\ "login":\ "residuum"\,\ "id":\ 136216\,\ "node_id":\ "MDQ6VXNlcjEzNjIxNg=="\,\ "avatar_url":\ "https:\\/\\/avatars.githubusercontent.com\\/u\\/136216?v=4"\,\ "gravatar_id":\ ""\,\ "url":\ "https:\\/\\/api.github.com\\/users\\/residuum"\,\ "html_url":\ "https:\\/\\/github.com\\/residuum"\,\ "followers_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/followers"\,\ "following_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/following{\\/other_user}"\,\ "gists_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/gists{\\/gist_id}"\,\ "starred_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/starred{\\/owner}{\\/repo}"\,\ "subscriptions_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/subscriptions"\,\ "organizations_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/orgs"\,\ "repos_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/repos"\,\ "events_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/events{\\/privacy}"\,\ "received_events
array-decoded: committer {\ "login":\ "residuum"\,\ "id":\ 136216\,\ "node_id":\ "MDQ6VXNlcjEzNjIxNg=="\,\ "avatar_url":\ "https:\\/\\/avatars.githubusercontent.com\\/u\\/136216?v=4"\,\ "gravatar_id":\ ""\,\ "url":\ "https:\\/\\/api.github.com\\/users\\/residuum"\,\ "html_url":\ "https:\\/\\/github.com\\/residuum"\,\ "followers_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/followers"\,\ "following_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/following{\\/other_user}"\,\ "gists_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/gists{\\/gist_id}"\,\ "starred_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/starred{\\/owner}{\\/repo}"\,\ "subscriptions_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/subscriptions"\,\ "organizations_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/orgs"\,\ "repos_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/repos"\,\ "events_url":\ "https:\\/\\/api.github.com\\/users\\/residuum\\/events{\\/privacy}"\,\ "received_events
array-decoded: parents [\ {\ "sha":\ "b1c602e3d73a9088e951181bea0a500e49223c8e"\,\ "url":\ "https:\\/\\/api.github.com\\/repos\\/residuum\\/PuRestJson\\/commits\\/b1c602e3d73a9088e951181bea0a500e49223c8e"\,\ "html_url":\ "https:\\/\\/github.com\\/residuum\\/PuRestJson\\/commit\\/b1c602e3d73a9088e951181bea0a500e49223c8e"\ }\ ]
array-done: bang
...
</code></pre>
<h2>
<a aria-hidden="true" class="anchor" href="#using-couchdb" id="user-content-using-couchdb">
<span aria-hidden="true" class="octicon octicon-link">
</span>
</a>
Using CouchDB
</h2>
<p>
This example will show the usage of diverse HTTP request methods, GET, PUT, and DELETE. You will also see, how you can add data to PUT requests.
</p>
<p>
<a rel="noopener noreferrer nofollow" target="_blank">
<img alt="CouchDB Example" src="couchdb-example.png" style="max-width: 100%;"/>
</a>
</p>
<p>
A CouchDB running on the same computer is implied.
</p>
<p>
Here is the output from the main example in examples/purest-json-test.pd with explanations.
</p>
<p>
First, we create the database and get info about the database:
</p>
<pre><code>couchdb return: list ok 1
couchdb return: list db_name test
couchdb return: list doc_count 0
couchdb return: list doc_del_count 0
couchdb return: list update_seq 0
couchdb return: list purge_seq 0
couchdb return: list compact_running 0
couchdb return: list disk_size 79
couchdb return: list instance_start_time 1.31733e+15
couchdb return: list disk_format_version 5
couchdb return: list committed_update_seq 0
</code></pre>
<p>
After that, we build a JSON document and store it in the database:
</p>
<pre><code>json-encoded data: symbol \{ "value": "440"\\, "duration": "1000"\\, "start": "10"\\, "name": "first note"\\, "var": [ "yabba"\\, "dabba"\\, "doo" ]\\, "on": "TRUE" \}
decode json data: list value 440
decode json data: list duration 1000
decode json data: list start 10
decode json data: list name first note
decode json data: list var [ "yabba"\\, "dabba"\\, "doo" ]
decode json data: list on 1
couchdb return: list ok 1
couchdb return: list id 1
couchdb return: list rev 1-b7a3cb13c20812a4c7845e9a86f92099`
</code></pre>
<p>
Then we query the database for the stored document and use it to drive a simple ramp:
</p>
<pre><code>id: 1
couchdb return: list _id 1
couchdb return: list _rev 1-b7a3cb13c20812a4c7845e9a86f92099
couchdb return: list value 440
couchdb return: list duration 1000
couchdb return: list start 10
name: first note
couchdb return: list name first note
couchdb return: list var [ "yabba"\\, "dabba"\\, "doo" ]
couchdb return: list on 1
</code></pre>
<p>
In the end we delete the database:
</p>
<pre><code>couchdb return: list ok 1
</code></pre>
</article>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</main>
</div>
</div>
<script>
function showCanonicalImages() {
var images = document.getElementsByTagName('img');
if (!images) {
return;
}
for (var index = 0; index < images.length; index++) {
var image = images[index];
if (image.getAttribute('data-canonical-src') && image.src !== image.getAttribute('data-canonical-src')) {
image.src = image.getAttribute('data-canonical-src');
}
}
}
function scrollToHash() {
if (location.hash && !document.querySelector(':target')) {
var element = document.getElementById('user-content-' + location.hash.slice(1));
if (element) {
element.scrollIntoView();
}
}
}
function autorefreshContent(eventSourceUrl) {
var initialTitle = document.title;
var contentElement = document.getElementById('grip-content');
var source = new EventSource(eventSourceUrl);
var isRendering = false;
source.onmessage = function(ev) {
var msg = JSON.parse(ev.data);
if (msg.updating) {
isRendering = true;
document.title = '(Rendering) ' + document.title;
} else {
isRendering = false;
document.title = initialTitle;
contentElement.innerHTML = msg.content;
showCanonicalImages();
}
}
source.onerror = function(e) {
if (e.readyState === EventSource.CLOSED && isRendering) {
isRendering = false;
document.title = initialTitle;
}
}
}
window.onhashchange = function() {
scrollToHash();
}
window.onload = function() {
scrollToHash();
}
showCanonicalImages();
var autorefreshUrl = document.getElementById('preview-page').getAttribute('data-autorefresh-url');
if (autorefreshUrl) {
autorefreshContent(autorefreshUrl);
}
</script>
</body>
</html>