Creating an Alfred workflow with Ruby
Tags: ruby, productivity
Published: 2020-04-28 07:19:51

If you love [Alfred](https://www.alfredapp.com/) 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](https://bitly.com/a/sign_up).

Then create a generic access token (You will need it later). [Here are the instructions](https://support.bitly.com/hc/en-us/articles/230647907-How-to-create-an-OAuth-access-token-for-the-Bitly-API).


## Building the Alfred workflow

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

```ruby
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']
```

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](https://github.com/bonsai-labs-io/alfredapp-bitly-shorten/raw/master/bitly-shorten.alfredworkflow)

**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 :star: on [Github](https://github.com/bonsai-labs-io/alfredapp-bitly-shorten). 

Happy coding!