Skip to content

Commit

Permalink
✨ Add teams API (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
hirakiuc authored Jul 2, 2017
1 parent a9c8e2a commit 121074b
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/tinybucket/api/helper/team_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,36 @@ module TeamHelper

private

def path_to_list
base_path
end

def path_to_find(name)
base_path(name)
team_path(name)
end

def path_to_members(name)
build_path(base_path(name), 'members')
build_path(team_path(name), 'members')
end

def path_to_followers(name)
build_path(base_path(name), 'followers')
build_path(team_path(name), 'followers')
end

def path_to_following(name)
build_path(base_path(name), 'following')
build_path(team_path(name), 'following')
end

def path_to_repos(name)
build_path(base_path(name), 'repositories')
build_path(team_path(name), 'repositories')
end

def base_path
build_path('/teams')
end

def base_path(name)
build_path('/teams', [name, 'teamname'])
def team_path(name)
build_path(base_path, [name, 'teamname'])
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/tinybucket/api/team_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ module Api
class TeamApi < BaseApi
include Tinybucket::Api::Helper::TeamHelper

# Send 'GET teams' request
#
# @param role_name [String] role name
# @param options [Hash]
# @return [Tinybucket::Model::Page]
def list(role_name, options = {})
get_path(
path_to_list,
{ role: role_name }.merge(options),
Tinybucket::Parser::TeamsParser
)
end

# Send 'GET the team profile' request
#
# @param name [String] The team's name
Expand Down
9 changes: 9 additions & 0 deletions lib/tinybucket/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ def repo(owner, repo_slug)
end
end

# Get teams
#
# @param role_name [String] role name (one of "admin", "contributor", or "member")
# @param options
# @return [Tinybucket::Resource::Teams]
def teams(role_name, options = {})
Tinybucket::Resource::Teams.new(role_name, options)
end

# Get the team
#
# @param teamname [String] the team name.
Expand Down
1 change: 1 addition & 0 deletions lib/tinybucket/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Resource
:PublicRepos,
:PullRequests,
:Repos,
:Teams,
:Watchers
].each do |klass_name|
autoload klass_name
Expand Down
22 changes: 22 additions & 0 deletions lib/tinybucket/resource/teams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Tinybucket
module Resource
class Teams < Base
def initialize(role_name, options = {})
@role_name = role_name
@args = [options]
end

private

def teams_api
create_api('Team')
end

def enumerator
create_enumerator(teams_api, :list, @role_name, *@args)
end
end
end
end
48 changes: 48 additions & 0 deletions spec/fixtures/teams/get_role_admin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"pagelen": 10,
"values": [
{
"username": "test_team",
"website": null,
"display_name": "test tesm",
"uuid": "{e0f17982-effb-43cb-8589-7bacabb37cab}",
"links": {
"hooks": {
"href": "https://api.bitbucket.org/2.0/teams/test_team/hooks"
},
"self": {
"href": "https://api.bitbucket.org/2.0/teams/test_team"
},
"repositories": {
"href": "https://api.bitbucket.org/2.0/repositories/test_team"
},
"html": {
"href": "https://bitbucket.org/test_team/"
},
"followers": {
"href": "https://api.bitbucket.org/2.0/teams/test_team/followers"
},
"avatar": {
"href": "https://bitbucket.org/account/test_team/avatar/32/"
},
"members": {
"href": "https://api.bitbucket.org/2.0/teams/test_team/members"
},
"following": {
"href": "https://api.bitbucket.org/2.0/teams/test_team/following"
},
"projects": {
"href": "https://api.bitbucket.org/2.0/teams/test_team/projects/"
},
"snippets": {
"href": "https://api.bitbucket.org/2.0/snippets/test_team"
}
},
"created_on": "2014-08-05T15:17:01.717714+00:00",
"location": null,
"type": "team"
}
],
"page": 1,
"size": 1
}
8 changes: 8 additions & 0 deletions spec/lib/tinybucket/api/team_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

it { expect(api).to be_a_kind_of(Tinybucket::Api::BaseApi) }

describe 'list' do
subject { api.list(role_name) }

let(:role_name) { "admin" }
let(:request_path) { "/teams?role=admin" }
it { expect(subject).to be_an_instance_of(Tinybucket::Model::Page) }
end

describe 'profile' do
subject { api.find(teamname) }

Expand Down
7 changes: 7 additions & 0 deletions spec/lib/tinybucket/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@
end
end

describe 'tesma' do
subject { client.teams(role_name) }

let(:role_name) { "admin" }
it { expect(subject).to be_instance_of(Tinybucket::Resource::Teams) }
end

describe 'team' do
let(:team) { 'test_team' }
subject { client.team(team) }
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/tinybucket/resource/teams_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

RSpec.describe Tinybucket::Resource::Teams do
include ApiResponseMacros

let(:role_name) { "admin" }
let(:resource) { Tinybucket::Resource::Teams.new(role_name) }

describe "Enumerable Methods" do
let(:request_path) { "/teams?role=#{role_name}" }
before { stub_enum_response(:get, request_path) }

describe "#take(1)" do
subject { resource.take(1) }
it { expect(subject).to be_an_instance_of(Array) }
end

describe "#each" do
it 'iterate models' do
resource.each do |m|
expect(m).to be_an_instance_of(Tinybucket::Model::Team)
end
end
end
end
end

0 comments on commit 121074b

Please sign in to comment.