Skip to content

Commit

Permalink
Qutoe csv->model refactor starting point.
Browse files Browse the repository at this point in the history
  • Loading branch information
ackleymi committed Feb 9, 2017
1 parent b13adef commit 833f324
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 283 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
cmd/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

![codecov.io](https://codecov.io/github/FlashBoys/go-finance/branch.svg?branch=master)

`go-finance` is a Golang library for retrieving financial data for quantitative analysis.
`go-finance` is a Go library for retrieving financial data for quantitative analysis.

To install go-finance, use the following command:

Expand Down Expand Up @@ -75,7 +75,7 @@ func main() {
// Set time bounds to 1 month starting Jan. 1.
start, _ := time.Parse(time.RFC3339, "2016-01-01T16:00:00+00:00")
end := start.AddDate(0, 1, 0)

// Request daily history for TWTR.
// IntervalDaily OR IntervalWeekly OR IntervalMonthly are supported.
bars, err := finance.GetQuoteHistory("TWTR", start, end, finance.IntervalDaily)
Expand All @@ -102,7 +102,7 @@ func main() {
// This example will return a slice of both dividends and splits.
start, _ := time.Parse(time.RFC3339, "2010-01-01T16:00:00+00:00")
end := time.Now()

// Request event history for AAPL.
events, err := finance.GetDividendSplitHistory("AAPL", start, end)
if err == nil {
Expand Down
23 changes: 10 additions & 13 deletions requests.go → fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,26 @@ var (
secondRegex = regexp.MustCompile(`(\w+):`)
)

// requestCSV fetches a csv from a supplied URL.
func requestCSV(url string) ([][]string, error) {
func fetchCSV(url string) (table [][]string, err error) {

resp, err := http.Get(url)
if err != nil {
return nil, err
return
}
defer resp.Body.Close()
reader := csv.NewReader(resp.Body)
reader.FieldsPerRecord = -1
data, err := reader.ReadAll()
if err != nil {
return nil, err
}

return data, nil
r := csv.NewReader(resp.Body)
r.FieldsPerRecord = -1
table, err = r.ReadAll()
return
}

// buildURL takes a base URL and parameters returns the full URL.
func buildURL(base string, params map[string]string) string {

url, _ := url.ParseRequestURI(base)
q := url.Query()

for k, v := range params {
q.Set(k, v)
}
Expand All @@ -44,8 +41,9 @@ func buildURL(base string, params map[string]string) string {
return url.String()
}

// request fetches a file from a supplied URL.
func request(url string) ([]byte, error) {
// fetch retrieves a file from a supplied URL.
func fetch(url string) ([]byte, error) {

resp, err := http.Get(url)
if err != nil {
return []byte{}, err
Expand All @@ -56,7 +54,6 @@ func request(url string) ([]byte, error) {
return []byte{}, err
}
result := string(contents)

return transformResult(result), err
}

Expand Down
8 changes: 4 additions & 4 deletions requests_test.go → fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_RequestCSV(t *testing.T) {
func Test_FetchCSV(t *testing.T) {

// Given that we want to download a csv
ts := startTestServer("request_csv_fixture.csv")
defer ts.Close()

// When we request the csv,
table, err := requestCSV(ts.URL)
table, err := fetchCSV(ts.URL)
assert.Nil(t, err)

// Then the returned table should have 1 row.
Expand All @@ -39,13 +39,13 @@ func Test_BuildURL(t *testing.T) {
assert.Equal(t, "http://example.com/d/quotes.csv?s=AAPL", url)
}

func Test_Request(t *testing.T) {
func Test_Fetch(t *testing.T) {
// Given that we want to download options data
ts := startTestServer("request_fixture.txt")
defer ts.Close()

// When we request the malformed json text,
response, err := request(ts.URL)
response, err := fetch(ts.URL)
assert.Nil(t, err)

// Then the returned string should be-
Expand Down
50 changes: 50 additions & 0 deletions fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package finance

import (
"reflect"

"github.com/shopspring/decimal"
)

func mapFields(vals []string, v interface{}) {

typ := reflect.TypeOf(v)
val := reflect.ValueOf(v).Elem()

if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}

for i := 0; i < typ.NumField(); i++ {

f := val.Field(i)

switch f.Interface().(type) {
case string:
f.Set(reflect.ValueOf(vals[i]))
case int:
f.Set(reflect.ValueOf(toInt(vals[i])))
case Timestamp:
f.Set(reflect.ValueOf(newStamp(vals[i])))
case decimal.Decimal:
f.Set(reflect.ValueOf(toDecimal(vals[i])))
}
}

}

func constructFields(in interface{}) (fields string) {

typ := reflect.TypeOf(in)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}

for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i)
tag := f.Tag.Get("yfin")
fields = fields + tag
}

return
}
2 changes: 1 addition & 1 deletion history.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func GetDividendSplitHistory(symbol string, start time.Time, end time.Time) (eve

func getHistoryTable(url string) ([][]string, error) {

table, err := requestCSV(url)
table, err := fetchCSV(url)
if err != nil {
return nil, fmt.Errorf("request history table error: (error was: %s)\n", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func urlParams(symbol string, e *Expiration) map[string]string {
// getOptionsData fetches data from the endpoint and returns an intermediate result.
func getOptionsData(url string) (fr *fetchResult, err error) {

b, err := request(url)
b, err := fetch(url)
if err != nil {
return nil, fmt.Errorf("options fetch error: (error was: %s)\n", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion pairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func GetCurrencyPairQuote(symbol string) (*FXPairQuote, error) {
// getPairsQuotesTable fetches the pairs data table from the endpoint.
func getPairsQuotesTable(url string) ([][]string, error) {

table, err := requestCSV(url)
table, err := fetchCSV(url)
if err != nil {
return nil, fmt.Errorf("request pairs table error: (error was: %s)\n", err.Error())
}
Expand Down
139 changes: 0 additions & 139 deletions quote.go

This file was deleted.

30 changes: 0 additions & 30 deletions quote_test.go

This file was deleted.

Loading

0 comments on commit 833f324

Please sign in to comment.