Skip to content
This repository has been archived by the owner on Jul 10, 2018. It is now read-only.

Commit

Permalink
Merge e1990cd into 17a294c
Browse files Browse the repository at this point in the history
  • Loading branch information
mtlynch committed Jun 23, 2018
2 parents 17a294c + e1990cd commit 05cf1a3
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 36 deletions.
9 changes: 8 additions & 1 deletion src/app/_models/parse-result.ts
Expand Up @@ -6,9 +6,16 @@ export class IngredientParsed {
public preparationNotes?: string) {}
}

export class ParseResult {
export class IngredientResult {
constructor(
public ingredientRaw?: string,
public ingredientParsed?: IngredientParsed,
public error?: string) {}
}

export class ParseResults {
constructor(
public results?: IngredientResult[],
public error?: string,
public requestsRemaining?: number) {}
}
20 changes: 14 additions & 6 deletions src/app/_pipes/curl-cmd.pipe.spec.ts
Expand Up @@ -11,16 +11,24 @@ describe('CurlCmdPipe', () => {
it('should generate curl command', () => {
expect(pipe.transform('2 lbs carrots', 'http://dummyurl.com')).toBe(`
curl \\
--get \\
--data-urlencode 'q=2 lbs carrots' \\
"http://dummyurl.com/v1/parse"`.trim());
--header "Content-Type: application/json" \\
--data '{
"ingredients": [
"2 lbs carrots"
]
}' \\
"http://dummyurl.com/parseIngredients"`.trim());
});

it('should escape single quotes', () => {
expect(pipe.transform('2 cups tomatoes (don\'t chop them up; you won\'t like it)', 'http://dummyurl.com')).toBe(`
curl \\
--get \\
--data-urlencode 'q=2 cups tomatoes (don\\\'t chop them up; you won\\\'t like it)' \\
"http://dummyurl.com/v1/parse"`.trim());
--header "Content-Type: application/json" \\
--data '{
"ingredients": [
"2 cups tomatoes (don\\\'t chop them up; you won\\\'t like it)"
]
}' \\
"http://dummyurl.com/parseIngredients"`.trim());
});
});
10 changes: 7 additions & 3 deletions src/app/_pipes/curl-cmd.pipe.ts
Expand Up @@ -9,9 +9,13 @@ export class CurlCmdPipe implements PipeTransform {
const ingredientEscaped = ingredientRaw.replace(/'/g, '\\\'');
return `
curl \\
--get \\
--data-urlencode 'q=${ingredientEscaped}' \\
"${backendUrl}/v1/parse"
--header "Content-Type: application/json" \\
--data '{
"ingredients": [
"${ingredientEscaped}"
]
}' \\
"${backendUrl}/parseIngredients"
`.trim();
}

Expand Down
36 changes: 22 additions & 14 deletions src/app/_services/parser.service.spec.ts
Expand Up @@ -3,9 +3,9 @@ import { HttpRequest, HttpParams } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { environment } from '../../environments/environment';

import { ParserService } from './parser.service';
import { ParserService, ParseRequest } from './parser.service';
import { Parser } from '@angular/compiler/src/ml_parser/parser';
import { ParseResult, IngredientParsed } from '../_models/parse-result';
import { ParseResults, IngredientParsed } from '../_models/parse-result';

