Skip to content

Commit

Permalink
Add Programs API
Browse files Browse the repository at this point in the history
  • Loading branch information
yyoshiki41 committed Nov 9, 2016
1 parent 0abb07f commit 4c9b1cd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
6 changes: 3 additions & 3 deletions area_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ func TestAreaID(t *testing.T) {
}

func TestProcessSpanNode(t *testing.T) {
const expectedAreaID = "JP13"
s := `document.write('<span class="` + expectedAreaID + `">TOKYO JAPAN</span>');`
const expected = "JP13"
s := `document.write('<span class="` + expected + `">TOKYO JAPAN</span>');`

doc, err := html.Parse(strings.NewReader(s))
if err != nil {
t.Errorf("Parse HTML: %s", err)
}

areaID := processSpanNode(doc)
if areaID != expectedAreaID {
if areaID != expected {
t.Errorf(
"Failed to process span node.\nAreaID: %s", areaID)
}
Expand Down
19 changes: 15 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,26 @@ func TestNew(t *testing.T) {
}
}

func TestEmptyHTTPClient(t *testing.T) {
var c *http.Client
SetHTTPClient(c)

client, err := New()
if err == nil {
t.Errorf(
"Should detect HTTPClient is nil.\nclient: %v", client)
}
}

func TestSetHTTPClient(t *testing.T) {
const timeout = 1 * time.Second
SetHTTPClient(&http.Client{Timeout: timeout})
const expected = 1 * time.Second
SetHTTPClient(&http.Client{Timeout: expected})

client, err := New()
if err != nil {
t.Errorf("Failed to construct client: %s", err)
}
if client.HTTPClient.Timeout != timeout {
t.Errorf("Failed to set http client: %s", err)
if client.HTTPClient.Timeout != expected {
t.Errorf("expected %d, but %d", expected, client.HTTPClient.Timeout)
}
}
21 changes: 10 additions & 11 deletions program.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type Programs struct {
Stations Stations `xml:"stations"`
Stations *Stations `xml:"stations"`
}

type Stations struct {
Expand All @@ -32,9 +32,13 @@ type Progs struct {
type Prog struct {
Ft string `xml:"ft,attr"`
To string `xml:"to,attr"`
Ftl string `xml:"ftl,attr"`
Tol string `xml:"tol,attr"`
Dur string `xml:"dur,attr"`
Title string `xml:"title"`
SubTitle string `xml:"sub_title"`
Desc string `xml:"desc"`
Pfm string `xml:"pfm"`
Info string `xml:"info"`
URL string `xml:"url"`
}
Expand Down Expand Up @@ -67,18 +71,13 @@ func (c *Client) GetNowPrograms(ctx context.Context, areaID string) (*Programs,
return &programs, err
}

// GetStationMaps returns available station's map.
// GetStationList returns a slice of available Station.
// This API wraps GetNowPrograms.
func (c *Client) GetStationMaps(ctx context.Context, areaID string) (map[string]string, error) {
m := make(map[string]string)

func (c *Client) GetStationList(ctx context.Context, areaID string) ([]Station, error) {
programs, err := c.GetNowPrograms(ctx, areaID)
if err != nil {
return m, err
if err != nil || programs.Stations == nil {
return nil, err
}

for _, s := range programs.Stations.Stations {
m[s.ID] = s.Name
}
return m, err
return programs.Stations.Stations, err
}
10 changes: 5 additions & 5 deletions program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ func TestGetNowPrograms(t *testing.T) {
if err != nil {
t.Errorf("Error: %s", err)
}
if programs != nil {
if programs == nil {
t.Errorf("Programs is nil.")
}
}

func TestGetStationMaps(t *testing.T) {
func TestGetStationList(t *testing.T) {
client, err := New()
if err != nil {
t.Fatalf("Failed to construct client: %s", err)
}

ctx := context.Background()
m, err := client.GetStationMaps(ctx, areaIDTokyo)
s, err := client.GetStationList(ctx, areaIDTokyo)
if err != nil {
t.Errorf("Error: %s", err)
}
if len(m) == 0 {
t.Errorf("Program map is empty.")
if s == nil {
t.Error("Stations is empty.")
}
}

0 comments on commit 4c9b1cd

Please sign in to comment.