---
title: "Tutorial for urlshorteneR -- v4"
author: "dmpe @ github"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Tutorial for urlshorteneR -- v4}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

`urlshorteneR` wraps 2 URL services `Bit.ly` and its 2 friends `is.gd` as well as `v.gd`. 
For Bit.ly, in order to shorten URLs and downloading useful statistics about them, you have to authenticate 
using Bearer Token, for which you need user account and API key.

For 2 other services `is.gd` & `v.gd`, there is no authentication needed. Just create desired short links, see below.

# Bit.ly

## User/App Information & Authentication for Bit.ly

Return some basic information about my user on Bit.ly. Additionally, is my account a "premium" one?

```{r}
library("knitr")
library(urlshorteneR)

if (interactive()) {
  bitly_bearerToken("access token")
  ui <- bitly_user_info(showRequestURL = TRUE)
  is_bitly_user_premium_holder()
}
```

We can also decide to update my username with a different one.

```{r}
if (interactive()) {
  bitly_update_user(name = "John Malc", showRequestURL = TRUE)
}
```

And what about the metadata about our OAUTH application?

```{r}
if (interactive()) {
  bitly_bearerToken("access token")
  bitly_app_details()
}
```

## Group Information

This retrieves information about a single group that user belongs to and and then about all groups that user are associated with.

```{r}
if (interactive()) {
  bitly_bearerToken("access token")
  bitly_retrieve_group(ui$default_group_guid)
  bitly_retrieve_groups()
}
```

## Information about Organizations

Official API documentation <https://dev.bitly.com/api-reference>.

The first method returns an information about myself.

```{r}
if (interactive()) {
  bitly_bearerToken("access token")
  bitly_user_info()
}
```

### Custom Bitlinks

```{r}
bitly_bearerToken("access token")
if (interactive()) {
  df <- data.frame(
    pubDate = rep("2016-02-10", 4),
    link = c(
      "https://www.google.com",
      "https://www.apple.com"
    )
  )
  df

  fin <- NULL
  for (p in 1:length(df$link)) {
    fin[[p]] <- bitly_create_bitlink(long_url = df$link[p])
  }
}
```

**WARNING:** This will proceed only with the two real links, not with the NA, NULL or an empty strings. These, however, will stop the flow of the code (i.e. are errors).

# Is.gd & V.gd

## Is.gd

```{r}
if (interactive()) {
  isgd_LinksShorten(longUrl = "https://www.google.com", showRequestURL = TRUE)
}
```

## V.gd

```{r}
if (interactive()) {
  vgd_LinksShorten(longUrl = "https://www.apple.com", showRequestURL = TRUE)
}
```
