Skip to content

Instantly share code, notes, and snippets.

@jrecursive
Created March 20, 2014 17:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jrecursive/9668987 to your computer and use it in GitHub Desktop.
Save jrecursive/9668987 to your computer and use it in GitHub Desktop.
// example breadboard script
//
// the logic behind http://thinkdifferent.ly/stuff/breadboard7.mp4
//
// this same script is generated if you wire your circuit in the UI, but
// i thought it would be interesting to share how to create circuits from
// the underlying script (the UI issues these same commands).
// there are a few basic concepts: devices, wires, and channels. devices
// are data stream sources, functions, and sinks. every device has zero
// or more "ports" which are ingress and egress points for streaming data.
// wires connect data from output ports to input ports. channels allow
// communication with external connections (e.g., websocket connections for
// the UI, etc.). so let's take a look at how to create the circuit you
// saw in the video by script:
// first we need to create a circuit and set it as our working context.
create circuit test;
use test;
//
// let's create a channel so we can spy on tweets as they come in
//
create channel tweets;
//
// tweeter is a device that connects to twitter's filterstream streaming tweets
//
create device tweeter-1 as tweeter {"filters":["ukraine", "russia", "putin", "news", "crimea"]};
// now let's connect the tweeter output to the channel input via a
// "channeler" device; this device takes any messages it receives and
// publishes them to a channel (allowing you to see them in a console, etc.)
create device tweet-channel as channeler {"channel":"tweets"};
create wire tweet-wire from tweeter-1.tweets to tweet-channel.data;
// json-path devices effectively operate as xpath would on streaming xml.
// in this case, let's pull out the screen names from each tweet and send it
// to a special type of lru device. by default this lru has a 1 minute window
// and computes a histogram. in the device UI, if it's a link, it will render
// it as a clickable link that opens in a new window.
create device tw-users as json-path {"path":"$.tweet.screen_name_s"};
create device tw-users-lru as lru {"field":"value"};
create wire w-0 from tweeter-1.tweets to tw-users.data;
create wire w-tw-users from tw-users.value to tw-users-lru.data;
// let's use another json-path instance to pull out the first media_url_ss field
// value which is always an image url. we'll send those to another instance of
// the lru, this time with a "media" flag telling it to expect images (and display
// them instead of clickable links)
create device tw-media as json-path {"path":"$.media[0].media_url_ss"};
create device tw-media-lru as lru {"field":"value", "media":true};
create wire w-1 from tweeter-1.tweets to tw-media.data;
create wire w-tw-ctry from tw-media.value to tw-media-lru.data;
// now we'll use a json-path instance again and pull the hashtags_s_mv array
// of hashtags from each tweet. because most devices expect one type of value
// to operate on, we'll connect the output of the json-path instance to an
// instance of a device called "expander". it takes any array of values and
// expands them into individual messages, one for each element of the array,
// so they can be operated on individually downstream; in this case, one
// hashtag at a time. then on to another lru.
create device tw-tags as json-path {"path":"$.tweet.hashtags_s_mv"};
create device expander-2 as array-expander;
create device tw-tags-lru as lru {"field":"value"};
create wire w-2 from tweeter-1.tweets to tw-tags.data;
create wire w-tw-tags from tw-tags.value to expander-2.in;
create wire w-tw-tags-1 from expander-2.out to tw-tags-lru.data;
// this is the same as the above, except we pull a list of urls out,
// and since they're object, we run them through another json-path
// instance to pull the actual url out of it, then on to another lru
// device.
create device tw-urls as json-path {"path":"$.urls"};
create device expander-1 as array-expander;
create device tw-urls-1 as json-path {"path":"$.value.expanded_url_tt"};
create device tw-urls-lru as lru {"field":"value"};
create wire w-3 from tweeter-1.tweets to tw-urls.data;
create wire w-tw-urls-1 from tw-urls.value to expander-1.in;
create wire w-tw-urls-2 from expander-1.out to tw-urls-1.data;
create wire w-tw-urls-3 from tw-urls-1.value to tw-urls-lru.data;
// and that's it! thanks for reading. @jrecursive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment