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

Add file loader option #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 31 additions & 15 deletions country_mapper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package country_mapper

import (
"bytes"
"encoding/csv"
"io"
"io/ioutil"
"net/http"
"strings"
)
Expand Down Expand Up @@ -123,14 +126,8 @@ func (c *CountryInfo) CallingCodeLower() []string {
return updated
}

func readCSVFromURL(fileURL string) ([][]string, error) {
resp, err := http.Get(fileURL)
if err != nil {
return nil, err
}

defer resp.Body.Close()
reader := csv.NewReader(resp.Body)
func readCSV(body io.Reader) ([][]string, error) {
reader := csv.NewReader(body)
reader.Comma = ';'
data, err := reader.ReadAll()
if err != nil {
Expand All @@ -140,20 +137,39 @@ func readCSVFromURL(fileURL string) ([][]string, error) {
return data, nil
}

// Pass in an optional url if you would like to use your own downloadable csv file for country's data.
// This is useful if you prefer to host the data file yourself or if you have modified some of the fields
// for your specific use case.
func Load(specifiedURL ...string) (*CountryInfoClient, error) {
func LoadByUrl(url ...string) (*CountryInfoClient, error) {
var fileURL string

// use user specified url for csv file if provided, else use default file URL
if len(specifiedURL) > 0 {
fileURL = specifiedURL[0]
if len(url) > 0 {
fileURL = url[0]
} else {
fileURL = defaultFile
}

data, err := readCSVFromURL(fileURL)
resp, err := http.Get(fileURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()

return load(resp.Body)
}

func LoadByFile(file string) (*CountryInfoClient, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}

return load(bytes.NewBuffer(data))
}

// Pass in an optional url if you would like to use your own downloadable csv file for country's data.
// This is useful if you prefer to host the data file yourself or if you have modified some of the fields
// for your specific use case.
func load(body io.Reader) (*CountryInfoClient, error) {
data, err := readCSV(body)
if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion country_mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ var mockClient *CountryInfoClient
// Setup Tests
//===========================================
func Test_Init(t *testing.T) {
client, err := Load()
client, err := LoadByUrl()
assert.Nil(t, err)
mockClient = client
}

func TestLoadByFile(t *testing.T) {
client, err := LoadByFile("./files/country_info.csv")
assert.Nil(t, err)
mockClient = client
}
Expand Down