Creating an Alfred workflow with Ruby

victorhazbun

Victor Hazbun

Posted on April 28, 2020

Creating an Alfred workflow with Ruby

If you love Alfred like me, you will enjoy this tutorial, I will teach you how to build a custom workflow to short URLs using https://bitly.com/ (Free account).

Setting up a Bitly account

First, you will need to signup for a Bitly account.

Then create a generic access token (You will need it later). Here are the instructions.

Building the Alfred workflow

This is the code for the workflow, let's talk about it.

require 'net/http'
require 'uri'
require 'json'

uri = URI.parse('https://api-ssl.bitly.com/v4/shorten')

# Set parameters
params = {
  long_url:  ARGV[0]
}
# Set headers
headers = {
    'Host': 'api-ssl.bitly.com',
    'Authorization': "Bearer #{ENV['BITLY_ACCESS_TOKEN']}",
    'Content-Type': 'application/json',
    'Accept': 'application/json'
}
# Create the HTTP objects
http = Net::HTTP.new(uri.host, uri.port)
# Force SSL request
http.use_ssl = true
# Build POST request
request = Net::HTTP::Post.new(uri.request_uri, headers)
# Set request body (As JSON)
request.body = params.to_json
# Send the request
response = http.request(request)
# Get link from response
puts JSON.parse(response.body)['link']
Enter fullscreen mode Exit fullscreen mode

As you might know, you can build your own workflows using Ruby and other languages, even bash.

I used three Ruby native dependencies:

  • Net::HTTP to make HTTP requests to Bitly
  • URI to build URLs
  • JSON to parse the response body and be able to extract the shorten link

The parameters I'm sending to Bitly come from the user input via Alfred, so in order to access the first and only argument, we have to use ARGV[0].

For the headers it is mandatory to set the Authorization Bearer since this is how Bitly knows who is trying to shorten the link. We read the access token from the environment variables.

Alfred will ask for the token when you download the workflow.

We also need to make the request of type https since they Bitly API requires a secure protocol. We not only need to use https:// in the request URL, we also have to tell http.use_ssl, otherwise it won't work.

The request.body has to be set up as JSON as Bitly documentation indicates, so we convert the parameters to JSON like this params.to_json.

Finally we have to parse the JSON response using JSON.parse(response.body), which returns a hash with keys and values.

The key we need is the link, for Alfred workflows, you actually need to puts the output, if you don't do this the next script won't contain the expected input.

Download the Alfred workflow

You can directly download the workflow from the Github repository

NOTE: You will be asked to introduce your personal access token.

Final thoughts

I had a really fun time coding this workflow. It was a bit tricky initially since there is not too many examples out there, however the documentation was good enough.

If you liked the workflow please consider giving it a ⭐ on Github.

Happy coding!

💖 💪 🙅 🚩
victorhazbun
Victor Hazbun

Posted on April 28, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related