Skip to content

Commit

Permalink
Refactor: print report from reports pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
dude333 committed Apr 26, 2021
1 parent 9d027ae commit 7cba815
Show file tree
Hide file tree
Showing 15 changed files with 554 additions and 98 deletions.
19 changes: 1 addition & 18 deletions cmd/rapina/fii.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
/*
Copyright © 2021 Adriano P <dev@dude333.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Distributed under the MIT License.
*/
package main

Expand Down
73 changes: 39 additions & 34 deletions cmd/rapina/fii_dividends.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
/*
Copyright © 2021 Adriano P <dev@dude333.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Distributed under the MIT License.
*/
package main

Expand All @@ -27,10 +10,9 @@ import (
"os"
"strings"

"github.com/dude333/rapina/fetch"
"github.com/dude333/rapina/parsers"
"github.com/dude333/rapina/reports"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// fiiDividendsCmd represents the rendimentos command
Expand Down Expand Up @@ -67,34 +49,57 @@ func init() {
}

//
// FIIDividends prints the dividends from 'code' fund for 'n' months,
// FIIDividends prints the dividends from 'code' for 'n' months,
// starting from latest.
//
func FIIDividends(code string, n int) error {
code = strings.ToUpper(code)

db, err := openDatabase()
if err != nil {
return err
}
code = strings.ToUpper(code)

stockStore, _ := parsers.NewStockStore(db)
srv, err := fetch.NewStockServer(stockStore, viper.GetString("apikey"))
fiiParser := parsers.NewFIIStore(db)

r, err := reports.NewFIITerminalReport(db, stockStore, fiiParser)
if err != nil {
return err
}
err = srv.FetchStockQuote(fix(code))
err = r.Dividends(code, n)
if err != nil {
return err
}
/*
// QUOTES
stockStore, _ := parsers.NewStockStore(db)
stock, err := fetch.NewStockFetch(stockStore, viper.GetString("apikey"))
if err != nil {
return err
}
q, err := stock.Quote(code, "2011-01-02")
if err != nil {
return err
}
fmt.Println(q)
fiiStore := parsers.NewFIIStore(db)
fii := fetch.NewFII(fiiStore)
return fii.FetchFIIDividends(code, n)
}
// DIVIDENDS
fiiStore := parsers.NewFIIStore(db)
fii := fetch.NewFII(fiiStore)
err = fii.Dividends(code, n)
if err != nil {
return err
}
func fix(code string) string {
if len(code) == 4 {
return code + "11"
}
return code
// DIVIDENDS REPORT
r, err := reports.NewFIITerminalReport(db)
if err != nil {
return err
}
r.Dividends(code, n)
if err != nil {
return err
}
*/
return nil
}
72 changes: 72 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package rapina

import (
"fmt"
"net/url"
"path"
"strconv"
"strings"
"time"
)

// IsDate checks if date is in format YYYY-MM-DD.
func IsDate(date string) bool {
if len(date) != len("2021-04-26") || strings.Count(date, "-") != 2 {
return false
}

y, errY := strconv.Atoi(date[0:4])
m, errM := strconv.Atoi(date[5:7])
d, errD := strconv.Atoi(date[8:10])
if errY != nil || errM != nil || errD != nil {
return false
}

// Ok, we'll still be using this in 2200 :)
if y < 1970 || y > 2200 {
return false
}
if m < 1 || m > 12 {
return false
}
nDays := [13]int{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if d < 1 || d > nDays[m] {
return false
}
return true
}

func IsUrl(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}

// JoinURL joins strings as URL paths
func JoinURL(base string, paths ...string) string {
p := path.Join(paths...)
return fmt.Sprintf("%s/%s", strings.TrimRight(base, "/"), strings.TrimLeft(p, "/"))
}

var _timeNow = time.Now

// MonthsFromToday returns a list of months including the current.
// Date formatted as YYYY-MM-DD.
func MonthsFromToday(n int) []string {
if n < 1 {
n = 1
}
if n > 100 {
n = 100
}

now := _timeNow()
now = time.Date(now.Year(), now.Month(), 15, 12, 0, 0, 0, time.UTC)

var monthYears []string
for ; n > 0; n-- {
monthYears = append(monthYears, now.Format("2006-01"))
now = now.AddDate(0, -1, 0)
}

return monthYears
}
120 changes: 120 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package rapina

import (
"reflect"
"testing"
"time"
)

func TestIsDate(t *testing.T) {
type args struct {
date string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "should be true",
args: args{date: "2021-04-26"},
want: true,
},
{
name: "should be true too",
args: args{date: "2030-12-31"},
want: true,
},
{
name: "should be false",
args: args{date: "2021-04-31"},
want: false,
},
{
name: "should be false too",
args: args{date: "20/12/2000"},
want: false,
},
{
name: "should be false three",
args: args{date: "2021-07-32"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsDate(tt.args.date); got != tt.want {
t.Errorf("IsDate() = %v, want %v", got, tt.want)
}
})
}
}

func TestIsUrl(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "should be true",
args: args{str: "http://example.com/path"},
want: true,
},
{
name: "should be false",
args: args{str: "example.com/path"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsUrl(tt.args.str); got != tt.want {
t.Errorf("IsUrl() = %v, want %v", got, tt.want)
}
})
}
}

func TestMonthsFromToday(t *testing.T) {
timeNow1 := func() time.Time {
return time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
}
timeNow2 := func() time.Time {
return time.Date(2009, time.March, 31, 23, 0, 0, 0, time.UTC)
}

type args struct {
n int
}
tests := []struct {
name string
args args
timeNow func() time.Time
want []string
}{
{
name: "should show 3 months",
args: args{n: 3},
timeNow: timeNow1,
want: []string{"2009-11", "2009-10", "2009-09"},
},
{
name: "should show 2 months",
args: args{n: 2},
timeNow: timeNow2,
want: []string{"2009-03", "2009-02"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_timeNow = tt.timeNow
if got := MonthsFromToday(tt.args.n); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MonthsFromToday() = %#v, want %v", got, tt.want)
}
})
}
}
12 changes: 12 additions & 0 deletions data_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ type FIIDetails struct {
ShareHolderEmail string `json:"shareHolderEmail"`
} `json:"shareHolder"`
}

type Quotation struct {
Code string
Date string
Val float64
}

type Dividend struct {
Code string
Date string
Val float64
}
7 changes: 7 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package rapina

import "errors"

var (
ErrInvalidDate = errors.New("invalid date format")
)
Loading

0 comments on commit 7cba815

Please sign in to comment.