Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mremond committed Jul 26, 2016
1 parent 7419c0b commit bad60f9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 72 deletions.
11 changes: 0 additions & 11 deletions api.go
Expand Up @@ -129,14 +129,3 @@ func (r *RegisterUser) params() (HTTPParams, error) {
body: body,
}, nil
}

//==============================================================================

func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
49 changes: 49 additions & 0 deletions helpers.go
@@ -0,0 +1,49 @@
package ejabberd

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

//==============================================================================
// Helpers for command-line tool

// JoinURL checks that Base URL is a valid URL and joins base URL with
// the method suffix string.
func JoinURL(baseURL string, suffix string) (string, error) {
var u *url.URL
var err error

if u, err = url.Parse(baseURL); err != nil {
return "", fmt.Errorf("invalid url: %s", baseURL)
}

if u.Scheme != "http" && u.Scheme != "https" {
return "", fmt.Errorf("invalid url scheme: %s", u.Scheme)
}

u.Path = path.Join(u.Path, suffix)
return u.String(), nil
}

// PrepareScope ensures we return scopes as space separated. However,
// we accept comma separated scopes as input as well for convenience.
func PrepareScope(s string) string {
return strings.Replace(s, ",", " ", -1)
}

//==============================================================================
// Internal helper functions

// stringInSlice returns whether a string is a member of a string
// slice.
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
38 changes: 38 additions & 0 deletions jid.go
@@ -0,0 +1,38 @@
package ejabberd

import (
"errors"
"fmt"
"strings"
)

// JID processing
// TODO update gox and import it directly from gox

type jid struct {
username string
domain string
resource string
}

func parseJID(sjid string) (jid, error) {
var j jid

s1 := strings.SplitN(sjid, "/", 2)
if len(s1) > 1 {
j.resource = s1[1]
}

s2 := strings.Split(s1[0], "@")
if len(s2) != 2 {
return jid{}, errors.New("invalid jid")
}

j.username = s2[0]
j.domain = s2[1]
return j, nil
}

func (j jid) bare() string {
return fmt.Sprintf("%s@%s", j.username, j.domain)
}
61 changes: 0 additions & 61 deletions oauth_token.go
Expand Up @@ -7,9 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -117,62 +115,3 @@ func params(j jid, password, scope, ttl string) url.Values {
}

// =============================================================================

// Helpers for command-line tool

// JoinURL checks that Base URL is a valid URL and joins base URL with
// the method suffix string.
func JoinURL(baseURL string, suffix string) (string, error) {
var u *url.URL
var err error

if u, err = url.Parse(baseURL); err != nil {
return "", fmt.Errorf("invalid url: %s", baseURL)
}

if u.Scheme != "http" && u.Scheme != "https" {
return "", fmt.Errorf("invalid url scheme: %s", u.Scheme)
}

u.Path = path.Join(u.Path, suffix)
return u.String(), nil
}

// PrepareScope ensures we return scopes as space separated. However,
// we accept comma separated scopes as input as well for convenience.
func PrepareScope(s string) string {
return strings.Replace(s, ",", " ", -1)
}

// =============================================================================

// JID processing
// TODO update gox and import it directly from gox

type jid struct {
username string
domain string
resource string
}

func parseJID(sjid string) (jid, error) {
var j jid

s1 := strings.SplitN(sjid, "/", 2)
if len(s1) > 1 {
j.resource = s1[1]
}

s2 := strings.Split(s1[0], "@")
if len(s2) != 2 {
return jid{}, errors.New("invalid jid")
}

j.username = s2[0]
j.domain = s2[1]
return j, nil
}

func (j jid) bare() string {
return fmt.Sprintf("%s@%s", j.username, j.domain)
}

0 comments on commit bad60f9

Please sign in to comment.