Skip to content

Commit

Permalink
Merge pull request #10 from brunomvsouza/add-last-used-budget-support
Browse files Browse the repository at this point in the history
Add support to easily fetch last used budget
  • Loading branch information
Bruno Souza committed Aug 25, 2018
2 parents d669117 + 86d08c8 commit cc1919d
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/budget/example_test.go
Expand Up @@ -20,6 +20,14 @@ func ExampleService_GetBudget() {
// Output: *budget.Snapshot
}

func ExampleService_GetLastUsedBudget() {
c := ynab.NewClient("<valid_ynab_access_token>")
b, _ := c.Budget().GetLastUsedBudget(nil)
fmt.Println(reflect.TypeOf(b))

// Output: *budget.Snapshot
}

func ExampleService_GetBudget_filtered() {
c := ynab.NewClient("<valid_ynab_access_token>")

Expand Down
8 changes: 8 additions & 0 deletions api/budget/service.go
Expand Up @@ -61,6 +61,14 @@ func (s *Service) GetBudget(budgetID string, f *Filter) (*Snapshot, error) {
}, nil
}

// GetLastUsedBudget fetches the last used budget with all related
// entities, effectively a full budget export with filtering capabilities
// https://api.youneedabudget.com/v1#/Budgets/getBudgetById
func (s *Service) GetLastUsedBudget(f *Filter) (*Snapshot, error) {
const lastUsedBudgetID = "last-used"
return s.GetBudget(lastUsedBudgetID, f)
}

// GetBudgetSettings fetches a budget settings
// https://api.youneedabudget.com/v1#/Budgets/getBudgetSettingsById
func (s *Service) GetBudgetSettings(budgetID string) (*Settings, error) {
Expand Down
163 changes: 163 additions & 0 deletions api/budget/service_test.go
Expand Up @@ -250,6 +250,169 @@ func TestService_GetBudget(t *testing.T) {
assert.NoError(t, err)
}

func TestService_GetLastUsedBudget(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

url := "https://api.youneedabudget.com/v1/budgets/last-used"
httpmock.RegisterResponder(http.MethodGet, url,
func(req *http.Request) (*http.Response, error) {
res := httpmock.NewStringResponse(200, `{
"data": {
"budget": {
"id": "aa248caa-eed7-4575-a990-717386438d2c",
"name": "Test Budget",
"last_modified_on": "2018-03-05T17:24:36+00:00",
"date_format": {
"format": "DD/MM/YYYY"
},
"currency_format": {
"iso_code": "BRL",
"example_format": "123.456,78",
"decimal_digits": 2,
"decimal_separator": ",",
"symbol_first": true,
"group_separator": ".",
"currency_symbol": "R$",
"display_symbol": true
},
"first_month": "2017-12-01",
"last_month": "2018-02-01",
"accounts": [
{
"id": "312bf0ae-9d1a-42d7-84c1-8f1d5e4e7bb0",
"name": "Cash",
"type": "cash",
"on_budget": true,
"closed": false,
"note": null,
"balance": 0,
"cleared_balance": 0,
"uncleared_balance": 0,
"deleted": false
}
],
"payees": [
{
"id": "793846ad-f8f5-454e-9ae4-8d938d0d89ca",
"name": "Starting Balance",
"transfer_account_id": null,
"deleted": false
}
],
"payee_locations": [
{
"id": "47471638-da3e-4cdd-9288-e373b50fafa7",
"payee_id": "793846ad-f8f5-454e-9ae4-8d938d0d89ca",
"latitude": "20.8988754",
"longitude": "-33.9167891",
"deleted": false
}
],
"category_groups": [
{
"id": "840512c5-3b1d-426f-b033-f7c64a16a076",
"name": "Category group",
"hidden": false,
"deleted": false
}
],
"categories": [
{
"id": "138c8bcd-6ca3-4c09-82ca-1cde7aa1d6f8",
"category_group_id": "840512c5-3b1d-426f-b033-f7c64a16a076",
"name": "Category",
"hidden": false,
"original_category_group_id": null,
"note": null,
"budgeted": 0,
"activity": 12190,
"balance": 18740,
"deleted": false
}
],
"months": [
{
"month": "2018-03-01",
"note": null,
"to_be_budgeted": 0,
"age_of_money": null,
"categories": [
{
"id": "138c8bcd-6ca3-4c09-82ca-1cde7aa1d6f8",
"category_group_id": "840512c5-3b1d-426f-b033-f7c64a16a076",
"name": "Category",
"hidden": true,
"note": null,
"budgeted": 0,
"activity": 12190,
"balance": 18740,
"deleted": false
}
]
}
],
"transactions": [
{
"id": "e31928db-b236-4c88-9a99-7aa46ff7a6f7",
"date": "2018-01-09",
"amount": -85440,
"memo": null,
"cleared": "cleared",
"approved": true,
"flag_color": null,
"account_id": "312bf0ae-9d1a-42d7-84c1-8f1d5e4e7bb0",
"payee_id": "fa8d442e-0bfc-4386-8e5b-480c4f70733a",
"category_id": "0d3552a4-49da-4191-bac6-e22f80eb2056",
"transfer_account_id": null,
"import_id": null,
"deleted": false
}
],
"subtransactions": [
{
"id": "254049fe-cadc-4657-b36e-99baac0bd9ca",
"transaction_id": "891a41b8-bc0f-4c0b-b3a3-97d5d6d61276",
"amount": 0,
"memo": null,
"payee_id": "33fc3c91-8489-4da7-aef5-57ccd19d60dd",
"category_id": "2d9e60f6-0c7e-472f-8064-0465aa1c58d4",
"transfer_account_id": null,
"deleted": false
}
],
"scheduled_transactions": [
{
"id": "0971ec91-0961-42be-8598-c6d79c800b28",
"date_first": "2018-11-20",
"date_next": "2018-11-20",
"frequency": "never",
"amount": -17000,
"memo": "Domain bmvs.me",
"flag_color": "yellow",
"account_id": "09eaca5e-2a34-4baa-89c4-828fb90638f2",
"payee_id": "793846ad-f8f5-454e-9ae4-8d938d0d89ca",
"category_id": "138c8bcd-6ca3-4c09-82ca-1cde7aa1d6f8",
"transfer_account_id": null,
"deleted": false
}
],
"scheduled_subtransactions": []
},
"server_knowledge": 473
}
}
`)
res.Header.Add("X-Rate-Limit", "36/200")
return res, nil
},
)

client := ynab.NewClient("")
_, err := client.Budget().GetLastUsedBudget(nil)
assert.NoError(t, err)
}

func TestService_GetBudgetSettings(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
Expand Down

0 comments on commit cc1919d

Please sign in to comment.