Quickstart: Add your first Ruby function to Blockspring.

In this tutorial we'll show you how to add a Ruby function to Blockspring. Doing so is helpful if you need to:

  • Expose your function to the world. Need some Ruby in your Node app, Android app, Arduino device, or even in Google Spreadsheets? You'll be able to run your function from any application and device by simply copy/pasting a single line of code.

  • Share your function with anyone. You'll automatically generate a simple form and webpage documentation. Never spend time explaining your code, or running it for other people.

  • Combine Blockspring functions together. You can mix/match Blockspring functions like building blocks. And the system gets really fast when it knows ahead of time which functions you're about to run together.

  • Add really, really powerful caching. Don't just cache when you run a function, use Blockspring's social caching. What's the point of recalculating when someone's already done the calculating?

Let's get started!


Expose your function to the world.


This is going to be insanely fast. In 30 seconds, you're going to add a function to Blockspring that that reads PDFs from the Bureau of Labor Statistics - very specific :).

require "pdf-reader"
require "open-uri"

reader = PDF::Reader.new(open("http://www.bls.gov/web/jolts/jlt_labstatgraphs.pdf"))
text = reader.pages.map {|page| page.text}

puts text.join("\n")

Click to populate the Blockspring editor with the code above, click Publish, and you're done!


What just happened?

When you published your function, Blockspring automatically deployed it to the cloud, gave it an API, generated a webpage, and exposed it to all of the Blockspring client libraries.


Why is this awesome?

Your Ruby function is now in the cloud. That means you can run it from any application or internet-enabled device with a single command. What once took 4 lines of Ruby, now is 1 line in any language. For instance, to run your PDF reader in Python, just type the following:

import blockspring

# call PDF reader (https://open.blockspring.com/blocks/9e52ff6d982b96c944ca1ea4590ea581)
blockspring.runParsed("9e52ff6d982b96c944ca1ea4590ea581")


Where do I learn more about using my function?

Your published function should like something like this PDF reader. On each Blockspring function page you can click "Integrations", and depending on where you need to use your function, copy/paste the associated one line of code to run it. Check out the run Ruby in Blockspring quickstart to learn more.


That was fast. Can you show me how to publish again?

Here's a quick video that shows you how to publish step-by-step.


Share your function with anyone.

Blockspring lets you share your work with programmers and non-programmers alike because we generate the webpage and docs.

Currently, the webpage for your scraper function is just a "Submit" button. That's because this function doesn't take any inputs.

Submit only form

Let's tell Blockspring that your function accepts inputs. For instance, try reading the PDF from any site, not just BLS.

Click to populate the Blockspring editor with the code below, click Publish, and you'll have a nice form for your function.

require "pdf-reader"
require "open-uri"
require "blockspring"

block = lambda do |request, response|
    reader = PDF::Reader.new(open(request.params["url"]))
    text = reader.pages.map {|page| page.text}
    response.addOutput("text", text.join("\n"))
    response.end()
end

Blockspring.define(block)

Take a look at the final product. Here's a video that shows the process step-by-step:



What just happened?

You updated your function in four ways:

  • Import the library. You imported the blockspring library with require "blockspring". Blockspring captures outputs by reading what is printed. That's why we're suppressing loading messages.
  • Define the function. You wrapped the function in a Blockspring.define(). This tells Blockspring what function run.
  • Get the inputs. Receiving the url input in request.params["url"]. All function inputs will be found in request.params. The "url" name is defined when you added a parameter through the Blockspring code editor.
  • Declare the outputs. Adding the scraped output using response.addOutput. Blockspring passes inputs and outputs as JSON. This is adding your output to the JSON response.
Learn more with the Ruby API reference.


Why is this awesome?

You can now share a parameterized function without doing any extra work. By telling Blockspring about your inputs, you automatically get a form so anyone can run your function manually. Learn more about input parameter types.


Wait - how do I run my function in other languages with inputs?

Check out running Ruby functions to get a refresher on running Blockspring functions. Here's the updated Python one-liner:

import blockspring

# call pdf reader (https://open.blockspring.com/blocks/3f329534df4829d17037a4357587f54c)
print blockspring.runParsed("3f329534df4829d17037a4357587f54c", {
  "url": "http://www.mfwi.edu/MFWI/Recordings/cat%20in%20the%20hat.pdf"
}).params["text"]

Let's get even fancier. For instance, we might want to get the sentiment of a Dr Seuss poem, the Cat in the Hat. Combine a Python sentiment analysis function with the Ruby PDF reader.

require "blockspring"

# pdf reader
pdf_text = Blockspring.runParsed("3f329534df4829d17037a4357587f54c", { url: "http://www.mfwi.edu/MFWI/Recordings/cat%20in%20the%20hat.pdf"}).params["text"]

# sentiment analysis of pdf text
puts Blockspring.runParsed("6dd22564137f10b8108ec6c8f354f031", { text: pdf_text}).params
You're running a Ruby and Python process together in two lines of code!


Now it's your turn.


Congratulations young grasshopper. You've learned to add a Ruby function to your Blockspring library. Write a function, get an application. Now go build!


Create a new function Work at Blockspring