-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added rounds and sups to the API. Closes #10.
- Loading branch information
Showing
19 changed files
with
279 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Api | ||
module Endpoints | ||
class RoundsEndpoint < Grape::API | ||
format :json | ||
helpers Api::Helpers::CursorHelpers | ||
helpers Api::Helpers::SortHelpers | ||
helpers Api::Helpers::PaginationParameters | ||
|
||
namespace :rounds do | ||
desc "Get a S'Up round." | ||
params do | ||
requires :id, type: String, desc: 'Round ID.' | ||
end | ||
get ':id' do | ||
round = Round.find(params[:id]) || error!('Not Found', 404) | ||
error!('Not Found', 404) unless round.team.api? | ||
present round, with: Api::Presenters::RoundPresenter | ||
end | ||
|
||
desc "Get all the S'Up rounds for a team." | ||
params do | ||
requires :team_id, type: String, desc: 'Team ID.' | ||
use :pagination | ||
end | ||
get do | ||
team = Team.find(params[:team_id]) || error!('Not Found', 404) | ||
error!('Not Found', 404) unless team.api? | ||
rounds = paginate_and_sort_by_cursor(team.rounds, default_sort_order: '_id') | ||
present rounds, with: Api::Presenters::RoundsPresenter | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Api | ||
module Endpoints | ||
class SupsEndpoint < Grape::API | ||
format :json | ||
helpers Api::Helpers::CursorHelpers | ||
helpers Api::Helpers::SortHelpers | ||
helpers Api::Helpers::PaginationParameters | ||
|
||
namespace :sups do | ||
desc "Get a S'Up." | ||
params do | ||
requires :id, type: String, desc: 'Sup ID.' | ||
end | ||
get ':id' do | ||
sup = Sup.find(params[:id]) || error!('Not Found', 404) | ||
error!('Not Found', 404) unless sup.round.team.api? | ||
present sup, with: Api::Presenters::SupPresenter | ||
end | ||
|
||
desc "Get all the S'Up for a round." | ||
params do | ||
requires :round_id, type: String, desc: 'Team ID.' | ||
use :pagination | ||
end | ||
get do | ||
round = Round.find(params[:round_id]) || error!('Not Found', 404) | ||
error!('Not Found', 404) unless round.team.api? | ||
sups = paginate_and_sort_by_cursor(round.sups, default_sort_order: '_id') | ||
present sups, with: Api::Presenters::SupsPresenter | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Api | ||
module Presenters | ||
module RoundPresenter | ||
include Roar::JSON::HAL | ||
include Roar::Hypermedia | ||
include Grape::Roar::Representer | ||
|
||
property :id, type: String, desc: 'Round ID.' | ||
property :ran_at, type: DateTime, desc: 'Date/time when the round was run.' | ||
property :asked_at, type: DateTime, desc: 'Date/time when outcomes were collected.' | ||
property :created_at, type: DateTime, desc: 'Date/time when the round was created.' | ||
property :updated_at, type: DateTime, desc: 'Date/time when the round was updated.' | ||
|
||
link :team do |opts| | ||
request = Grape::Request.new(opts[:env]) | ||
"#{request.base_url}/api/teams/#{team_id}" | ||
end | ||
|
||
link :sups do |opts| | ||
request = Grape::Request.new(opts[:env]) | ||
"#{request.base_url}/api/sups?round_id=#{id}" | ||
end | ||
|
||
link :self do |opts| | ||
request = Grape::Request.new(opts[:env]) | ||
"#{request.base_url}/api/rounds/#{id}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Api | ||
module Presenters | ||
module RoundsPresenter | ||
include Roar::JSON::HAL | ||
include Roar::Hypermedia | ||
include Grape::Roar::Representer | ||
include Api::Presenters::PaginatedPresenter | ||
|
||
collection :results, extend: RoundPresenter, as: :rounds, embedded: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Api | ||
module Presenters | ||
module SupPresenter | ||
include Roar::JSON::HAL | ||
include Roar::Hypermedia | ||
include Grape::Roar::Representer | ||
|
||
property :id, type: String, desc: "S'Up ID." | ||
property :outcome, type: String, desc: "S'up outcome." | ||
property :created_at, type: DateTime, desc: "Date/time when the S'Up was created." | ||
property :updated_at, type: DateTime, desc: "Date/time when the S'Up was updated." | ||
|
||
collection :users, extend: UserPresenter, as: :users, embedded: true | ||
|
||
link :round do |opts| | ||
request = Grape::Request.new(opts[:env]) | ||
"#{request.base_url}/api/rounds/#{round_id}" | ||
end | ||
|
||
link :self do |opts| | ||
request = Grape::Request.new(opts[:env]) | ||
"#{request.base_url}/api/sups/#{id}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Api | ||
module Presenters | ||
module SupsPresenter | ||
include Roar::JSON::HAL | ||
include Roar::Hypermedia | ||
include Grape::Roar::Representer | ||
include Api::Presenters::PaginatedPresenter | ||
|
||
collection :results, extend: SupPresenter, as: :sups, embedded: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'spec_helper' | ||
|
||
describe Api::Endpoints::RoundsEndpoint do | ||
include Api::Test::EndpointTest | ||
|
||
let!(:team) { Fabricate(:team, api: true) } | ||
|
||
before do | ||
@cursor_params = { team_id: team.id.to_s } | ||
end | ||
|
||
it_behaves_like 'a cursor api', Round | ||
|
||
context 'round' do | ||
let(:existing_round) { Fabricate(:round, team: team) } | ||
it 'returns a round' do | ||
round = client.round(id: existing_round.id) | ||
expect(round.id).to eq existing_round.id.to_s | ||
expect(round._links.self._url).to eq "http://example.org/api/rounds/#{existing_round.id}" | ||
end | ||
it 'cannot return a round for a team with api off' do | ||
team.update_attributes!(api: false) | ||
expect { client.round(id: existing_round.id).resource }.to raise_error Faraday::ClientError do |e| | ||
json = JSON.parse(e.response[:body]) | ||
expect(json['error']).to eq 'Not Found' | ||
end | ||
end | ||
end | ||
|
||
context 'rounds' do | ||
let!(:round_1) { Fabricate(:round, team: team) } | ||
let!(:round_2) { Fabricate(:round, team: team) } | ||
it 'cannot return rounds for a team with api off' do | ||
team.update_attributes!(api: false) | ||
expect { client.rounds(team_id: team.id).resource }.to raise_error Faraday::ClientError do |e| | ||
json = JSON.parse(e.response[:body]) | ||
expect(json['error']).to eq 'Not Found' | ||
end | ||
end | ||
it 'returns rounds' do | ||
rounds = client.rounds(team_id: team.id) | ||
expect(rounds.map(&:id).sort).to eq [round_1, round_2].map(&:id).map(&:to_s).sort | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'spec_helper' | ||
|
||
describe Api::Endpoints::SupsEndpoint do | ||
include Api::Test::EndpointTest | ||
|
||
let!(:team) { Fabricate(:team, api: true) } | ||
let!(:round) { Fabricate(:round, team: team) } | ||
|
||
before do | ||
@cursor_params = { round_id: round.id.to_s } | ||
end | ||
|
||
it_behaves_like 'a cursor api', Sup | ||
|
||
context 'sup' do | ||
let(:existing_sup) { Fabricate(:sup, round: round) } | ||
it 'returns a sup' do | ||
sup = client.sup(id: existing_sup.id) | ||
expect(sup.id).to eq existing_sup.id.to_s | ||
expect(sup._links.self._url).to eq "http://example.org/api/sups/#{existing_sup.id}" | ||
end | ||
it 'cannot return a sup for a team with api off' do | ||
team.update_attributes!(api: false) | ||
expect { client.sup(id: existing_sup.id).resource }.to raise_error Faraday::ClientError do |e| | ||
json = JSON.parse(e.response[:body]) | ||
expect(json['error']).to eq 'Not Found' | ||
end | ||
end | ||
end | ||
|
||
context 'sups' do | ||
let!(:sup_1) { Fabricate(:sup, round: round) } | ||
let!(:sup_2) { Fabricate(:sup, round: round) } | ||
it 'cannot return sups for a team with api off' do | ||
team.update_attributes!(api: false) | ||
expect { client.sups(round_id: round.id).resource }.to raise_error Faraday::ClientError do |e| | ||
json = JSON.parse(e.response[:body]) | ||
expect(json['error']).to eq 'Not Found' | ||
end | ||
end | ||
it 'returns sups' do | ||
sups = client.sups(round_id: round.id) | ||
expect(sups.map(&:id).sort).to eq [sup_1, sup_2].map(&:id).map(&:to_s).sort | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
Fabricator(:round) do | ||
team { Fabricate(:team) } | ||
team { Team.first || Fabricate(:team) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
Fabricator(:sup) do | ||
round { Fabricate(:round) } | ||
round { Round.first || Fabricate(:round) } | ||
end |