tinybucket 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +35 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +44 -0
  6. data/.travis.yml +10 -0
  7. data/Gemfile +22 -0
  8. data/Guardfile +13 -0
  9. data/LICENSE +21 -0
  10. data/README.md +237 -0
  11. data/Rakefile +39 -0
  12. data/lib/faraday_middleware/follow_oauth_redirects.rb +67 -0
  13. data/lib/tinybucket.rb +61 -0
  14. data/lib/tinybucket/api.rb +20 -0
  15. data/lib/tinybucket/api/base_api.rb +31 -0
  16. data/lib/tinybucket/api/branch_restrictions_api.rb +26 -0
  17. data/lib/tinybucket/api/comments_api.rb +46 -0
  18. data/lib/tinybucket/api/commits_api.rb +26 -0
  19. data/lib/tinybucket/api/diff_api.rb +17 -0
  20. data/lib/tinybucket/api/helper.rb +22 -0
  21. data/lib/tinybucket/api/helper/api_helper.rb +48 -0
  22. data/lib/tinybucket/api/helper/branch_restrictions_helper.rb +27 -0
  23. data/lib/tinybucket/api/helper/comments_helper.rb +49 -0
  24. data/lib/tinybucket/api/helper/commits_helper.rb +27 -0
  25. data/lib/tinybucket/api/helper/diff_helper.rb +29 -0
  26. data/lib/tinybucket/api/helper/pull_requests_helper.rb +44 -0
  27. data/lib/tinybucket/api/helper/repo_helper.rb +29 -0
  28. data/lib/tinybucket/api/helper/repos_helper.rb +21 -0
  29. data/lib/tinybucket/api/helper/team_helper.rb +35 -0
  30. data/lib/tinybucket/api/helper/user_helper.rb +31 -0
  31. data/lib/tinybucket/api/pull_requests_api.rb +49 -0
  32. data/lib/tinybucket/api/repo_api.rb +35 -0
  33. data/lib/tinybucket/api/repos_api.rb +19 -0
  34. data/lib/tinybucket/api/team_api.rb +51 -0
  35. data/lib/tinybucket/api/user_api.rb +44 -0
  36. data/lib/tinybucket/api_factory.rb +21 -0
  37. data/lib/tinybucket/client.rb +95 -0
  38. data/lib/tinybucket/connection.rb +62 -0
  39. data/lib/tinybucket/constants.rb +4 -0
  40. data/lib/tinybucket/error.rb +8 -0
  41. data/lib/tinybucket/error/base_error.rb +12 -0
  42. data/lib/tinybucket/error/service_error.rb +20 -0
  43. data/lib/tinybucket/model.rb +20 -0
  44. data/lib/tinybucket/model/base.rb +54 -0
  45. data/lib/tinybucket/model/branch_restriction.rb +17 -0
  46. data/lib/tinybucket/model/comment.rb +39 -0
  47. data/lib/tinybucket/model/commit.rb +39 -0
  48. data/lib/tinybucket/model/concerns.rb +14 -0
  49. data/lib/tinybucket/model/concerns/reloadable.rb +45 -0
  50. data/lib/tinybucket/model/concerns/repository_keys.rb +43 -0
  51. data/lib/tinybucket/model/error_response.rb +7 -0
  52. data/lib/tinybucket/model/page.rb +65 -0
  53. data/lib/tinybucket/model/profile.rb +37 -0
  54. data/lib/tinybucket/model/pull_request.rb +64 -0
  55. data/lib/tinybucket/model/repository.rb +96 -0
  56. data/lib/tinybucket/model/team.rb +39 -0
  57. data/lib/tinybucket/parser.rb +25 -0
  58. data/lib/tinybucket/parser/base_parser.rb +17 -0
  59. data/lib/tinybucket/parser/branch_restriction_parser.rb +9 -0
  60. data/lib/tinybucket/parser/branch_restrictions_parser.rb +10 -0
  61. data/lib/tinybucket/parser/comment_parser.rb +9 -0
  62. data/lib/tinybucket/parser/comments_parser.rb +10 -0
  63. data/lib/tinybucket/parser/commit_parser.rb +9 -0
  64. data/lib/tinybucket/parser/commits_parser.rb +9 -0
  65. data/lib/tinybucket/parser/profile_parser.rb +9 -0
  66. data/lib/tinybucket/parser/profiles_parser.rb +9 -0
  67. data/lib/tinybucket/parser/pull_request_parser.rb +9 -0
  68. data/lib/tinybucket/parser/pull_requests_parser.rb +10 -0
  69. data/lib/tinybucket/parser/repo_parser.rb +9 -0
  70. data/lib/tinybucket/parser/repos_parser.rb +10 -0
  71. data/lib/tinybucket/parser/team_parser.rb +9 -0
  72. data/lib/tinybucket/parser/teams_parser.rb +9 -0
  73. data/lib/tinybucket/request.rb +55 -0
  74. data/lib/tinybucket/response.rb +7 -0
  75. data/lib/tinybucket/response/error_handler.rb +14 -0
  76. data/lib/tinybucket/version.rb +3 -0
  77. data/spec/fixtures/commit.json +83 -0
  78. data/spec/fixtures/profile.json +29 -0
  79. data/spec/fixtures/pull_request.json +106 -0
  80. data/spec/fixtures/repositories/get.json +78 -0
  81. data/spec/fixtures/repositories/test_owner/get.json +78 -0
  82. data/spec/fixtures/repositories/test_owner/test_repo/branch-restrictions/1/get.json +17 -0
  83. data/spec/fixtures/repositories/test_owner/test_repo/branch-restrictions/get.json +101 -0
  84. data/spec/fixtures/repositories/test_owner/test_repo/commit/1/comments/1/get.json +38 -0
  85. data/spec/fixtures/repositories/test_owner/test_repo/commit/1/comments/get.json +40 -0
  86. data/spec/fixtures/repositories/test_owner/test_repo/commit/1/get.json +83 -0
  87. data/spec/fixtures/repositories/test_owner/test_repo/commits/get.json +73 -0
  88. data/spec/fixtures/repositories/test_owner/test_repo/diff/1/get.json +21 -0
  89. data/spec/fixtures/repositories/test_owner/test_repo/forks/get.json +78 -0
  90. data/spec/fixtures/repositories/test_owner/test_repo/get.json +71 -0
  91. data/spec/fixtures/repositories/test_owner/test_repo/patch/1/get.json +29 -0
  92. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/1/approve/delete.json +3 -0
  93. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/1/approve/post.json +3 -0
  94. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/1/comments/1/get.json +30 -0
  95. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/1/comments/get.json +44 -0
  96. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/1/commits/get.json +66 -0
  97. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/1/diff/get.txt +13 -0
  98. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/1/get.json +164 -0
  99. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/get.json +112 -0
  100. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/get_state_declined.json +112 -0
  101. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/get_state_merged.json +112 -0
  102. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/get_state_open.json +112 -0
  103. data/spec/fixtures/repositories/test_owner/test_repo/watchers/get.json +32 -0
  104. data/spec/fixtures/repository.json +71 -0
  105. data/spec/fixtures/teams/test_team/followers/get.json +36 -0
  106. data/spec/fixtures/teams/test_team/following/get.json +39 -0
  107. data/spec/fixtures/teams/test_team/get.json +32 -0
  108. data/spec/fixtures/teams/test_team/members/get.json +36 -0
  109. data/spec/fixtures/teams/test_team/repositories/get.json +125 -0
  110. data/spec/fixtures/users/test_owner/followers/get.json +65 -0
  111. data/spec/fixtures/users/test_owner/following/get.json +100 -0
  112. data/spec/fixtures/users/test_owner/get.json +29 -0
  113. data/spec/lib/tinybucket/api/branch_restrictions_api_spec.rb +78 -0
  114. data/spec/lib/tinybucket/api/comments_api_spec.rb +133 -0
  115. data/spec/lib/tinybucket/api/commits_api_spec.rb +63 -0
  116. data/spec/lib/tinybucket/api/diff_api_spec.rb +5 -0
  117. data/spec/lib/tinybucket/api/pull_requests_api_spec.rb +229 -0
  118. data/spec/lib/tinybucket/api/repo_api_spec.rb +100 -0
  119. data/spec/lib/tinybucket/api/repos_api_spec.rb +28 -0
  120. data/spec/lib/tinybucket/api/team_api_spec.rb +87 -0
  121. data/spec/lib/tinybucket/api/user_api_spec.rb +60 -0
  122. data/spec/lib/tinybucket/api_factory_spec.rb +23 -0
  123. data/spec/lib/tinybucket/client_spec.rb +102 -0
  124. data/spec/lib/tinybucket/connection_spec.rb +30 -0
  125. data/spec/lib/tinybucket/model/branch_restriction_spec.rb +29 -0
  126. data/spec/lib/tinybucket/model/comment_spec.rb +31 -0
  127. data/spec/lib/tinybucket/model/commit_spec.rb +62 -0
  128. data/spec/lib/tinybucket/model/page_spec.rb +58 -0
  129. data/spec/lib/tinybucket/model/profile_spec.rb +47 -0
  130. data/spec/lib/tinybucket/model/pull_request_spec.rb +122 -0
  131. data/spec/lib/tinybucket/model/repository_spec.rb +131 -0
  132. data/spec/lib/tinybucket/model/team_spec.rb +53 -0
  133. data/spec/lib/tinybucket_spec.rb +32 -0
  134. data/spec/spec_helper.rb +42 -0
  135. data/spec/support/api_response_macros.rb +30 -0
  136. data/spec/support/model_macros.rb +61 -0
  137. data/tinybucket.gemspec +36 -0
  138. metadata +437 -0
