Skip to content

Commit

Permalink
Adding write on folder requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Dec 3, 2017
1 parent 6b6d10b commit 34f41f3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
10 changes: 8 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ func main() {
return
}

err = user_interface.ShowContentFolder(setup.RemoteFolder)
if user_interface.HasPipedInput() {
inputData := user_interface.GetPipedInputData()

util.ShowMessageAndExitOnError(err)
user_interface.WriteFolder(setup.RemoteFolder, inputData);
} else {
err = user_interface.ShowContentFolder(setup.RemoteFolder)

util.ShowMessageAndExitOnError(err)
}
}
25 changes: 23 additions & 2 deletions dontpad/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/nettoclaudio/dontpad-cli/util"
)

Expand All @@ -15,8 +17,10 @@ type Response struct {

var (
extractHttpResponseBody func(string) ([]byte, error)
templateURLViewFolder string = "http://dontpad.com/%s.body.json?lastUpdate=%d"
templateURLSubfolders string = "http://dontpad.com/%s.menu.json"

templateURL string = "http://dontpad.com/%s"
templateURLViewFolder string = templateURL + ".body.json?lastUpdate=%d"
templateURLSubfolders string = templateURL + ".menu.json"
)

func init() {
Expand Down Expand Up @@ -51,6 +55,23 @@ func GetSubfolders(remoteFolder string) ([]string, error) {
return subfolders, err
}

func ReplaceContentFolder(remoteFolder, data string) {
finalURL := formatTemplateURL(remoteFolder)

formValues := url.Values{}
formValues.Set("text", data)

http.PostForm(finalURL, formValues)
}

func formatTemplateURL(remoteFolder string) string {
buffer := &bytes.Buffer{}

fmt.Fprintf(buffer, templateURL, remoteFolder)

return buffer.String()
}

func formatTemplateURLViewFolder(remoteFolder string, lastUpdate int) string {
buffer := &bytes.Buffer{}

Expand Down
12 changes: 12 additions & 0 deletions dontpad/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,15 @@ func TestGetSubfolders_ResponseOK_NoSubfolders_MustReturnEmptyList(t *testing.T)
t.Errorf("Expected %v but got %v.", expectedSubfolders, subfolders)
}
}

func TestFormatTemplateURL_MustReturnSameStringAsExpected(t *testing.T) {
remoteFolder := "my-folder"

expectedURL := "http://dontpad.com/my-folder"

actualURL := formatTemplateURL(remoteFolder)

if actualURL != expectedURL {
t.Errorf("Expected [%s] but got [%s].", expectedURL, actualURL)
}
}
4 changes: 4 additions & 0 deletions user_interface/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ func ListSubfolders(remoteFolder string) error {

return err
}

func WriteFolder(remoteFolder, data string) {
dontpad.ReplaceContentFolder(remoteFolder, data)
}

0 comments on commit 34f41f3

Please sign in to comment.