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

Ryanairbee #163

Open
wants to merge 3 commits 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
Binary file added assets/bees/ryanairbee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions bees/ryanairbee/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ryanairbee

Interact with the ryanairAPI

---

Currently supported:
* get_schedules: Fetching flight schedules
116 changes: 116 additions & 0 deletions bees/ryanairbee/ryanairbee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (C) 2014-2017 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Christian Muehlhaeuser <[email protected]>
* Nicolas Martin <[email protected]>
*/

// Package ryanairbee is a Bee that can post blogs & quotes on Tumblr.
package ryanairbee

import (
"github.com/muesli/beehive/bees"
"github.com/seblw/goryan"
"time"
)

// RyanairBee is a Bee that can interact with the ryanairAPI
type RyanairBee struct {
bees.Bee

client *goryan.RyanairAPI

evchan chan bees.Event
}

const dateLayout = "2006-01-02"

// Action triggers the action passed to it.
func (mod *RyanairBee) Action(action bees.Action) []bees.Placeholder {
outs := []bees.Placeholder{}

switch action.Name {
case "get_schedules":
var cityFrom, cityTo, date string
action.Options.Bind("city_from", &cityFrom)
action.Options.Bind("city_to", &cityTo)
action.Options.Bind("date", &date)

// Parsing date && format to time.Time

time, err := time.Parse(dateLayout, date)
if err != nil {
mod.LogErrorf("Failed to convert date/format: %v", err)
return nil
}

// Fetching schedules
flights, err := mod.client.GetSchedules(cityFrom, cityTo, time)
if err != nil {
mod.LogErrorf("Failed to fetch schedules: %v", err)
return nil
}

for _, v := range flights {
ev := bees.Event{
Bee: mod.Name(),
Name: "flight_schedule",
Options: []bees.Placeholder{
{
Name: "number",
Type: "string",
Value: v.Number,
},
{
Name: "departure_time",
Type: "string",
Value: v.DepartureTime,
},
{
Name: "arrival_time",
Type: "string",
Value: v.ArrivalTime,
},
},
}
mod.evchan <- ev
}

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

return outs
}

// Run executes the Bee's event loop.
func (mod *RyanairBee) Run(eventChan chan bees.Event) {
mod.evchan = eventChan

mod.client = goryan.NewRyanairAPI(nil) // If nil {defaultHttpClient}

select {
case <-mod.SigChan:
return
}
}

// ReloadOptions parses the config options and initializes the Bee.
func (mod *RyanairBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)

}
139 changes: 139 additions & 0 deletions bees/ryanairbee/ryanairbeefactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (C) 2014-2017 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Christian Muehlhaeuser <[email protected]>
* Nicolas Martin <[email protected]>
*/

package ryanairbee

import (
"github.com/muesli/beehive/bees"
)

// RyanairBeeFactory is a factory for TumblrBees.
type RyanairBeeFactory struct {
bees.BeeFactory
}

// New returns a new Bee instance configured with the supplied options.
func (factory *RyanairBeeFactory) New(name, description string, options bees.BeeOptions) bees.BeeInterface {
bee := RyanairBee{
Bee: bees.NewBee(name, factory.ID(), description, options),
}
bee.ReloadOptions(options)

return &bee
}

// ID returns the ID of this Bee.
func (factory *RyanairBeeFactory) ID() string {
return "ryanairbee"
}

// Name returns the name of this Bee.
func (factory *RyanairBeeFactory) Name() string {
return "Ryanair"
}

// Description returns the description of this Bee.
func (factory *RyanairBeeFactory) Description() string {
return "interacts with the RyanairAPI"
}

// Image returns the filename of an image for this Bee.
func (factory *RyanairBeeFactory) Image() string {
return factory.ID() + ".png"
}

// LogoColor returns the preferred logo background color (used by the admin interface).
func (factory *RyanairBeeFactory) LogoColor() string {
return "#35465c"
}

// Options returns the options available to configure this Bee.
func (factory *RyanairBeeFactory) Options() []bees.BeeOptionDescriptor {
opts := []bees.BeeOptionDescriptor{}
// No options needed :)
return opts
}

// Events describes the available events provided by this Bee.
func (factory *RyanairBeeFactory) Events() []bees.EventDescriptor {
events := []bees.EventDescriptor{
{
Namespace: factory.Name(),
Name: "flight_schedule",
Description: "is triggered after fetching schedules",
Options: []bees.PlaceholderDescriptor{
{
Name: "number",
Description: "Flight number",
Type: "string",
},
{
Name: "departure_time",
Description: "Departure Time",
Type: "string",
},
{
Name: "arrival_time",
Description: "Arrival time",
Type: "string",
},
},
},
}
return events
}

// Actions describes the available actions provided by this Bee.
func (factory *RyanairBeeFactory) Actions() []bees.ActionDescriptor {
actions := []bees.ActionDescriptor{
{
Namespace: factory.Name(),
Name: "get_schedules",
Description: "Fetches flight schedules from the ryanairAPI",
Options: []bees.PlaceholderDescriptor{
{
Name: "city_from",
Description: "City flight starts",
Type: "string",
Mandatory: true,
},
{
Name: "city_to",
Description: "Arrival city",
Type: "string",
Mandatory: true,
},
{
Name: "date",
Description: "Date to fetch (Layout: 2023-04-20)",
Type: "string",
Mandatory: true,
},
},
},
}
return actions
}

func init() {
f := RyanairBeeFactory{}
bees.RegisterFactory(&f)
}
1 change: 1 addition & 0 deletions hives.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
_ "github.com/muesli/beehive/bees/pastebinbee"
_ "github.com/muesli/beehive/bees/pushoverbee"
_ "github.com/muesli/beehive/bees/rssbee"
_ "github.com/muesli/beehive/bees/ryanairbee"
_ "github.com/muesli/beehive/bees/s3bee"
_ "github.com/muesli/beehive/bees/serialbee"
_ "github.com/muesli/beehive/bees/simplepushbee"
Expand Down