Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit b1b7bb2

Browse files
authored
Merge pull request #71 from BuffettCode/development
Release v11
2 parents 61d19bb + bbf278c commit b1b7bb2

36 files changed

Lines changed: 602 additions & 117 deletions

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ npm run deploy:prod
6464
1. Edit `src/version.ts`.
6565
2. Merge development branch into master.
6666
3. Create a release on github.
67-
3. Create Deployments from `Publish` -> `Deploy from manifest` in script editor for prodcution.
67+
4. Run `git checkout master && npm run deploy:prod` to deploy master branch.
68+
5. Create Deployments from `Publish` -> `Deploy from manifest` in script editor for prodcution.

src/__mocks__/fixtures/v3/quarter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ module.exports = {
182182
employee_num: null,
183183
net_sales_per_employee: null,
184184
operating_income_per_employee: null,
185-
segment_member: null,
185+
segment_member: {"segments":[],"periods":[],"values":[]},
186186
increase_in_properties: 346000000,
187187
r_and_d_expenses: 35000000,
188188
defined_benefit_asset: null,

src/api/company-service.test.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import { CompanyService } from '~/api/company-service'
22
import { CachingBuffettCodeApiClientV2 } from '~/api/v2/caching-client'
33
import { CachingBuffettCodeApiClientV3 } from '~/api/v3/caching-client'
44
import { DateParam } from '~/fiscal-periods/date-param'
5+
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
6+
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
57
import { YearQuarter } from '~/fiscal-periods/year-quarter'
68
import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'
79

10+
const LY = new LyWithOffset()
11+
const LQ = new LqWithOffset()
12+
813
jest.mock('~/api/v2/client', () =>
914
jest.requireActual('~/__mocks__/api/v2/client')
1015
)
@@ -21,6 +26,55 @@ test('isSupportedTicker', () => {
2126
expect(new CompanyService('9999', client).isSupportedTicker()).toBe(false)
2227
})
2328

29+
test('convertYearQuarterParamToYearQuarter', () => {
30+
const client = new CachingBuffettCodeApiClientV2('token')
31+
const service = new CompanyService('2371', client)
32+
expect(
33+
service.convertYearQuarterParamToYearQuarter(new YearQuarterParam(LY, 3))
34+
).toEqual(new YearQuarter(2021, 3))
35+
expect(
36+
service.convertYearQuarterParamToYearQuarter(new YearQuarterParam(2016, LQ))
37+
).toEqual(new YearQuarter(2016, 2))
38+
expect(
39+
service.convertYearQuarterParamToYearQuarter(new YearQuarterParam(LY, LQ))
40+
).toEqual(new YearQuarter(2021, 2))
41+
expect(
42+
service.convertYearQuarterParamToYearQuarter(
43+
new YearQuarterParam(new LyWithOffset(-5), LQ)
44+
)
45+
).toEqual(new YearQuarter(2016, 2))
46+
expect(
47+
service.convertYearQuarterParamToYearQuarter(
48+
new YearQuarterParam(LY, new LqWithOffset(-1))
49+
)
50+
).toEqual(new YearQuarter(2021, 1))
51+
expect(
52+
service.convertYearQuarterParamToYearQuarter(
53+
new YearQuarterParam(LY, new LqWithOffset(-2))
54+
)
55+
).toEqual(new YearQuarter(2020, 4))
56+
expect(
57+
service.convertYearQuarterParamToYearQuarter(
58+
new YearQuarterParam(LY, new LqWithOffset(-3))
59+
)
60+
).toEqual(new YearQuarter(2020, 3))
61+
expect(
62+
service.convertYearQuarterParamToYearQuarter(
63+
new YearQuarterParam(LY, new LqWithOffset(-4))
64+
)
65+
).toEqual(new YearQuarter(2020, 2))
66+
expect(
67+
service.convertYearQuarterParamToYearQuarter(
68+
new YearQuarterParam(LY, new LqWithOffset(-5))
69+
)
70+
).toEqual(new YearQuarter(2020, 1))
71+
expect(
72+
service.convertYearQuarterParamToYearQuarter(
73+
new YearQuarterParam(new LyWithOffset(-5), new LqWithOffset(-5))
74+
)
75+
).toEqual(new YearQuarter(2015, 1))
76+
})
77+
2478
test('isOndemandQuarterApiPeriod', () => {
2579
const client = new CachingBuffettCodeApiClientV2('token')
2680
const service = new CompanyService('2371', client)
@@ -34,9 +88,30 @@ test('isOndemandQuarterApiPeriod', () => {
3488
expect(service.isOndemandQuarterApiPeriod(new YearQuarter(2016, 3))).toBe(
3589
false
3690
)
91+
expect(service.isOndemandQuarterApiPeriod(new YearQuarterParam(LY, 3))).toBe(
92+
false
93+
)
94+
expect(
95+
service.isOndemandQuarterApiPeriod(new YearQuarterParam(2016, LQ))
96+
).toBe(true)
97+
expect(service.isOndemandQuarterApiPeriod(new YearQuarterParam(LY, LQ))).toBe(
98+
false
99+
)
37100
expect(
38-
service.isOndemandQuarterApiPeriod(new YearQuarterParam('LY', 'LQ'))
101+
service.isOndemandQuarterApiPeriod(
102+
new YearQuarterParam(new LyWithOffset(-4), new LqWithOffset(-3))
103+
)
39104
).toBe(false)
105+
expect(
106+
service.isOndemandQuarterApiPeriod(
107+
new YearQuarterParam(new LyWithOffset(-4), new LqWithOffset(-4))
108+
)
109+
).toBe(true)
110+
expect(
111+
service.isOndemandQuarterApiPeriod(
112+
new YearQuarterParam(new LyWithOffset(-5), LQ)
113+
)
114+
).toBe(true)
40115
})
41116

42117
test('isOndemandDailyApiPeriod', () => {

src/api/company-service.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { BuffettCodeApiClientV2 } from '~/api/v2/client'
22
import { BuffettCodeApiClientV3 } from '~/api/v3/client'
33
import { DateParam } from '~/fiscal-periods/date-param'
4+
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
5+
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
46
import { YearQuarter } from '~/fiscal-periods/year-quarter'
57
import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'
68

@@ -19,20 +21,38 @@ export class CompanyService {
1921
return !!this.company
2022
}
2123

24+
public convertYearQuarterParamToYearQuarter(
25+
period: YearQuarterParam
26+
): YearQuarter {
27+
if (period.year instanceof LyWithOffset) {
28+
period.year = this.company['latest_fiscal_year'] + period.year.offset
29+
}
30+
31+
if (period.quarter instanceof LqWithOffset) {
32+
period.year =
33+
(period.year as number) + Math.ceil(period.quarter.offset / 4)
34+
period.quarter =
35+
this.company['latest_fiscal_quarter'] + (period.quarter.offset % 4)
36+
37+
if (period.quarter <= 0) {
38+
period.year -= 1
39+
period.quarter = 4 + (period.quarter as number)
40+
}
41+
}
42+
43+
return period.toYearQuarter()
44+
}
45+
2246
public isOndemandQuarterApiPeriod(
2347
_period: YearQuarter | YearQuarterParam
2448
): boolean {
2549
if (!this.isSupportedTicker()) {
2650
throw new Error('unsupported ticker')
2751
}
2852

29-
let period
53+
let period: YearQuarter
3054
if (_period instanceof YearQuarterParam) {
31-
if (_period.isLatestYear() && _period.isLatestQuarter()) {
32-
return false
33-
}
34-
35-
period = _period.toYearQuarter()
55+
period = this.convertYearQuarterParamToYearQuarter(_period)
3656
} else {
3757
period = _period
3858
}

src/api/http-error.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { useMockedUrlFetchApp } from '~/api/test-helper'
33

44
test('HttpError', () => {
55
const res = useMockedUrlFetchApp(403, '{"message": "Forbidden"}')()
6-
const error = new HttpError(res)
6+
const error = new HttpError('https://example.com', res)
77
expect(error instanceof HttpError).toBeTruthy()
88
})

src/api/http-error.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ export class HttpError implements Error {
22
public name = 'HttpError'
33
public message: string
44

5-
// eslint-disable-next-line @typescript-eslint/camelcase
6-
constructor(public response: GoogleAppsScript.URL_Fetch.HTTPResponse) {
7-
this.message = `${response.getResponseCode()}: ${response.getContentText()}`
5+
constructor(
6+
public url: string,
7+
public response: GoogleAppsScript.URL_Fetch.HTTPResponse // eslint-disable-line @typescript-eslint/camelcase
8+
) {
9+
this.message = `${url} - ${response.getResponseCode()}: ${response.getContentText()}`
810
}
911

1012
public isInvalidTestingRequest(): boolean {

src/api/v2/caching-client.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { CompanyCache } from '~/__mocks__/services/company-cache'
22
import { QuarterCache } from '~/__mocks__/services/quarter-cache'
33
import { CachingBuffettCodeApiClientV2 } from '~/api/v2/caching-client'
4+
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
5+
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
46
import { YearQuarter } from '~/fiscal-periods/year-quarter'
57
import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'
68
import { IndicatorCache } from '~/services/indicator-cache'
@@ -18,6 +20,9 @@ jest.mock('~/services/quarter-cache', () =>
1820
jest.requireActual('~/__mocks__/services/quarter-cache')
1921
)
2022

23+
const LY = new LyWithOffset()
24+
const LQ = new LqWithOffset()
25+
2126
describe('company', () => {
2227
const ticker = '2371'
2328

@@ -111,7 +116,7 @@ describe('quarter', () => {
111116
QuarterCache.clearAll()
112117
})
113118

114-
const period = new YearQuarterParam('LY', 'LQ')
119+
const period = new YearQuarterParam(LY, LQ)
115120

116121
test('(uncached)', () => {
117122
expect(QuarterCache.getData(ticker, new YearQuarter(2018, 1))).toBeNull()
@@ -168,7 +173,7 @@ describe('ondemandQuarter', () => {
168173
QuarterCache.clearAll()
169174
})
170175

171-
const period = new YearQuarterParam('LY', 'LQ')
176+
const period = new YearQuarterParam(LY, LQ)
172177

173178
test('(uncached)', () => {
174179
expect(QuarterCache.getData(ticker, new YearQuarter(2018, 1))).toBeNull()

src/api/v2/client.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ test('HttpError#isInvalidTestingRequest', () => {
1111
200,
1212
'{"message":"Testing Apikey is only allowed to ticker ending with \\"01\\""}'
1313
)()
14-
const error1 = new HttpError(res1)
14+
const error1 = new HttpError('https://example.com', res1)
1515
expect(error1.isInvalidTestingRequest()).toBeTruthy()
1616

1717
const res2 = useMockedUrlFetchApp(403, '{"message": "Forbidden"}')()
18-
const error2 = new HttpError(res2)
18+
const error2 = new HttpError('https://example.com', res2)
1919
expect(error2.isInvalidTestingRequest()).toBeFalsy()
2020
})
2121

src/api/v2/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class BuffettCodeApiClientV2 {
1616

1717
const code = res.getResponseCode()
1818
const content = res.getContentText()
19-
const error = new HttpError(res)
19+
const error = new HttpError(url, res)
2020
if (
2121
Math.floor(code / 100) === 4 ||
2222
Math.floor(code / 100) === 5 ||
@@ -29,8 +29,8 @@ export class BuffettCodeApiClientV2 {
2929
try {
3030
json = JSON.parse(content)
3131
} catch (e) {
32-
console.error('JSON parsing error', code, content)
33-
throw new HttpError(res)
32+
console.error('JSON parsing error', url, code, content)
33+
throw new HttpError(url, res)
3434
}
3535

3636
return json

src/api/v3/caching-client.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { DailyCache } from '~/__mocks__/services/daily-cache'
33
import { QuarterCache } from '~/__mocks__/services/quarter-cache'
44
import { CachingBuffettCodeApiClientV3 } from '~/api/v3/caching-client'
55
import { DateParam } from '~/fiscal-periods/date-param'
6+
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
7+
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
68
import { YearQuarter } from '~/fiscal-periods/year-quarter'
79
import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'
810

@@ -19,6 +21,9 @@ jest.mock('~/services/quarter-cache', () =>
1921
jest.requireActual('~/__mocks__/services/quarter-cache')
2022
)
2123

24+
const LY = new LyWithOffset()
25+
const LQ = new LqWithOffset()
26+
2227
describe('company', () => {
2328
const ticker = '2371'
2429

@@ -112,7 +117,7 @@ describe('quarter', () => {
112117
QuarterCache.clearAll()
113118
})
114119

115-
const period = new YearQuarterParam('LY', 'LQ')
120+
const period = new YearQuarterParam(LY, LQ)
116121

117122
test('(uncached)', () => {
118123
expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).toBeNull()
@@ -165,7 +170,7 @@ describe('ondemandQuarter', () => {
165170
QuarterCache.clearAll()
166171
})
167172

168-
const period = new YearQuarterParam('LY', 'LQ')
173+
const period = new YearQuarterParam(LY, LQ)
169174

170175
test('(uncached)', () => {
171176
expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).toBeNull()

0 commit comments

Comments
 (0)