@@ -0,0 +1,43 @@
1
+ module Tinybucket
2
+ module Model
3
+ module Concerns
4
+ module RepositoryKeys
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ attr_accessor :repo_owner, :repo_slug
9
+
10
+ def repo_keys?
11
+ repo_owner.present? && repo_slug.present?
12
+ end
13
+
14
+ def repo_keys
15
+ { repo_owner: repo_owner, repo_slug: repo_slug }
16
+ end
17
+
18
+ def repo_keys=(keys)
19
+ self.repo_owner = keys[:repo_owner]
20
+ self.repo_slug = keys[:repo_slug]
21
+ end
22
+
23
+ private
24
+
25
+ def inject_repo_keys(result)
26
+ case result
27
+ when Tinybucket::Model::Page
28
+ result.items.map do |m|
29
+ next unless (m.class).concern_included?(:RepositoryKeys)
30
+ m.repo_keys = repo_keys
31
+ end
32
+ when Tinybucket::Model::Base
33
+ result.repo_keys = repo_keys \
34
+ if (result.class).concern_included?(:RepositoryKeys)
35
+ end
36
+
37
+ result
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ module Tinybucket
2
+ module Model
3
+ class ErrorResponse < Base
4
+ attr_accessor :message, :fields, :detail, :id
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,65 @@
1
+ module Tinybucket
2
+ module Model
3
+ class Page
4
+ # attr_reader :size, :page, :pagelen, :next, :previous
5
+ attr_reader :attrs
6
+ attr_reader :items
7
+ attr_writer :next_proc
8
+
9
+ def initialize(json, item_klass)
10
+ @attrs = {}
11
+ %w(size page pagelen next previous).each do |attr|
12
+ @attrs[attr.intern] = json[attr]
13
+ end
14
+
15
+ @items = if json['values'].present? && json['values'].is_a?(Array)
16
+ json['values'].map { |hash| item_klass.new(hash) }
17
+ else
18
+ []
19
+ end
20
+ end
21
+
22
+ def size
23
+ @attrs[:size]
24
+ end
25
+
26
+ def each(options = {})
27
+ loop do
28
+ start = (@attrs[:page] - 1) * @attrs[:pagelen]
29
+
30
+ @items.each_with_index do |m, i|
31
+ pos = start + i
32
+ yield(m, pos)
33
+
34
+ fail StopIteration if \
35
+ (!options[:limit].nil? && pos == (options[:limit] - 1))
36
+ end
37
+
38
+ break unless next_page?
39
+
40
+ load_next
41
+ end
42
+
43
+ rescue StopIteration
44
+ return
45
+ end
46
+
47
+ private
48
+
49
+ def next_page?
50
+ @attrs[:next].present?
51
+ end
52
+
53
+ def load_next
54
+ fail StopIteration if @attrs[:next].nil? || @next_proc.nil?
55
+
56
+ list = @next_proc.call(@attrs[:page] + 1)
57
+
58
+ fail StopIteration if list.items.size == 0
59
+
60
+ @attrs = list.attrs
61
+ @items = list.items
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,37 @@
1
+ module Tinybucket
2
+ module Model
3
+ class Profile < Base
4
+ include Tinybucket::Model::Concerns::Reloadable
5
+
6
+ attr_accessor \
7
+ :username, :kind, :website, :display_name,
8
+ :links, :created_on, :location, :type
9
+
10
+ def followers(options = {})
11
+ user_api(options).followers(options)
12
+ end
13
+
14
+ def following(options = {})
15
+ user_api(options).following(options)
16
+ end
17
+
18
+ def repos(options = {})
19
+ user_api(options).repos(options)
20
+ end
21
+
22
+ private
23
+
24
+ def user_api(options)
25
+ return @user if @user
26
+
27
+ @user = create_instance 'User', options
28
+ @user.username = username
29
+ @user
30
+ end
31
+
32
+ def load_model
33
+ user_api({}).profile
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,64 @@
1
+ module Tinybucket
2
+ module Model
3
+ class PullRequest < Base
4
+ include Tinybucket::Model::Concerns::RepositoryKeys
5
+ include Tinybucket::Model::Concerns::Reloadable
6
+
7
+ attr_accessor \
8
+ :state, :description, :links, :title, :close_source_branch,
9
+ :destination, :reason, :id, :source, :created_on, :author, :updated_on,
10
+ :merge_commit, :closed_by, :reviewers, :participants
11
+
12
+ def create(_params)
13
+ fail NotImplementedError
14
+ end
15
+
16
+ def update(_params)
17
+ fail NotImplementedError
18
+ end
19
+
20
+ def approve(options = {})
21
+ pull_request_api(options).approve(id, options)
22
+ end
23
+
24
+ def unapprove(options = {})
25
+ pull_request_api(options).unapprove(id, options)
26
+ end
27
+
28
+ def commits(options = {})
29
+ pull_request_api(options).commits(id, options)
30
+ end
31
+
32
+ def comments(options = {})
33
+ comment_api(options).list(options)
34
+ end
35
+
36
+ def comment(comment_id, options = {})
37
+ comment_api(options).find(comment_id, options)
38
+ end
39
+
40
+ def diff(options = {})
41
+ pull_request_api(options).diff(id, options)
42
+ end
43
+
44
+ private
45
+
46
+ def comment_api(options)
47
+ api = create_api('Comments', repo_keys, options)
48
+ api.commented_to = self
49
+ api
50
+ end
51
+
52
+ def pull_request_api(options)
53
+ fail ArgumentError,
54
+ 'This method call require repository keys.' unless repo_keys?
55
+
56
+ create_api('PullRequests', repo_keys, options)
57
+ end
58
+
59
+ def load_model
60
+ pull_request_api({}).find(id)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,96 @@
1
+ module Tinybucket
2
+ module Model
3
+ class Repository < Base
4
+ include Tinybucket::Model::Concerns::RepositoryKeys
5
+ include Tinybucket::Model::Concerns::Reloadable
6
+
7
+ attr_accessor :scm, :has_wiki, :description, :links, :updated_on,
8
+ :fork_policy, :created_on, :owner, :size, :parent,
9
+ :has_issues, :is_private, :full_name, :name, :language
10
+
11
+ def create(_params)
12
+ fail NotImplementedError
13
+ end
14
+
15
+ def destroy
16
+ fail NotImplementedError
17
+ end
18
+
19
+ def pull_requests(options = {})
20
+ list = pull_requests_api(options).list(options)
21
+ inject_repo_keys(list)
22
+ end
23
+
24
+ def pull_request(pullrequest_id = nil, options = {})
25
+ m = if pullrequest_id.present?
26
+ pull_requests_api(options).find(pullrequest_id, options)
27
+ else
28
+ Tinybucket::Model::PullRequest.new({})
29
+ end
30
+ inject_repo_keys(m)
31
+ end
32
+
33
+ def watchers(options = {})
34
+ repo_api(options).watchers(options)
35
+ end
36
+
37
+ def forks(options = {})
38
+ repo_api(options).forks(options)
39
+ end
40
+
41
+ def commits(options = {})
42
+ list = commits_api(options).list(options)
43
+ inject_repo_keys(list)
44
+ end
45
+
46
+ def commit(revision, options = {})
47
+ m = commits_api(options).find(revision, options)
48
+ inject_repo_keys(m)
49
+ end
50
+
51
+ def branch_restrictions(options = {})
52
+ list = restrictions_api(options).list(options)
53
+ inject_repo_keys(list)
54
+ end
55
+
56
+ def branch_restriction(restriction_id, options = {})
57
+ m = restrictions_api(options).find(restriction_id, options)
58
+ inject_repo_keys(m)
59
+ end
60
+
61
+ def diff(spec, options = {})
62
+ diff_api(options).find(spec, options)
63
+ end
64
+
65
+ def patch(spec, options = {})
66
+ diff_api(options).find_patch(spec, options)
67
+ end
68
+
69
+ private
70
+
71
+ def pull_requests_api(options)
72
+ create_api('PullRequests', repo_keys, options)
73
+ end
74
+
75
+ def repo_api(options)
76
+ create_api('Repo', repo_keys, options)
77
+ end
78
+
79
+ def commits_api(options)
80
+ create_api('Commits', repo_keys, options)
81
+ end
82
+
83
+ def restrictions_api(options)
84
+ create_api('BranchRestrictions', repo_keys, options)
85
+ end
86
+
87
+ def diff_api(options)
88
+ create_api('Diff', repo_keys, options)
89
+ end
90
+
91
+ def load_model
92
+ repo_api({}).find()
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,39 @@
1
+ module Tinybucket
2
+ module Model
3
+ class Team < Base
4
+ include Tinybucket::Model::Concerns::Reloadable
5
+
6
+ attr_accessor \
7
+ :username, :kind, :website, :display_name,
8
+ :links, :created_on, :location, :type
9
+
10
+ def members(options = {})
11
+ team_api(options).members(username, options)
12
+ end
13
+
14
+ def followers(options = {})
15
+ team_api(options).followers(username, options)
16
+ end
17
+
18
+ def following(options = {})
19
+ team_api(options).following(username, options)
20
+ end
21
+
22
+ def repos(options = {})
23
+ team_api(options).repos(username, options)
24
+ end
25
+
26
+ private
27
+
28
+ def team_api(options)
29
+ return @team if @team
30
+
31
+ @team = create_instance 'Team', options
32
+ end
33
+
34
+ def load_model
35
+ team_api({}).find(username)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,25 @@
1
+ module Tinybucket
2
+ module Parser
3
+ extend ActiveSupport::Autoload
4
+
5
+ [
6
+ :BaseParser,
7
+ :BranchRestrictionParser,
8
+ :BranchRestrictionsParser,
9
+ :CommitParser,
10
+ :CommitsParser,
11
+ :CommentParser,
12
+ :CommentsParser,
13
+ :ProfileParser,
14
+ :ProfilesParser,
15
+ :PullRequestParser,
16
+ :PullRequestsParser,
17
+ :RepoParser,
18
+ :ReposParser,
19
+ :TeamParser,
20
+ :TeamsParser
21
+ ].each do |klass_name|
22
+ autoload klass_name
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ module Tinybucket
2
+ module Parser
3
+ class BaseParser < FaradayMiddleware::ResponseMiddleware
4
+ def process_response(env)
5
+ env[:body] = convert(env[:body])
6
+ end
7
+
8
+ def parse_response?(env)
9
+ (env[:body]).is_a?(Hash)
10
+ end
11
+
12
+ # override on subclass
13
+ def convert(_json)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module Tinybucket
2
+ module Parser
3
+ class BranchRestrictionParser < BaseParser
4
+ def convert(json)
5
+ Tinybucket::Model::BranchRestriction.new(json)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module Tinybucket
2
+ module Parser
3
+ class BranchRestrictionsParser < BaseParser
4
+ def convert(json)
5
+ Tinybucket::Model::Page.new(
6
+ json, Tinybucket::Model::BranchRestriction)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module Tinybucket
2
+ module Parser
3
+ class CommentParser < BaseParser
4
+ def convert(json)
5
+ Tinybucket::Model::Comment.new(json)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module Tinybucket
2
+ module Parser
3
+ class CommentsParser < BaseParser
4
+ def convert(json)
5
+ Tinybucket::Model::Page.new(
6
+ json, Tinybucket::Model::Comment)
7
+ end
8
+ end
9
+ end
10
+ end