Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve language negotiation #196

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rowan/projects/Hyperspace.ston
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ RwLoadSpecificationV2 {
#specName: 'Hyperspace',
#projectName : 'Hyperspace',
#gitUrl : 'https://github.com/ba-st/Hyperspace.git',
#revision : 'v5',
#revision : 'v6',
#projectSpecFile : 'rowan/project.ston',
#componentNames : [
'Deployment',
Expand Down
2 changes: 1 addition & 1 deletion rowan/projects/JRPC.ston
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ RwLoadSpecificationV2 {
#specName: 'JRPC',
#projectName : 'JRPC',
#gitUrl : 'https://github.com/ba-st-dependencies/JRPC.git',
#revision : 'v4',
#revision : 'v5',
#projectSpecFile : 'rowan/project.ston',
#componentNames : [
'Deployment'
Expand Down
2 changes: 1 addition & 1 deletion rowan/projects/JSONWebToken.ston
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ RwLoadSpecificationV2 {
#specName: 'JSONWebToken',
#projectName : 'JSONWebToken',
#gitUrl : 'https://github.com/ba-st-dependencies/JSONWebToken.git',
#revision : 'v1',
#revision : 'v2',
#projectSpecFile : 'rowan/project.ston',
#componentNames : [
'Deployment'
Expand Down
2 changes: 1 addition & 1 deletion rowan/projects/Launchpad.ston
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ RwLoadSpecificationV2 {
#specName: 'Launchpad',
#projectName : 'Launchpad',
#gitUrl : 'https://github.com/ba-st/Launchpad.git',
#revision : 'v5',
#revision : 'v6',
#projectSpecFile : 'rowan/project.ston',
#componentNames : [
'Deployment',
Expand Down
2 changes: 1 addition & 1 deletion rowan/projects/SortFunctions.ston
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ RwLoadSpecificationV2 {
#specName: 'SortFunctions',
#projectName : 'SortFunctions',
#gitUrl : 'https://github.com/ba-st-dependencies/SortFunctions.git',
#revision : 'v1',
#revision : 'v2',
#projectSpecFile : 'rowan/project.ston',
#componentNames : [
'Deployment'
Expand Down
2 changes: 1 addition & 1 deletion rowan/projects/Teapot.ston
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ RwLoadSpecificationV2 {
#specName: 'Teapot',
#projectName : 'Teapot',
#gitUrl : 'https://github.com/ba-st-dependencies/Teapot.git',
#revision : 'v2',
#revision : 'v3',
#projectSpecFile : 'rowan/project.ston',
#componentNames : [
'Deployment'
Expand Down
12 changes: 6 additions & 6 deletions source/BaselineOfStargate/BaselineOfStargate.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,32 @@ BaselineOfStargate >> setUpCorePackages: spec [
BaselineOfStargate >> setUpDependencies: spec [

spec
baseline: 'Buoy' with: [ spec repository: 'github://ba-st/Buoy:v7' ];
baseline: 'Buoy' with: [ spec repository: 'github://ba-st/Buoy:v8' ];
project: 'Buoy-Deployment' copyFrom: 'Buoy' with: [ spec loads: 'Deployment' ];
project: 'Buoy-SUnit' copyFrom: 'Buoy' with: [ spec loads: 'Dependent-SUnit-Extensions' ].

spec
baseline: 'Hyperspace' with: [ spec repository: 'github://ba-st/Hyperspace:v5' ];
baseline: 'Hyperspace' with: [ spec repository: 'github://ba-st/Hyperspace:v6' ];
project: 'Hyperspace-Deployment' copyFrom: 'Hyperspace' with: [ spec loads: 'Deployment' ];
project: 'Hyperspace-Tools' copyFrom: 'Hyperspace' with: [ spec loads: 'Tools' ];
project: 'Hyperspace-SUnit'
copyFrom: 'Hyperspace'
with: [ spec loads: 'Dependent-SUnit-Extensions' ].

spec
baseline: 'Teapot' with: [ spec repository: 'github://ba-st-dependencies/Teapot:v2' ];
baseline: 'Teapot' with: [ spec repository: 'github://ba-st-dependencies/Teapot:v3' ];
project: 'Teapot-Deployment' copyFrom: 'Teapot' with: [ spec loads: 'Deployment' ].

spec
baseline: 'JSONWebToken' with: [ spec repository: 'github://ba-st-dependencies/JSONWebToken:v1' ];
baseline: 'JSONWebToken' with: [ spec repository: 'github://ba-st-dependencies/JSONWebToken:v2' ];
project: 'JSONWebToken-Deployment' copyFrom: 'JSONWebToken' with: [ spec loads: 'SSL' ].

spec
baseline: 'JRPC' with: [ spec repository: 'github://ba-st-dependencies/JRPC:v4' ];
baseline: 'JRPC' with: [ spec repository: 'github://ba-st-dependencies/JRPC:v5' ];
project: 'JRPC-Deployment' copyFrom: 'JRPC' with: [ spec loads: 'Server-Deployment' ].

spec
baseline: 'Launchpad' with: [ spec repository: 'github://ba-st/Launchpad:v5' ];
baseline: 'Launchpad' with: [ spec repository: 'github://ba-st/Launchpad:v6' ];
project: 'Launchpad-Deployment' copyFrom: 'Launchpad' with: [ spec loads: 'Deployment' ];
project: 'Launchpad-SUnit' copyFrom: 'Launchpad' with: [ spec loads: 'Dependent-SUnit-Extensions' ]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ SouthAmericanCurrenciesRESTfulControllerTest >> languageRange [
^ 'de,en;q=0.5,es;q=0.6'
]

{ #category : 'private' }
SouthAmericanCurrenciesRESTfulControllerTest >> performTest [

self
use: [ :translator |
translator
translationFor: 'Argentine peso' in: 'es-AR' is: 'Peso';
translationFor: 'Bolivian boliviano' in: 'es-AR' is: 'Boliviano';
translationFor: 'Brazilian real' in: 'es-AR' is: 'Real brasileño';
translationFor: 'Chilean peso' in: 'es-AR' is: 'Peso chileno';
translationFor: 'Colombian peso' in: 'es-AR' is: 'Peso colombiano';
translationFor: 'Guyanese dollar' in: 'es-AR' is: 'Dólar guyanés';
translationFor: 'Paraguayan guaraní' in: 'es-AR' is: 'Guaraní';
translationFor: 'Peruvian sol' in: 'es-AR' is: 'Sol';
translationFor: 'Surinamese dollar' in: 'es-AR' is: 'Dólar surinamés';
translationFor: 'Uruguayan peso' in: 'es-AR' is: 'Peso uruguayo';
translationFor: 'Venezuelan bolívar soberano' in: 'es-AR' is: 'Bolívar';
translationFor: '{1} was not found in the system'
in: 'es-AR'
is: 'No se encontró {1} en el sistema'
]
asNaturalLanguageTranslatorDuring: [ super performTest ]
]

{ #category : 'running' }
SouthAmericanCurrenciesRESTfulControllerTest >> setUpResourceController [

Expand All @@ -54,7 +78,7 @@ SouthAmericanCurrenciesRESTfulControllerTest >> spanish [
{ #category : 'private - language ranges' }
SouthAmericanCurrenciesRESTfulControllerTest >> supportedLanguages [

^ Array with: self argentineSpanish with: self unitedStatesEnglish
^ { self argentineSpanish . self unitedStatesEnglish }
]

{ #category : 'tests' }
Expand Down Expand Up @@ -447,14 +471,36 @@ SouthAmericanCurrenciesRESTfulControllerTest >> testGetCurrenciesInSpanish [
{ #category : 'tests' }
SouthAmericanCurrenciesRESTfulControllerTest >> testGetCurrencyNotFoundShouldFail [

self
should: [
resourceController
currencyBasedOn: ( self requestToGETResourceIdentifiedBy: 'ZZZ' accepting: '*/*' )
within: self newHttpRequestContext
]
raise: HTTPClientError
withExceptionDo: [ :error | self assert: error code equals: 404 ]
self
should: [
resourceController
currencyBasedOn: ( self requestToGETResourceIdentifiedBy: 'ZZZ' accepting: '*/*' )
within: self newHttpRequestContext
]
raise: HTTPClientError
withExceptionDo: [ :error |
self
assert: error code equals: 404;
assert: error messageText equals: 'ZZZ was not found in the system'
]
]

{ #category : 'tests' }
SouthAmericanCurrenciesRESTfulControllerTest >> testGetCurrencyNotFoundShouldFailInSpanish [

self
should: [
| request |
request := self requestToGETResourceIdentifiedBy: 'ZZZ' accepting: '*/*'.
request setAcceptLanguage: self spanish.
resourceController currencyBasedOn: request within: self newHttpRequestContext
]
raise: HTTPClientError
withExceptionDo: [ :error |
self
assert: error code equals: 404;
assert: error messageText equals: 'No se encontró ZZZ en el sistema'
]
]

{ #category : 'tests' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Class {
'banknotesByCurrency',
'banknotesRequestHandler',
'currenciesRequestHandler',
'currenciesByLanguage'
'currencies'
],
#category : 'Stargate-Examples-Currencies',
#package : 'Stargate-Examples',
Expand All @@ -27,21 +27,7 @@ SouthAmericanCurrenciesRESTfulController >> banknotesTemplate [
{ #category : 'API' }
SouthAmericanCurrenciesRESTfulController >> currenciesBasedOn: httpRequest within: requestContext [

^ currenciesRequestHandler
from: httpRequest
within: requestContext
getCollection: [ self currenciesInLanguageAt: requestContext ]
]

{ #category : 'private' }
SouthAmericanCurrenciesRESTfulController >> currenciesInLanguageAt: requestContext [

| targetLanguage |

targetLanguage := [ requestContext targetLanguageTag ]
on: NotFound
do: [ :error | error return: 'en-US' asLanguageTag ].
^ currenciesByLanguage at: targetLanguage
^ currenciesRequestHandler from: httpRequest within: requestContext getCollection: [ currencies ]
]

{ #category : 'API' }
Expand All @@ -56,14 +42,12 @@ SouthAmericanCurrenciesRESTfulController >> currencyBanknotesBasedOn: httpReques
{ #category : 'API' }
SouthAmericanCurrenciesRESTfulController >> currencyBasedOn: httpRequest within: requestContext [

^ currenciesRequestHandler
from: httpRequest
within: requestContext
get: [ :isoCode |
( self currenciesInLanguageAt: requestContext )
detect: [ :currency | currency isoCode = isoCode ]
ifNone: [ NotFound signal ]
]
^ currenciesRequestHandler from: httpRequest within: requestContext get: [ :isoCode |
currencies
detect: [ :currency | currency isoCode = isoCode ]
ifNone: [
NotFound signal: ( '{1} was not found in the system' localizedWithAll: { isoCode } ) ]
]
]

{ #category : 'private' }
Expand Down Expand Up @@ -158,66 +142,58 @@ SouthAmericanCurrenciesRESTfulController >> initializeBanknotesRequestHandler [
{ #category : 'initialization' }
SouthAmericanCurrenciesRESTfulController >> initializeCurrencies [

currenciesByLanguage := Dictionary new
at: 'en-US' asLanguageTag
put:
( OrderedCollection new
add: ( self currencyNamed: 'Argentine peso' symbol: '$' isoCode: 'ARS' );
add: ( self currencyNamed: 'Bolivian boliviano' symbol: 'Bs.' isoCode: 'BOB' );
add: ( self currencyNamed: 'Brazilian real' symbol: 'R$' isoCode: 'BRL' );
add: ( self currencyNamed: 'Chilean peso' symbol: '$' isoCode: 'CLP' );
add: ( self currencyNamed: 'Colombian peso' symbol: '$' isoCode: 'COP' );
add: ( self currencyNamed: 'Guyanese dollar' symbol: '$' isoCode: 'GYD' );
add: ( self currencyNamed: 'Paraguayan guaraní' symbol: '₲' isoCode: 'PYG' );
add: ( self currencyNamed: 'Peruvian sol' symbol: 'S/.' isoCode: 'PEN' );
add: ( self currencyNamed: 'Surinamese dollar' symbol: '$' isoCode: 'SRD' );
add: ( self currencyNamed: 'Uruguayan peso' symbol: '$' isoCode: 'UYU' );
add: ( self currencyNamed: 'Venezuelan bolívar soberano' symbol: 'Bs.S' isoCode: 'VES' );
asArray );
at: 'es-AR' asLanguageTag
put:
( OrderedCollection new
add: ( self currencyNamed: 'Peso' symbol: '$' isoCode: 'ARS' );
add: ( self currencyNamed: 'Boliviano' symbol: 'Bs.' isoCode: 'BOB' );
add: ( self currencyNamed: 'Real brasileño' symbol: 'R$' isoCode: 'BRL' );
add: ( self currencyNamed: 'Peso chileno' symbol: '$' isoCode: 'CLP' );
add: ( self currencyNamed: 'Peso colombiano' symbol: '$' isoCode: 'COP' );
add: ( self currencyNamed: 'Dólar guyanés' symbol: '$' isoCode: 'GYD' );
add: ( self currencyNamed: 'Guaraní' symbol: '₲' isoCode: 'PYG' );
add: ( self currencyNamed: 'Sol' symbol: 'S/.' isoCode: 'PEN' );
add: ( self currencyNamed: 'Dólar surinamés' symbol: '$' isoCode: 'SRD' );
add: ( self currencyNamed: 'Peso uruguayo' symbol: '$' isoCode: 'UYU' );
add: ( self currencyNamed: 'Bolívar' symbol: 'Bs.S' isoCode: 'VES' );
asArray );
yourself
currencies := OrderedCollection new
add: ( self currencyNamed: 'Argentine peso' symbol: '$' isoCode: 'ARS' );
add: ( self currencyNamed: 'Bolivian boliviano' symbol: 'Bs.' isoCode: 'BOB' );
add: ( self currencyNamed: 'Brazilian real' symbol: 'R$' isoCode: 'BRL' );
add: ( self currencyNamed: 'Chilean peso' symbol: '$' isoCode: 'CLP' );
add: ( self currencyNamed: 'Colombian peso' symbol: '$' isoCode: 'COP' );
add: ( self currencyNamed: 'Guyanese dollar' symbol: '$' isoCode: 'GYD' );
add: ( self currencyNamed: 'Paraguayan guaraní' symbol: '₲' isoCode: 'PYG' );
add: ( self currencyNamed: 'Peruvian sol' symbol: 'S/.' isoCode: 'PEN' );
add: ( self currencyNamed: 'Surinamese dollar' symbol: '$' isoCode: 'SRD' );
add: ( self currencyNamed: 'Uruguayan peso' symbol: '$' isoCode: 'UYU' );
add:
( self
currencyNamed: 'Venezuelan bolívar soberano'
symbol: 'Bs.S'
isoCode: 'VES' );
asArray
]

{ #category : 'initialization' }
SouthAmericanCurrenciesRESTfulController >> initializeCurrenciesRequestHandler [

currenciesRequestHandler := RESTfulRequestHandlerBuilder new
handling: 'currencies'
extractingIdentifierWith: [ :httpRequest | self identifierIn: httpRequest ];
whenResponding: self currencyVersion1dot0dot0MediaType
encodeToJsonApplying:
[ :resource :requestContext :writer | writer for: StargateCurrency do: [ :mapping | mapping mapInstVars ] ];
createEntityTagHashing: [ :hasher :currency :requestContext |
hasher
include: currency name;
include: currency symbol;
include: currency isoCode
];
handleExceptionsApplying: [ :handler | handler addAsNotFoundError: NotFound ];
directCachingWith: [ :caching |
caching
beImmutable;
when: [ :response :resource | resource isCollection ] apply: [ caching beStaleAfter: 1 day ];
when: [ :response :resource | resource isCollection not and: [ resource symbol = '$' ] ]
apply: [ caching bePublic ]
];
addAsSupportedLanguage: 'en-US';
addAsSupportedLanguage: 'es-AR';
build
currenciesRequestHandler := RESTfulRequestHandlerBuilder new
handling: 'currencies'
extractingIdentifierWith: [ :httpRequest |
self identifierIn: httpRequest ];
whenResponding: self currencyVersion1dot0dot0MediaType
encodeToJsonApplying: [ :resource :requestContext :writer |
writer
for: StargateCurrency
do: [ :mapping |
mapping mapAccessors: #( #symbol #isoCode #name ) ] ];
createEntityTagHashing: [ :hasher :currency :requestContext |
hasher
include: currency name;
include: currency symbol;
include: currency isoCode
];
handleExceptionsApplying: [ :handler |
handler addAsNotFoundError: NotFound ];
directCachingWith: [ :caching |
caching
beImmutable;
when: [ :response :resource | resource isCollection ]
apply: [ caching beStaleAfter: 1 day ];
when: [ :response :resource |
resource isCollection not and: [ resource symbol = '$' ] ]
apply: [ caching bePublic ]
];
addAsSupportedLanguage: 'en-US';
addAsSupportedLanguage: 'es-AR';
build
]

{ #category : 'private' }
Expand Down
2 changes: 1 addition & 1 deletion source/Stargate-Examples/StargateCurrency.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ StargateCurrency >> isoCode [
{ #category : 'accessing' }
StargateCurrency >> name [

^ name
^ name localized
]

{ #category : 'accessing' }
Expand Down
6 changes: 6 additions & 0 deletions source/Stargate-Model/HttpRequestContext.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,9 @@ HttpRequestContext >> withTargetLanguageTagDo: aBlock [

knownObjects at: #targetLanguageTag ifPresent: aBlock
]

{ #category : 'accessing' }
HttpRequestContext >> withTargetLanguageTagDo: aBlock ifNone: noneBlock [

^ knownObjects at: #targetLanguageTag ifPresent: aBlock ifAbsent: noneBlock
]
Loading