-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGimmeeBeer.psm1
137 lines (123 loc) · 4.59 KB
/
GimmeeBeer.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
$Script:settingsFile = 'GimmeeBeer.json'
function Set-GimmeeBeerSettings {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $false)]
[ValidateScript( {
if (![system.uri]::IsWellFormedUriString($_, [System.UriKind]::Absolute)) {
throw "GimmeeUrl is not a valid URL"
}
else { return $true }
})]
[string] $GimmeeUrl = $(throw "GimmeeUrl is mandatory, please provide a value."),
[Parameter(ValueFromPipeline = $false)]
[ValidateNotNullOrEmpty()]
[string] $GimeeAppId = $(throw "GimeeAppId is mandatory, please provide a value."),
[Parameter(ValueFromPipeline = $false)]
[ValidateNotNullOrEmpty()]
[string] $GimmeePw = $(throw "GimmeePw is mandatory, please provide a value."),
[Parameter(ValueFromPipeline = $false)]
[ValidateScript( {
if (![system.uri]::IsWellFormedUriString($_, [System.UriKind]::Absolute)) {
throw "GimmeeUrl is not a valid URL"
}
else { return $true }
})]
[string] $BreweryDbUrl = $(throw "BreweryDbUrl is mandatory, please provide a value."),
[Parameter(ValueFromPipeline = $false)]
[ValidateNotNullOrEmpty()]
[string] $BreweryDbApiKey = $(throw "BreweryDbApiKey is mandatory, please provide a value.")
)
$settings = [ordered]@{
GimmeeUrl = $GimmeeUrl
GimmeeAppId = $GimeeAppId
GimmeePw = $GimmeePw
BreweryDbUrl = $BreweryDbUrl
BreweryDbApiKey = $BreweryDbApiKey
}
$settings | ConvertTo-Json | Set-Content -Path "$PSScriptRoot\$Script:settingsFile" -Force
}
function Get-GimmeeBeerSettings {
[CmdletBinding()]
param ()
if (!(Test-Path "$PSScriptRoot\$Script:settingsFile")) {
throw 'Unable to locate Gimmee settings. Run Set-GimmeeBeerSettings to create.'
}
Get-Content -Path "$PSScriptRoot\$Script:settingsFile" | ConvertFrom-Json
}
function Get-GimmeeToken {
[CmdletBinding()]
param ()
$uri = "$Script:GimmeeUrl/auth"
$headers = @{
'Content-Type' = 'application/x-www-form-urlencoded'
}
$body = @{
appid = $Script:GimmeeAppId
pw = $Script:GimmeePw
}
$response = Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers
$response.access_token.Trim()
}
# https://documenter.getpostman.com/view/1383381/RzZ3KMKV#991072cb-ff87-4c45-83fd-98ab2bd1bff9
function Get-GimmeeBeers {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $false)]
[string] $Name
)
$settings = Get-GimmeeBeerSettings
$uri = "$($settings.GimmeeUrl)/beer/info"
$response = Invoke-RestMethod -Method Get -Uri $uri
# Transform response data
$beers = foreach ($beer in $response.Beers) {
$s = [ordered]@{
Category = $beer.category
Name = $beer.name
ShortName = $beer.shortName
Description = $beer.description
IbuMin = $beer.ibuMin
IbuMax = $beer.ibuMax
AbvMin = $beer.abvMin
AbvMax = $beer.abvMax
SrmMin = $beer.srmMin
SrmMax = $beer.srmMax
OgMin = $beer.ogMin
FgMin = $beer.fgMin
FgMax = $beer.fgMax
}
$b = [ordered]@{
Name = $beer.name
NameDisplay = $beer.nameDisplay
Description = $beer.description
Abv = $beer.abv
Ibu = $beer.ibu
Style = New-Object -TypeName psobject -Property $s
IsOrganic = $beer.isOrganic
IsRetired = $beer.isRetired
Status = $beer.status
StatusDisplay = $beer.statusDisplay
}
New-Object -TypeName psobject -Property $b
}
$data = [ordered]@{
CurrentPage = $response.currentPage
NumberOfPages = $response.numberOfPages
TotalResults = $response.totalResults
Beers = $beers
DataProvidedBy = $response.DataProvidedBy
TermsOfServiceUrl = $response.TermsOfServiceUrl
DataPullDateTime = $response.DataPullDateTime
GimmeeVersion = $response.GimmeeVersion
}
New-Object -TypeName psobject -Property $data
}
function Get-GimmeeBreweries {
[CmdletBinding()]
param ()
}
Export-ModuleMember -Function Set-GimmeeBeerSettings
Export-ModuleMember -Function Get-GimmeeBeerSettings
Export-ModuleMember -Function Get-GimmeeToken
Export-ModuleMember -Function Get-GimmeeBeers
Export-ModuleMember -Function Get-GimmeeBreweries