Skip to content
Browse files

Refactored query to a parameter

  • Loading branch information...
1 parent 202eaef commit 7f2f1f39ff24faba1e3c34e2b1e94c3948603fc0 @dfinke committed
Showing with 37 additions and 35 deletions.
  1. +37 −35 chapter06/NYTSemanticAPI.ps1
View
72 chapter06/NYTSemanticAPI.ps1
@@ -1,36 +1,38 @@
-$apiKey = "e91adfa87e8f8026712c4d92b54a0b14:0:39364737"
-
-function Get-SemanticNYT {
-
- param($query = "obama")
-
- $uri = "http://api.nytimes.com/svc/semantic/v2/"+
- "concept/search.json?query=$query&api-key=$apiKey"
-
- (Invoke-RestMethod $uri).results
-}
-
-function Get-SemanticNYTArticles {
-
- param(
- [Parameter(ValueFromPipelineByPropertyName=$true)]
- $concept_name,
- [Parameter(ValueFromPipelineByPropertyName=$true)]
- $concept_type
- )
-
- process {
- $uri = "http://api.nytimes.com/svc/semantic/v2/" +
- "concept/name/$concept_type/$concept_name.json?&" +
- "fields=all&api-key=$apiKey"
-
- (Invoke-RestMethod $uri).results
- }
-}
-
-Get-SemanticNYT "Obama" |
- Get-SemanticNYTArticles |
- Where links |
- Select -ExpandProperty article_list |
- Select -ExpandProperty results |
+param($query = "Obama")
+
+$apiKey = "e91adfa87e8f8026712c4d92b54a0b14:0:39364737"
+
+function Get-SemanticNYT {
+
+ param($query)
+
+ $uri = "http://api.nytimes.com/svc/semantic/v2/"+
+ "concept/search.json?query=$query&api-key=$apiKey"
+
+ (Invoke-RestMethod $uri).results
+}
+
+function Get-SemanticNYTArticles {
+
+ param(
+ [Parameter(ValueFromPipelineByPropertyName=$true)]
+ $concept_name,
+ [Parameter(ValueFromPipelineByPropertyName=$true)]
+ $concept_type
+ )
+
+ process {
+ $uri = "http://api.nytimes.com/svc/semantic/v2/" +
+ "concept/name/$concept_type/$concept_name.json?&" +
+ "fields=all&api-key=$apiKey"
+
+ (Invoke-RestMethod $uri).results
+ }
+}
+
+Get-SemanticNYT $query |
+ Get-SemanticNYTArticles |
+ Where links |
+ Select -ExpandProperty article_list |
+ Select -ExpandProperty results |
Select date, title, url| Out-GridView

0 comments on commit 7f2f1f3

Please sign in to comment.
Something went wrong with that request. Please try again.