Skip to content

Commit

Permalink
Updated spaceapibee to SpaceAPI v13.
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Oct 25, 2014
1 parent ffea7ad commit 5623ebc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
26 changes: 10 additions & 16 deletions bees/spaceapibee/spaceapibee.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"encoding/json"
"github.com/muesli/beehive/bees"
"io/ioutil"
"log"
"net/http"
)

Expand All @@ -43,26 +44,24 @@ func (mod *SpaceApiBee) Action(action bees.Action) []bees.Placeholder {
switch action.Name {
case "get_status":
type SpaceApiResult struct {
Status string `json: "status"`
Open bool `json: "open"`
State struct {
Open bool `json: "open"`
} `json: "state"`
}
api_state := new(SpaceApiResult)

// get json data
resp, err := http.Get(mod.url)
var text string
if err != nil {
text = "Error: SpaceAPI instance @ " + mod.url + " not reachable"
log.Println("Error: SpaceAPI instance @ " + mod.url + " not reachable")
} else {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)

err = json.Unmarshal(body, api_state)

if err != nil {
text = "Sorry, couldn't unmarshal the JSON data from SpaceAPI Instance @ " + mod.url
} else {
text = api_state.Status
log.Println("Sorry, couldn't unmarshal the JSON data from SpaceAPI Instance @ " + mod.url)
api_state.State.Open = false
}
}

Expand All @@ -71,21 +70,16 @@ func (mod *SpaceApiBee) Action(action bees.Action) []bees.Placeholder {
Name: "query_result",
Options: []bees.Placeholder{
bees.Placeholder{
Name: "status",
Name: "open",
Type: "bool",
Value: api_state.Open,
},
bees.Placeholder{
Name: "text",
Type: "string",
Value: text,
Value: api_state.State.Open,
},
},
}
mod.evchan <- ev

default:
panic("Unknown action triggered in " +mod.Name()+": "+action.Name)
panic("Unknown action triggered in " + mod.Name() + ": " + action.Name)
}

return outs
Expand Down
11 changes: 3 additions & 8 deletions bees/spaceapibee/spaceapibeefactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type SpaceApiBeeFactory struct {
func (factory *SpaceApiBeeFactory) New(name, description string, options bees.BeeOptions) bees.BeeInterface {
bee := SpaceApiBee{
Bee: bees.NewBee(name, factory.Name(), description),
url: options.GetValue("url").(string),
url: options.GetValue("url").(string),
}

return &bee
Expand Down Expand Up @@ -83,15 +83,10 @@ func (factory *SpaceApiBeeFactory) Events() []bees.EventDescriptor {
Description: "is triggered as soon as the query has been executed",
Options: []bees.PlaceholderDescriptor{
bees.PlaceholderDescriptor{
Name: "status",
Description: "status of the spaceapi instance that was queried",
Name: "open",
Description: "open-state of the spaceapi instance that was queried",
Type: "bool",
},
bees.PlaceholderDescriptor{
Name: "text",
Description: "text of the spaceapi instance that was queried",
Type: "string",
},
},
},
}
Expand Down

0 comments on commit 5623ebc

Please sign in to comment.