Quickstart: Run any Blockspring function with Ruby.

In this tutorial, we'll learn how a single line of Ruby gets you access to all of the powerful utilities, algorithms, API calls, visualizations (and more) in the Blockspring community library.

Installation


First, install Blockspring. From your command line, enter:

gem install blockspring

Blockspring is installed. To initialize it, open your Ruby environment and enter:

require "blockspring"

Your first run: define a word.


Let's get word definitions in one line of Ruby:

res = Blockspring.runParsed("top-definition-for-word", {word: "Flibbertigibbet"})

You'll see the following printed in your Ruby console:

puts res.params["top_definition"]
a silly flighty person

You ran your first Blockspring function. Let's explain how that worked.


Blockspring functions in a nutshell


When you ran Blockspring.runParsed, here's what happened:

  1. Blockspring used your function name to find Top Definition for a Word, a simple call to Merriam Webster, written in Python, and submitted by user mapsup.

  2. Top Definition for a Word quickly executed in the cloud, with your word passed through as an input argument.

  3. You got back results and printed the definition, found in params["top_definition"].

That instant replay should raise some questions. Let's answer them.


A quick Q+A


Where do I find more Blockspring functions?
Browse the community library to find utilities, algorithms, API calls, visualizations, and more. You're now a quick search away from programming just about anything.


Great, I found a function. How do I run it?
Click "Integrations" on the function's homepage, choose Ruby, and copy and paste the given code snippet to wherever you need it. Try it yourself with Top Definition for a Word.


How do I know what inputs my function requires?
Again, click Top Definition for a Word, go to "Integrations", click Ruby, and you'll see in the given code snippet the only input required is word. The snippets are helpful for finding a function's required input arguments.


How do I know where to find the function output?
The output is found in the result's res.params. Explore that variable to find the output you're looking for. To learn more about the response, visit the api reference.


How do I create my own Blockspring function?
Visit the next Ruby quickstart to learn how to add your own function to Blockspring.


Your second run: interactive google map


Let's create an interactive Google map in one line of Ruby.

res = Blockspring.runParsed("interactive-google-map", {
  locations: [
    ["Latitude", "Longitude", "Tooltip"],
    [37.4217542234, -122.100920271, "Somewhere"],
    [41.895964876906, -87.632716978217, "Out"],
    [28.58230778, 77.09399505, "There"]
  ]
})

Now print out the response.

puts res.params["map"]
"/var/folders/6w/920995kj30d_tn8skl8ml5wr0000gn/T/20150212-88514-1rrr4uy-map.html"

This function saves the interactive Google map as an HTML file to a temporary directory on your machine, and sets that location to res.params["map"]. That means you can open map as you would open any HTML file.

Go to your terminal and copy/paste the filepath you saw in res.params["map"] (remember - your filepath will be different) like so:

open /var/folders/6w/920995kj30d_tn8skl8ml5wr0000gn/T/20150212-88514-1rrr4uy-map.html

You should see an interactive map open in your browser that looks like the image below:

Ruby quickstart gmap

Your third run: summarize a URL


This time, let's summarize a URL in a line of Ruby.

To use Blockspring in your apps, you'll want to . Doing so guarantees you 100k function runs per month for free! now and we'll show you how to include it in your function below.

We'll also enable caching. If the summarization was done within the last 3,600 seconds, you'll receive it right away instead of needing to re-run the function.

res = Blockspring.runParsed("summarize-text", {
  "url" => "http://www.usatoday.com/story/life/music/2015/04/18/rock-hall-of-fame-2015-induction-ceremony/25913423/",
  "sentences" => 1
}, {
  "cache" => true,
  "expiry" => 3600
})

puts res.params["summary"]
# Such was the case Saturday night at the 30th annual gathering, which saw the inductions of Ringo Starr, Bill Withers, Joan Jett and the Blackhearts, Stevie Ray Vaughan & Double Trouble, the Paul Butterfield Blues Band, Green Day, Lou Reed and the "5'' Royales.

Become a Blockspring pro


Blockspring lets you program just about anything with a single line of Ruby and you're well on your way. You now know how to:

You can also add your own public (or private) functions to Blockspring. Any function you add to Blockspring can be easily run across languages, chained, composed, cached, and shared with others, without any set up or maintenance. Check out the next quickstart tutorial to learn how to create your first Blockspring function.


Discover new functions Work at Blockspring