IFTTT_Maker_PowerShell.psm1

Set-StrictMode -Version Latest
<#
    .SYNOPSIS
        Triggers an event on the IFTTT Maker Channel.
    .DESCRIPTION
        Send-Maker triggers an event on the IFTTT Maker Channel.
        The event can have up to 3 values that can be passed.
    .PARAMETER EventName
        The name of the event to trigger on the IFTTT Maker Channel.
    .PARAMETER Key
        The secret key you got from IFTTT for triggering events on the Maker Channel.
    .PARAMETER Value1
        First value passed to the event (ingredient Value1).
    .PARAMETER Value2
        Second value passed to the event (ingredient Value2).
    .PARAMETER Value3
        Third value passed to the event (ingredient Value3).
    .PARAMETER Save
        Defines if the current config should be saved.
    .PARAMETER Delete
        Deletes the current config if it exists.
#>

Function Send-IFTTTMaker
{    
    #region Params
    [CmdletBinding()]
    param
    (
        [ValidateNotNullOrEmpty()]
        [Parameter(Position=0,HelpMessage="The secret key you got from IFTTT for triggering events on the Maker Channel.")]
        [string] $Key,

        [ValidateNotNullOrEmpty()]
        [Parameter(Position=1,HelpMessage="The name of the event to trigger on the IFTTT Maker Channel")]
        [string] $EventName,
        
        [ValidateNotNullOrEmpty()]
        [Parameter(Position=2,ValueFromPipeline=$true,HelpMessage="First value passed to the event (ingredient Value1).")]
        [string] $Value1,
        
        [ValidateNotNullOrEmpty()]
        [Parameter(Position=3,HelpMessage="Second value passed to the event (ingredient Value2).")]
        [string] $Value2,
        
        [ValidateNotNullOrEmpty()]
        [Parameter(Position=4,HelpMessage="Third value passed to the event (ingredient Value3).")]
        [string] $Value3,
        
        [Parameter(HelpMessage="Defines if the current config should be saved.")]
        [switch]$Save,

        [Parameter(HelpMessage="Deletes the current config if it exists.")]
        [switch]$Delete
    )
    #endregion
    
    $ErrorActionPreference = "Stop" # Die if Exception!
    $script:Site="https://maker.ifttt.com/trigger/"
    $script:SaveFile="$env:APPDATA\Send-IFTTTMaker\info.json"
    
    class MakerObj
    {
        [String] $Key
        [String] $EventName
        [String] $Value1
        [String] $Value2
        [String] $Value3

        hidden [string] GetJson()
        {
            $Dic = @{}
            
            if($this.Value1 -ne "")
            {    
                $Dic.value1 = $this.Value1
                if($this.Value2 -ne "")
                { 
                    $Dic.value2 = $this.Value2
                    if($this.Value3 -ne "")
                    {
                        $Dic.value3 = $this.Value3
                    }
                }
            }
                
            return $Dic | ConvertTo-Json 
        }
        
        Delete()
        {
            If (Test-Path $script:SaveFile)
            {
                Write-Verbose "Deleteing config file"
                Remove-Item $script:SaveFile
            }
        }
        
        Load()
        {
            $SaveFolder = Split-Path -Path $script:SaveFile
            If (Test-Path $script:SaveFile)
            {
                Write-Verbose "Attempting to load config file..."
                $Config = Get-Content $script:SaveFile | ConvertFrom-Json
                
                if($Config.Key -ne "" -and $this.Key -eq "")             
                    {$this.Key = $Config.Key}
                
                if($Config.EventName -ne "" -and $this.EventName -eq "")
                    {$this.EventName = $Config.EventName}
                
                if($Config.Value1 -ne "" -and $this.Value1 -eq "")
                    {$this.Value1 = $Config.Value1}
                
                if($Config.Value2 -ne "" -and $this.Value2 -eq "")
                    {$this.Value2 = $Config.Value2}
                
                if($Config.Value3 -ne "" -and $this.Value3 -eq "")
                    {$this.Value3 = $Config.Value3}
                
                Write-Verbose "Successfully loaded config file."
            }
        }
        
        Save()
        {
            $JsonExport = [ordered]@{}
            $JsonExport.Value1= $this.Value1
            $JsonExport.Value2= $this.Value2
            $JsonExport.Value3= $this.Value3
            $JsonExport.Key = $this.Key
            $JsonExport.EventName = $this.EventName
            Write-Verbose "Saving config file..."
            $JsonExport | ConvertTo-Json | Set-Content $script:SaveFile
            Write-Verbose "Successfully saved config file."
        }
        
        [object] Send()
        {
            $MySite = $script:Site+$this.EventName+"/with/key/"+$this.Key
            $MyJson = $this.GetJson()
            return Invoke-WebRequest -Uri $MySite -Method Post -ContentType "application/json" -Body $MyJson
        }
        
        CheckValidity()
        {
            Write-Verbose "Validating input."
            if(-not($this.Key)){Throw New-Object System.ArgumentNullException "Key","Parameter must be provided."}
            if(-not($this.EventName)){Throw New-Object System.ArgumentNullException "EventName","Parameter must be provided."}
            Write-Verbose "Successfully validatied input."
        }
    }

    $NewMakerObj = New-Object MakerObj
    $NewMakerObj.Key = $Key
    $NewMakerObj.EventName = $EventName
    $NewMakerObj.Value1 = $Value1
    $NewMakerObj.Value2 = $Value2
    $NewMakerObj.Value3 = $Value3
    
    if($Delete)
    {
        $NewMakerObj.Delete()
        return $true
    }
    
    $NewMakerObj.Load()
    $NewMakerObj.CheckValidity()
    
    if($Save)
    {
        $NewMakerObj.Save()
        return $true
    }
    
    $response =$NewMakerObj.Send()
    if($VerbosePreference){return $response}
        return $response.Content
}