describe('ParserService', () => {
let service;
Expand All @@ -30,22 +30,30 @@ describe('ParserService', () => {

it('should return the parse results', () => {
const mockResult = {
ingredientParsed: {
quantity: 2.0,
unit: 'tablespoon',
product: 'Parmesan cheese',
preparationNotes: 'shaved',
},
results: [
{
ingredientRaw: '2 tablespoons shaved Parmesan cheese',
ingredientParsed: {
quantity: 2.0,
unit: 'tablespoon',
product: 'Parmesan cheese',
preparationNotes: 'shaved',
},
error: null,
},
],
};

service.parseIngredient('2 tablespoons shaved Parmesan cheese').subscribe((parseResult) => {
expect(parseResult).toEqual(mockResult);
});
service.parseIngredients({ ingredients: ['2 tablespoons shaved Parmesan cheese'] })
.subscribe((parseResult) => {
expect(parseResult).toEqual(mockResult);
});

httpMock.expectOne((request: HttpRequest<any>) => {
return request.method === 'GET'
&& request.url === `${environment.backendBaseUrl}/v1/parse`
&& request.params.get('q') === '2 tablespoons shaved Parmesan cheese';
return request.method === 'POST'
&& request.url === `${environment.backendBaseUrl}/parseIngredients`
&& request.body.ingredients.length === 1
&& request.body.ingredients[0] === '2 tablespoons shaved Parmesan cheese';
}).flush(mockResult);
});
});
23 changes: 17 additions & 6 deletions src/app/_services/parser.service.ts
@@ -1,18 +1,29 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { environment } from '../../environments/environment';
import { ParseResult } from '../_models/parse-result';
import { ParseResults } from '../_models/parse-result';

export class ParseRequest {
constructor(
public ingredients?: string[],
) {}
}

@Injectable()
export class ParserService {

constructor(private http:HttpClient) { }

public parseIngredient(ingredientRaw: string) : Observable<ParseResult> {
return this.http.get<ParseResult>(`${environment.backendBaseUrl}/v1/parse`, {
params: new HttpParams().set('q', ingredientRaw),
});
public parseIngredients(request: ParseRequest) : Observable<ParseResults> {

const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
}),
};
return this.http.post<ParseResults>(
`${environment.backendBaseUrl}/parseIngredients`, request, httpOptions);
}

}
4 changes: 2 additions & 2 deletions src/app/public/demo/demo.component.spec.ts
Expand Up @@ -5,7 +5,7 @@ import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { HttpClientTestingModule } from '@angular/common/http/testing';

import { ParseResult } from '../../_models/parse-result';
import { ParseResults } from '../../_models/parse-result';
import { ParserService } from '../../_services/parser.service';
import { CurlCmdPipe } from '../../_pipes/curl-cmd.pipe';

Expand All @@ -15,7 +15,7 @@ import { DemoComponent } from './demo.component';
export class MockParserService {

public parseIngredient(ingredientRaw: string) {
return Observable.of<ParseResult>({});
return Observable.of<ParseResults>({});
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/app/public/demo/demo.component.ts
@@ -1,8 +1,8 @@
import { Component, OnInit } from '@angular/core';
import { environment } from '../../../environments/environment';

import { IngredientParsed, ParseResult } from '../../_models/parse-result';
import { ParserService } from '../../_services/parser.service';
import { IngredientParsed, ParseResults } from '../../_models/parse-result';
import { ParserService, ParseRequest } from '../../_services/parser.service';
import { CurlCmdPipe } from '../../_pipes/curl-cmd.pipe';

@Component({
Expand All @@ -26,7 +26,8 @@ export class DemoComponent implements OnInit {
this.isWaitingForParseResult = true;
this.error = null;
this.ingredientParsed = null;
this.parserService.parseIngredient(raw).subscribe(
const request = new ParseRequest([raw]);
this.parserService.parseIngredients(request).subscribe(
(response) => {
this.isWaitingForParseResult = false;
this.curlExample = this.curlCmdPipe.transform(raw, environment.backendBaseUrl);
Expand All @@ -35,7 +36,7 @@ export class DemoComponent implements OnInit {
this.error = parseResult.error;
this.ingredientParsed = null;
} else {
this.ingredientParsed = parseResult.ingredientParsed;
this.ingredientParsed = parseResult.results[0].ingredientParsed;
this.requestsRemaining = parseResult.requestsRemaining;
this.error = null;
}
Expand Down

0 comments on commit 05cf1a3

Please sign in to comment.