diff --git a/lib/tinybucket/api/helper/team_helper.rb b/lib/tinybucket/api/helper/team_helper.rb index ea2a624..776f8b7 100644 --- a/lib/tinybucket/api/helper/team_helper.rb +++ b/lib/tinybucket/api/helper/team_helper.rb @@ -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 diff --git a/lib/tinybucket/api/team_api.rb b/lib/tinybucket/api/team_api.rb index 7e52a7c..58a593b 100644 --- a/lib/tinybucket/api/team_api.rb +++ b/lib/tinybucket/api/team_api.rb @@ -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 diff --git a/lib/tinybucket/client.rb b/lib/tinybucket/client.rb index a61713f..3bd248b 100644 --- a/lib/tinybucket/client.rb +++ b/lib/tinybucket/client.rb @@ -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. diff --git a/lib/tinybucket/resource.rb b/lib/tinybucket/resource.rb index 03e9e6b..9c6a692 100644 --- a/lib/tinybucket/resource.rb +++ b/lib/tinybucket/resource.rb @@ -14,6 +14,7 @@ module Resource :PublicRepos, :PullRequests, :Repos, + :Teams, :Watchers ].each do |klass_name| autoload klass_name diff --git a/lib/tinybucket/resource/teams.rb b/lib/tinybucket/resource/teams.rb new file mode 100644 index 0000000..6b87277 --- /dev/null +++ b/lib/tinybucket/resource/teams.rb @@ -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 diff --git a/spec/fixtures/teams/get_role_admin.json b/spec/fixtures/teams/get_role_admin.json new file mode 100644 index 0000000..c2aa6d6 --- /dev/null +++ b/spec/fixtures/teams/get_role_admin.json @@ -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 +} diff --git a/spec/lib/tinybucket/api/team_api_spec.rb b/spec/lib/tinybucket/api/team_api_spec.rb index 335965c..cf112a4 100644 --- a/spec/lib/tinybucket/api/team_api_spec.rb +++ b/spec/lib/tinybucket/api/team_api_spec.rb @@ -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) } diff --git a/spec/lib/tinybucket/client_spec.rb b/spec/lib/tinybucket/client_spec.rb index 8813776..ccc194e 100644 --- a/spec/lib/tinybucket/client_spec.rb +++ b/spec/lib/tinybucket/client_spec.rb @@ -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) } diff --git a/spec/lib/tinybucket/resource/teams_spec.rb b/spec/lib/tinybucket/resource/teams_spec.rb new file mode 100644 index 0000000..803b61a --- /dev/null +++ b/spec/lib/tinybucket/resource/teams_spec.rb @@ -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