Skip to content

Commit

Permalink
Added status endpoint, closes #52.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Dec 24, 2015
1 parent f08dc9b commit 1630e44
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Changelog

* [#52](https://github.com/dblock/slack-gamebot/issues/52): Added an API `/status` endpoint that pings the first team - [@dblock](https://github.com/dblock).
* [#50](https://github.com/dblock/slack-gamebot/issues/50): Automatically disable teams with `account_inactive` - [@dblock](https://github.com/dblock).
* [#25](https://github.com/dblock/slack-gamebot/issues/25): Users can `draw` a challenge, all players have to `draw` - [@dblock](https://github.com/dblock).
* [#46](https://github.com/dblock/slack-gamebot/issues/46): Captains can reset seasons by confirming the team name - [@dblock](https://github.com/dblock).
Expand Down
1 change: 1 addition & 0 deletions slack-gamebot/api/endpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
require 'slack-gamebot/api/endpoints/users_endpoint'
require 'slack-gamebot/api/endpoints/seasons_endpoint'
require 'slack-gamebot/api/endpoints/teams_endpoint'
require 'slack-gamebot/api/endpoints/status_endpoint'
require 'slack-gamebot/api/endpoints/root_endpoint'
1 change: 1 addition & 0 deletions slack-gamebot/api/endpoints/root_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class RootEndpoint < Grape::API
present self, with: Api::Presenters::RootPresenter
end

mount Api::Endpoints::StatusEndpoint
mount Api::Endpoints::UsersEndpoint
mount Api::Endpoints::ChallengesEndpoint
mount Api::Endpoints::MatchesEndpoint
Expand Down
14 changes: 14 additions & 0 deletions slack-gamebot/api/endpoints/status_endpoint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Api
module Endpoints
class StatusEndpoint < Grape::API
format :json

namespace :status do
desc 'Get system status.'
get do
present self, with: Api::Presenters::StatusPresenter
end
end
end
end
end
1 change: 1 addition & 0 deletions slack-gamebot/api/presenters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'roar/json/hal'

require 'slack-gamebot/api/presenters/paginated_presenter'
require 'slack-gamebot/api/presenters/status_presenter'
require 'slack-gamebot/api/presenters/challenge_presenter'
require 'slack-gamebot/api/presenters/challenges_presenter'
require 'slack-gamebot/api/presenters/user_presenter'
Expand Down
4 changes: 4 additions & 0 deletions slack-gamebot/api/presenters/root_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module RootPresenter
"#{base_url(opts)}/"
end

link :status do |opts|
"#{base_url(opts)}/status"
end

link :users do |opts|
{
href: "#{base_url(opts)}/users/#{params(Api::Helpers::PaginationParameters::ALL, :team_id, :captain)}",
Expand Down
57 changes: 57 additions & 0 deletions slack-gamebot/api/presenters/status_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module Api
module Presenters
module StatusPresenter
include Roar::JSON::HAL
include Roar::Hypermedia
include Grape::Roar::Representer

link :self do |opts|
"#{base_url(opts)}/status"
end

property :teams_count
property :active_teams_count
property :users_count
property :challenges_count
property :matches_count
property :seasons_count
property :ping

private

def teams_count
Team.count
end

def users_count
User.count
end

def challenges_count
Challenge.count
end

def matches_count
Match.count
end

def seasons_count
Season.count
end

def active_teams_count
Team.active.count
end

def ping
team = Team.asc(:_id).first
team.ping! if team
end

def base_url(opts)
request = Grape::Request.new(opts[:env])
request.base_url
end
end
end
end
9 changes: 9 additions & 0 deletions slack-gamebot/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ def to_s
"name=#{name}, domain=#{domain}, id=#{team_id}, token=#{token[0..5]}..#{token[-5..-1]}"
end

def ping!
client = Slack::Web::Client.new(token: token)
auth = client.auth_test
{
auth: auth,
presence: client.users_getPresence(user: auth['user_id'])
}
end

def self.find_or_create_from_env!
token = ENV['SLACK_API_TOKEN']
return unless token
Expand Down
2 changes: 1 addition & 1 deletion spec/api/endpoints/root_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
get '/'
expect(last_response.status).to eq 200
links = JSON.parse(last_response.body)['_links']
expect(links.keys.sort).to eq(%w(self team teams user users challenge challenges match matches current_season season seasons).sort)
expect(links.keys.sort).to eq(%w(self status team teams user users challenge challenges match matches current_season season seasons).sort)
end
it 'follows all links' do
get '/'
Expand Down
26 changes: 26 additions & 0 deletions spec/api/endpoints/status_endpoint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

describe Api::Endpoints::StatusEndpoint do
include Api::Test::EndpointTest

before do
allow_any_instance_of(Team).to receive(:ping!).and_return(ok: 1)
end

context 'status' do
it 'returns a status' do
status = client.status
expect(status.teams_count).to eq 0
expect(status.active_teams_count).to eq 0
end

context 'with a team' do
let!(:team) { Fabricate(:team, active: false) }
it 'returns a status' do
status = client.status
expect(status.teams_count).to eq 1
expect(status.active_teams_count).to eq 0
end
end
end
end
1 change: 1 addition & 0 deletions spec/api/swagger_documentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
end
it 'documents root level apis' do
expect(subject['apis'].map { |api| api['path'] }).to eq([
'/status.{format}',
'/users.{format}',
'/challenges.{format}',
'/matches.{format}',
Expand Down

0 comments on commit 1630e44

Please sign in to comment.