Skip to content

Instantly share code, notes, and snippets.

@JPRuskin
Last active September 9, 2015 13:31
Show Gist options
  • Save JPRuskin/99633b6f954c16e3b871 to your computer and use it in GitHub Desktop.
Save JPRuskin/99633b6f954c16e3b871 to your computer and use it in GitHub Desktop.
function Get-Deck {
param([int]$decks=1)
$return = Invoke-WebRequest -Uri "http://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=$decks"
if ($return.StatusCode -eq 200) {return $return.Content | ConvertFrom-Json}
else {break $return.StatusCode}
}
function Get-Cards {
param(
[Parameter(ValueFromPipelineByPropertyName,Mandatory)]
[string]$deck_id,
[int]$cards=1
)
$return = Invoke-WebRequest -Uri "http://deckofcardsapi.com/api/deck/$deck_id/draw/?count=$cards"
if (($return | ConvertFrom-Json).remaining -eq 0) {Write-Warning '0 cards remaining in deck. Shuffle with Shuffle-Deck!'}
elseif ($return.StatusCode -eq 200) {return ($return.Content | ConvertFrom-Json).Cards}
else {break $return.StatusCode}
}
function Shuffle-Deck {
param(
[Parameter(ValueFromPipelineByPropertyName,Mandatory)]
[string]$deck_id
)
$return = Invoke-WebRequest -Uri "http://deckofcardsapi.com/api/deck/shuffle/$deck_id/"
if ($return.StatusCode -eq 200 -and ($return.Content | ConvertFrom-Json).success -eq $true) {}
else {Write-Error "Shuffling $deck_id failed"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment