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,95 @@
1
+ module Tinybucket
2
+ class Client
3
+ include ActiveSupport::Configurable
4
+
5
+ def initialize(options = {})
6
+ options.each_pair do |key, value|
7
+ config.send("#{key}=", value)
8
+ end
9
+
10
+ yield(config) if block_given?
11
+ end
12
+
13
+ # Get Repositories
14
+ #
15
+ # when you want to get repositories of the owner, call with owner, options.
16
+ # ex) repos('owner', options) or repos('owner')
17
+ #
18
+ # when you want to get public repositories of the owner, call with options.
19
+ # ex) repos(options) or repos
20
+ #
21
+ # @overload repos(owner, options)
22
+ # get the repositories of the owner.
23
+ # @param owner [String, Symbol] string or symbol to describe the owner.
24
+ # @option options [Hash] a hash with options
25
+ # @overload repos(options)
26
+ # get public repositories.
27
+ # @option options [Hash] a hash with options
28
+ # @return Tinybucket::Model::Page model instance.
29
+ def repos(*args)
30
+ case args.size
31
+ when 0
32
+ public_repos(*args)
33
+ when 1
34
+ case args.first
35
+ when Hash
36
+ public_repos(*args)
37
+ when String, Symbol
38
+ owners_repos(*args)
39
+ else
40
+ fail ArgumentError
41
+ end
42
+ when 2
43
+ owners_repos(*args)
44
+ else
45
+ fail ArgumentError
46
+ end
47
+ end
48
+
49
+ def repo(owner, repo_slug)
50
+ m = Tinybucket::Model::Repository.new({})
51
+ m.repo_owner = owner
52
+ m.repo_slug = repo_slug
53
+ m.api_config = config.dup
54
+ m
55
+ end
56
+
57
+ def team(teamname)
58
+ m = Tinybucket::Model::Team.new({})
59
+ m.username = teamname
60
+ m.api_config = config.dup
61
+ m
62
+ end
63
+
64
+ def user(username)
65
+ m = Tinybucket::Model::Profile.new({})
66
+ m.username = username
67
+ m.api_config = config.dup
68
+ m
69
+ end
70
+
71
+ private
72
+
73
+ def public_repos(*args)
74
+ options = (args.empty?) ? {} : args.first
75
+ fail ArgumentError unless options.is_a?(Hash)
76
+
77
+ @repos ||= create_instance('Repos', options)
78
+ @repos.list(options)
79
+ end
80
+
81
+ def owners_repos(*args)
82
+ owner = args.first
83
+ options = (args.size == 2) ? args[1] : {}
84
+ fail ArgumentError unless options.is_a?(Hash)
85
+
86
+ @user ||= create_instance('User', options)
87
+ @user.username = owner
88
+ @user.repos(options)
89
+ end
90
+
91
+ def create_instance(name, options)
92
+ ApiFactory.create_instance(name, config, options)
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,62 @@
1
+ module Tinybucket
2
+ module Connection
3
+ def clear_cache
4
+ @connection = nil
5
+ end
6
+
7
+ def caching?
8
+ !@connection.nil?
9
+ end
10
+
11
+ def connection(options = {}, parser = nil)
12
+ conn_options = default_options(options)
13
+ clear_cache
14
+
15
+ # TODO: cache connection for each (options, parser) pairs.
16
+ Faraday.new(
17
+ conn_options.merge(builder: stack(options, parser)))
18
+ end
19
+
20
+ private
21
+
22
+ def default_options(_options)
23
+ {
24
+ headers: {
25
+ USER_AGENT: 'test client' # TODO: fix this !
26
+ },
27
+ ssl: { verify: false },
28
+ url: 'https://bitbucket.org/api/2.0'.freeze
29
+ }
30
+ end
31
+
32
+ def default_middleware(_options)
33
+ proc do |conn|
34
+ oauth_secrets = {
35
+ consumer_key: config(:oauth_token),
36
+ consumer_secret: config(:oauth_secret)
37
+ }
38
+
39
+ conn.request :multipart
40
+ conn.request :url_encoded
41
+ conn.request :oauth, oauth_secrets
42
+
43
+ conn.response :follow_oauth_redirects, oauth_secrets
44
+ conn.response :json, content_type: /\bjson$/
45
+ conn.use Tinybucket::Response::ErrorHandler
46
+ conn.use :instrumentation
47
+
48
+ conn.adapter Faraday.default_adapter
49
+ end
50
+ end
51
+
52
+ def stack(options = {}, parser, &block)
53
+ Faraday::RackBuilder.new(&block) and return if block_given?
54
+
55
+ # TODO: cache stack for each (options, parser) pairs
56
+ Faraday::RackBuilder.new do |conn|
57
+ conn.use parser if parser.present?
58
+ default_middleware(options).call(conn)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,4 @@
1
+ module Tinybucket
2
+ module Constants
3
+ end
4
+ end
@@ -0,0 +1,8 @@
1
+ module Tinybucket
2
+ module Error
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :BaseError
6
+ autoload :ServiceError
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ module Tinybucket
2
+ module Error
3
+ class BaseError < StandardError
4
+ attr_reader :response_message, :response_headers
5
+
6
+ def initialize(message)
7
+ super message
8
+ @response_message = message
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ module Tinybucket
2
+ module Error
3
+ class ServiceError < BaseError
4
+ attr_accessor :http_headers
5
+
6
+ def initialize(env)
7
+ super generate_message(env)
8
+ @http_headers = env[:response_headers]
9
+ end
10
+
11
+ def generate_message(env)
12
+ http_method = env[:method].to_s.upcase
13
+ url = env[:url].to_s
14
+ status = env[:status]
15
+
16
+ "#{http_method} #{url} #{status} #{env[:body]}"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module Tinybucket
2
+ module Model
3
+ extend ActiveSupport::Autoload
4
+
5
+ [
6
+ :Base,
7
+ :BranchRestriction,
8
+ :Commit,
9
+ :Comment,
10
+ :ErrorResponse,
11
+ :Page,
12
+ :Profile,
13
+ :PullRequest,
14
+ :Repository,
15
+ :Team
16
+ ].each do |klass_name|
17
+ autoload klass_name
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,54 @@
1
+ module Tinybucket
2
+ module Model
3
+ class Base
4
+ include ::ActiveModel::Serializers::JSON
5
+ attr_accessor :api_config
6
+
7
+ def self.concern_included?(concern_name)
8
+ mod_name = "Tinybucket::Model::Concerns::#{concern_name}".constantize
9
+ ancestors.include?(mod_name)
10
+ end
11
+
12
+ def initialize(json)
13
+ self.attributes = json
14
+ @api_config = {}
15
+ @_loaded = !(json.empty?)
16
+ end
17
+
18
+ def attributes=(hash)
19
+ hash.each { |key, value| send("#{key}=", value) }
20
+ end
21
+
22
+ def attributes
23
+ instance_values.select do |key, _value|
24
+ case key
25
+ when /\A_.+\z/
26
+ when 'api_config', 'repo_owner', 'repo_slug'
27
+ false
28
+ else
29
+ true
30
+ end
31
+ end
32
+ end
33
+
34
+ protected
35
+
36
+ def create_api(api_key, keys, options)
37
+ key = ('@' + api_key.underscore).intern
38
+ api = instance_variable_get(key)
39
+ return api if api.present?
40
+
41
+ api = create_instance(api_key, options)
42
+ api.repo_owner = keys[:repo_owner]
43
+ api.repo_slug = keys[:repo_slug]
44
+ instance_variable_set(key, api)
45
+
46
+ api
47
+ end
48
+
49
+ def create_instance(klass_name, options)
50
+ ApiFactory.create_instance(klass_name, api_config, options)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,17 @@
1
+ module Tinybucket
2
+ module Model
3
+ class BranchRestriction < Base
4
+ include Tinybucket::Model::Concerns::RepositoryKeys
5
+
6
+ attr_accessor :groups, :id, :kind, :links, :pattern, :users
7
+
8
+ def update(_params)
9
+ fail NotImplementedError
10
+ end
11
+
12
+ def destroy
13
+ fail NotImplementedError
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ module Tinybucket
2
+ module Model
3
+ class Comment < Base
4
+ include Tinybucket::Model::Concerns::RepositoryKeys
5
+ include Tinybucket::Model::Concerns::Reloadable
6
+
7
+ attr_accessor \
8
+ :links, :id, :parent, :filename, :content, :user, :inline, \
9
+ :created_on, :updated_on
10
+
11
+ attr_accessor :commented_to
12
+
13
+ private
14
+
15
+ def commit_api
16
+ create_api('Comments', repo_keys, options)
17
+ end
18
+
19
+ def pull_request_api(options)
20
+ create_api('PullRequests', repo_keys, options)
21
+ end
22
+
23
+ def load_model
24
+ api =
25
+ case commented_to
26
+ when Tinybucket::Model::Commit
27
+ commit_api({})
28
+ when Tinybucket::Model::PullRequest
29
+ pull_request_api({})
30
+ else
31
+ fail ArgumentError, 'commented_to was invalid'
32
+ end
33
+
34
+ api.commented_to = commented_ato
35
+ api.find(id, {})
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ module Tinybucket
2
+ module Model
3
+ class Commit < Base
4
+ include Tinybucket::Model::Concerns::RepositoryKeys
5
+ include Tinybucket::Model::Concerns::Reloadable
6
+
7
+ attr_accessor \
8
+ :hash, :links, :repository, :author, :parents, :date,
9
+ :message, :participants
10
+
11
+ def comments(options = {})
12
+ comments_api(options).list(options)
13
+ end
14
+
15
+ def comment(comment_id, options = {})
16
+ comments_api(options).find(comment_id, options)
17
+ end
18
+
19
+ private
20
+
21
+ def comments_api(options)
22
+ fail ArgumentError,
23
+ 'This method call require repository keys.' unless repo_keys?
24
+
25
+ api = create_api('Comments', repo_keys, options)
26
+ api.commented_to = self
27
+ api
28
+ end
29
+
30
+ def commit_api(options)
31
+ create_api 'Commits', repo_keys, options
32
+ end
33
+
34
+ def load_model
35
+ commit_api({}).find(hash)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ module Tinybucket
2
+ module Model
3
+ module Concerns
4
+ extend ActiveSupport::Autoload
5
+
6
+ [
7
+ :RepositoryKeys,
8
+ :Reloadable
9
+ ].each do |mod_name|
10
+ autoload mod_name
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,45 @@
1
+ module Tinybucket
2
+ module Model
3
+ module Concerns
4
+ module Reloadable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ def load
9
+ return true if @_loaded
10
+
11
+ @_loaded = \
12
+ begin
13
+ self.attributes = load_model.attributes
14
+ true
15
+ rescue => e
16
+ Tinybucket.logger.error e
17
+ false
18
+ end
19
+
20
+ @_loaded
21
+ end
22
+
23
+ # rubocop: disable Style/TrivialAccessors
24
+
25
+ def loaded?
26
+ @_loaded
27
+ end
28
+
29
+ # rubocop: enabled Style/TrivialAccessors
30
+
31
+ def reload
32
+ @_loaded = false
33
+ load
34
+ end
35
+
36
+ private
37
+
38
+ def load_model
39
+ fail NotImplementedError
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end