aptonic/dropzone3-actions — pull request #22 by alexchiri (2014-09-14 16:23:54+00:00)
Title: Goo.gl action

Hi @aptonic, @atika!

# Goo.gl action
In order to use the Dropzone Google credentials with Goo.gl, the Url Shortener API should be enabled for that account in the Google Developer Console, I think @aptonic can help with that.

I initially intended to just give a few hints on how to migrate the action to the Google, but the hints grew more and more, so I decided to migrate it myself. Hope that's ok with you. Far from me to impose this as "the best" solution! Considering in how many points it could fail, I preferred writing the code and test it on the way.

I will explain here every step I did:

## 1 - extra class and libs, refactoring
* moved all the methods(except `dragged` and `clicked`) from the action.rb to another class, googl.rb - so the action contains only the event methods
* copied the lib folder from the Google drive action into this action - it contains the Ruby Google API and all its dependencies
* renamed `shorten` into `process`, as it handles shortening of URLs and also expanding of URLs
* split `process` into `shorten_url` and `expand_url`

## 2 - general API setup
* copied `urlsafe_base64` from the Google Drive action. It generates an unique ID for the action instance, which is used to generate an unique base_filename for an action instance. The base_filename is used to store a file where the whole API descriptor is stored, so it's not downloaded every time.
* API_VERSION = v1
* CACHED_API_FILE = "urlshortener-#{API_VERSION}.cache"

## 3 - authentication
* copied `get_authentication` method from Google Drive action - this one checks the `access_token` provided by Dropzone, gets a new one using the `refresh_token` and stores it

## 4 - configure client
* copied `configure_client` method from Google Drive action and refactored the discover code to get the 'urlshortener' api:

```ruby
@urlshortener = @client.discovered_api('urlshortener', API_VERSION)
```

## 5 - expand url
Refactored the `expand_url` method to use the API.
It's pretty simple, you just need to specify the right API method and because this is a GET request, the parameters. In this case, the short URL.

```ruby
result = @client.execute(
:api_method => @urlshortener.url.get,
:parameters => {
:shortUrl => url_to_expand
})
```

`result.success?` can tell you if everything went fine. If yes, the expanded url is in the `result.data` hash:
```ruby
expanded_url = result.data['longUrl']
```

## 6 - shorten url
This is one is a bit more complicated. Because it is a POST request, it has a payload. The payload is created like this:
```ruby
url_details = @urlshortener.url.insert.request_schema.new( { :longUrl => url_to_shorten } )
```

Then the API call is done, no parameters, but we have the body_object:
```ruby
result = @client.execute(
:api_method => @urlshortener.url.insert,
:body_object => url_details
)
```

There was no hash in the result this time, but an API class and the shortened URL in the `id` property:
```ruby
short_url = result.data.id
```
## 7 - testing
I tested the `shorten_url` and `expand_url` using my own Google credentials, by overwriting the credentials provided by Dropzone.

Looking forward for your thoughts!

Wish you a great new week ahead,
Alex