reenhanced_bitbucket_api 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +43 -0
  3. data/README.md +169 -0
  4. data/Rakefile +3 -0
  5. data/lib/bitbucket_rest_api/api/actions.rb +50 -0
  6. data/lib/bitbucket_rest_api/api.rb +120 -0
  7. data/lib/bitbucket_rest_api/api_factory.rb +30 -0
  8. data/lib/bitbucket_rest_api/authorization.rb +34 -0
  9. data/lib/bitbucket_rest_api/client.rb +58 -0
  10. data/lib/bitbucket_rest_api/compatibility.rb +23 -0
  11. data/lib/bitbucket_rest_api/configuration.rb +101 -0
  12. data/lib/bitbucket_rest_api/connection.rb +96 -0
  13. data/lib/bitbucket_rest_api/constants.rb +58 -0
  14. data/lib/bitbucket_rest_api/core_ext/array.rb +17 -0
  15. data/lib/bitbucket_rest_api/core_ext/hash.rb +56 -0
  16. data/lib/bitbucket_rest_api/core_ext/ordered_hash.rb +107 -0
  17. data/lib/bitbucket_rest_api/deprecation.rb +39 -0
  18. data/lib/bitbucket_rest_api/error/bad_request.rb +12 -0
  19. data/lib/bitbucket_rest_api/error/client_error.rb +20 -0
  20. data/lib/bitbucket_rest_api/error/forbidden.rb +12 -0
  21. data/lib/bitbucket_rest_api/error/internal_server_error.rb +12 -0
  22. data/lib/bitbucket_rest_api/error/invalid_options.rb +18 -0
  23. data/lib/bitbucket_rest_api/error/not_found.rb +12 -0
  24. data/lib/bitbucket_rest_api/error/required_params.rb +18 -0
  25. data/lib/bitbucket_rest_api/error/service_error.rb +19 -0
  26. data/lib/bitbucket_rest_api/error/service_unavailable.rb +12 -0
  27. data/lib/bitbucket_rest_api/error/unauthorized.rb +12 -0
  28. data/lib/bitbucket_rest_api/error/unknown_value.rb +18 -0
  29. data/lib/bitbucket_rest_api/error/unprocessable_entity.rb +12 -0
  30. data/lib/bitbucket_rest_api/error/validations.rb +18 -0
  31. data/lib/bitbucket_rest_api/error.rb +35 -0
  32. data/lib/bitbucket_rest_api/invitations.rb +15 -0
  33. data/lib/bitbucket_rest_api/issues/comments.rb +118 -0
  34. data/lib/bitbucket_rest_api/issues/components.rb +106 -0
  35. data/lib/bitbucket_rest_api/issues/milestones.rb +107 -0
  36. data/lib/bitbucket_rest_api/issues.rb +230 -0
  37. data/lib/bitbucket_rest_api/normalizer.rb +27 -0
  38. data/lib/bitbucket_rest_api/parameter_filter.rb +32 -0
  39. data/lib/bitbucket_rest_api/repos/changesets.rb +54 -0
  40. data/lib/bitbucket_rest_api/repos/following.rb +39 -0
  41. data/lib/bitbucket_rest_api/repos/keys.rb +87 -0
  42. data/lib/bitbucket_rest_api/repos/services.rb +103 -0
  43. data/lib/bitbucket_rest_api/repos/sources.rb +31 -0
  44. data/lib/bitbucket_rest_api/repos.rb +238 -0
  45. data/lib/bitbucket_rest_api/request/basic_auth.rb +31 -0
  46. data/lib/bitbucket_rest_api/request/jsonize.rb +46 -0
  47. data/lib/bitbucket_rest_api/request/oauth.rb +51 -0
  48. data/lib/bitbucket_rest_api/request.rb +67 -0
  49. data/lib/bitbucket_rest_api/response/helpers.rb +21 -0
  50. data/lib/bitbucket_rest_api/response/jsonize.rb +30 -0
  51. data/lib/bitbucket_rest_api/response/mashify.rb +24 -0
  52. data/lib/bitbucket_rest_api/response/raise_error.rb +31 -0
  53. data/lib/bitbucket_rest_api/response/xmlize.rb +26 -0
  54. data/lib/bitbucket_rest_api/response.rb +28 -0
  55. data/lib/bitbucket_rest_api/result.rb +140 -0
  56. data/lib/bitbucket_rest_api/user.rb +101 -0
  57. data/lib/bitbucket_rest_api/users/account.rb +53 -0
  58. data/lib/bitbucket_rest_api/users.rb +24 -0
  59. data/lib/bitbucket_rest_api/utils/url.rb +56 -0
  60. data/lib/bitbucket_rest_api/validations/format.rb +24 -0
  61. data/lib/bitbucket_rest_api/validations/presence.rb +25 -0
  62. data/lib/bitbucket_rest_api/validations/required.rb +24 -0
  63. data/lib/bitbucket_rest_api/validations/token.rb +43 -0
  64. data/lib/bitbucket_rest_api/validations.rb +25 -0
  65. data/lib/bitbucket_rest_api/version.rb +11 -0
  66. data/lib/bitbucket_rest_api.rb +91 -0
  67. metadata +338 -0
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Repos::Following < API
5
+
6
+ # List repo followers
7
+ #
8
+ # = Examples
9
+ # bitbucket = BitBucket.new :user => 'user-name', :repo => 'repo-name'
10
+ # bitbucket.repos.following.followers
11
+ # bitbucket.repos.following.followers { |watcher| ... }
12
+ #
13
+ def followers(user_name, repo_name, params={})
14
+ _update_user_repo_params(user_name, repo_name)
15
+ _validate_user_repo_params(user, repo) unless user? && repo?
16
+ normalize! params
17
+
18
+ response = get_request("/repositories/#{user}/#{repo.downcase}/followers/", params)
19
+ return response unless block_given?
20
+ response.each { |el| yield el }
21
+ end
22
+
23
+ # List repos being followed by the authenticated user
24
+ #
25
+ # = Examples
26
+ # bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
27
+ # bitbucket.repos.following.followed
28
+ #
29
+ def followed(*args)
30
+ params = args.extract_options!
31
+ normalize! params
32
+
33
+ response = get_request("/user/follows", params)
34
+ return response unless block_given?
35
+ response.each { |el| yield el }
36
+ end
37
+
38
+ end # Repos::Watching
39
+ end # BitBucket
@@ -0,0 +1,87 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Repos::Keys < API
5
+
6
+ VALID_KEY_PARAM_NAMES = %w[ label key ].freeze
7
+
8
+ # List deploy keys
9
+ #
10
+ # = Examples
11
+ # bitbucket = BitBucket.new
12
+ # bitbucket.repos.keys.list 'user-name', 'repo-name'
13
+ # bitbucket.repos.keys.list 'user-name', 'repo-name' { |key| ... }
14
+ #
15
+ def list(user_name, repo_name, params={})
16
+ _update_user_repo_params(user_name, repo_name)
17
+ _validate_user_repo_params(user, repo) unless user? && repo?
18
+ normalize! params
19
+
20
+ response = get_request("/repositories/#{user}/#{repo.downcase}/deploy-keys/", params)
21
+ return response unless block_given?
22
+ response.each { |el| yield el }
23
+ end
24
+ alias :all :list
25
+
26
+ # Create a key
27
+ #
28
+ # = Inputs
29
+ # * <tt>:title</tt> - Required string.
30
+ # * <tt>:key</tt> - Required string.
31
+ #
32
+ # = Examples
33
+ # bitbucket = BitBucket.new
34
+ # bitbucket.repos.keys.create 'user-name', 'repo-name',
35
+ # "label" => "octocat@octomac",
36
+ # "key" => "ssh-rsa AAA..."
37
+ #
38
+ def create(user_name, repo_name, params={})
39
+ _update_user_repo_params(user_name, repo_name)
40
+ _validate_user_repo_params(user, repo) unless user? && repo?
41
+ normalize! params
42
+ filter! VALID_KEY_PARAM_NAMES, params
43
+ assert_required_keys(VALID_KEY_PARAM_NAMES, params)
44
+
45
+ post_request("/repositories/#{user}/#{repo.downcase}/deploy-keys/", params)
46
+ end
47
+
48
+ # Edit a key
49
+ #
50
+ # = Inputs
51
+ # * <tt>:title</tt> - Required string.
52
+ # * <tt>:key</tt> - Required string.
53
+ #
54
+ # = Examples
55
+ # bitbucket = BitBucket.new
56
+ # bitbucket.repos.keys.edit 'user-name', 'repo-name',
57
+ # "label" => "octocat@octomac",
58
+ # "key" => "ssh-rsa AAA..."
59
+ #
60
+ def edit(user_name, repo_name, key_id, params={})
61
+ _update_user_repo_params(user_name, repo_name)
62
+ _validate_user_repo_params(user, repo) unless user? && repo?
63
+ _validate_presence_of key_id
64
+
65
+ normalize! params
66
+ filter! VALID_KEY_PARAM_NAMES, params
67
+
68
+ put_request("/repositories/#{user}/#{repo.downcase}/deploy-keys/#{key_id}", params)
69
+ end
70
+
71
+ # Delete key
72
+ #
73
+ # = Examples
74
+ # @bitbucket = BitBucket.new
75
+ # @bitbucket.repos.keys.delete 'user-name', 'repo-name', 'key-id'
76
+ #
77
+ def delete(user_name, repo_name, key_id, params={})
78
+ _update_user_repo_params(user_name, repo_name)
79
+ _validate_user_repo_params(user, repo) unless user? && repo?
80
+ _validate_presence_of key_id
81
+ normalize! params
82
+
83
+ delete_request("/repositories/#{user}/#{repo.downcase}/deploy-keys/#{key_id}", params)
84
+ end
85
+
86
+ end # Repos::Keys
87
+ end # BitBucket
@@ -0,0 +1,103 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Repos::Services < API
5
+
6
+ REQUIRED_KEY_PARAM_NAMES = %w[ type ].freeze
7
+
8
+ # List services
9
+ #
10
+ # = Examples
11
+ # bitbucket = BitBucket.new
12
+ # bitbucket.repos.services.list 'user-name', 'repo-name'
13
+ # bitbucket.repos.services.list 'user-name', 'repo-name' { |service| ... }
14
+ #
15
+ def list(user_name, repo_name, params={})
16
+ _update_user_repo_params(user_name, repo_name)
17
+ _validate_user_repo_params(user, repo) unless user? && repo?
18
+ normalize! params
19
+
20
+ response = get_request("/repositories/#{user}/#{repo.downcase}/services", params)
21
+ return response unless block_given?
22
+ response.each { |el| yield el }
23
+ end
24
+ alias :all :list
25
+
26
+ # Gets a single service
27
+ #
28
+ # = Examples
29
+ # @bitbucket = BitBucket.new
30
+ # @bitbucket.repos.services.get 'user-name', 'repo-name', 109172378)
31
+ #
32
+ def get(user_name, repo_name, service_id, params={})
33
+ _update_user_repo_params(user_name, repo_name)
34
+ _validate_user_repo_params(user, repo) unless user? && repo?
35
+ _validate_presence_of(service_id)
36
+ normalize! params
37
+
38
+ get_request("/repositories/#{user}/#{repo.downcase}/services/#{service_id}", params)
39
+ end
40
+ alias :find :get
41
+
42
+ # Create a service
43
+ #
44
+ # = Inputs
45
+ # * <tt>:type</tt> - One of the supported services. The type is a case-insensitive value.
46
+ #
47
+ # = Examples
48
+ # bitbucket = BitBucket.new
49
+ # bitbucket.repos.services.create 'user-name', 'repo-name',
50
+ # "type" => "Basecamp",
51
+ # "Password" => "...",
52
+ # "Username" => "...",
53
+ # "Discussion URL" => "..."
54
+ #
55
+ def create(user_name, repo_name, params={})
56
+ _update_user_repo_params(user_name, repo_name)
57
+ _validate_user_repo_params(user, repo) unless user? && repo?
58
+ normalize! params
59
+ assert_required_keys(REQUIRED_KEY_PARAM_NAMES, params)
60
+
61
+ post_request("/repositories/#{user}/#{repo.downcase}/services", params)
62
+ end
63
+
64
+ # Edit a service
65
+ #
66
+ # = Inputs
67
+ # * <tt>:type</tt> - One of the supported services. The type is a case-insensitive value.
68
+ #
69
+ # = Examples
70
+ # bitbucket = BitBucket.new
71
+ # bitbucket.repos.services.edit 'user-name', 'repo-name', 109172378,
72
+ # "type" => "Basecamp",
73
+ # "Password" => "...",
74
+ # "Username" => "...",
75
+ # "Discussion URL" => "..."
76
+ #
77
+ def edit(user_name, repo_name, service_id, params={})
78
+ _update_user_repo_params(user_name, repo_name)
79
+ _validate_user_repo_params(user, repo) unless user? && repo?
80
+ _validate_presence_of(service_id)
81
+
82
+ normalize! params
83
+
84
+ put_request("/repositories/#{user}/#{repo.downcase}/services/#{service_id}", params)
85
+ end
86
+
87
+ # Delete service
88
+ #
89
+ # = Examples
90
+ # @bitbucket = BitBucket.new
91
+ # @bitbucket.repos.services.delete 'user-name', 'repo-name', 109172378
92
+ #
93
+ def delete(user_name, repo_name, service_id, params={})
94
+ _update_user_repo_params(user_name, repo_name)
95
+ _validate_user_repo_params(user, repo) unless user? && repo?
96
+ _validate_presence_of(service_id)
97
+ normalize! params
98
+
99
+ delete_request("/repositories/#{user}/#{repo.downcase}/services/#{service_id}", params)
100
+ end
101
+
102
+ end # Repos::Keys
103
+ end # BitBucket
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Repos::Sources < API
5
+
6
+ REQUIRED_COMMENT_PARAMS = %w[
7
+ body
8
+ changeset_id
9
+ line
10
+ path
11
+ position
12
+ ].freeze
13
+
14
+ # Gets a source of repo
15
+ #
16
+ # = Examples
17
+ # @bitbucket = BitBucket.new
18
+ # @bitbucket.repos.sources.get 'user-name', 'repo-name', '6dcb09b5b57875f334f61aebed6', 'app/contorllers/')
19
+ #
20
+ def get(user_name, repo_name, sha, path, params={})
21
+ _update_user_repo_params(user_name, repo_name)
22
+ _validate_user_repo_params(user, repo) unless user? && repo?
23
+ _validate_presence_of sha
24
+ normalize! params
25
+
26
+ get_request("/repositories/#{user}/#{repo.downcase}/src/#{sha}/#{path}", params)
27
+ end
28
+ alias :find :get
29
+
30
+ end # Repos::Sources
31
+ end # BitBucket
@@ -0,0 +1,238 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Repos < API
5
+ extend AutoloadHelper
6
+
7
+ # Load all the modules after initializing Repos to avoid superclass mismatch
8
+ autoload_all 'bitbucket_rest_api/repos',
9
+ :Changesets => 'changesets',
10
+ :Keys => 'keys',
11
+ :Services => 'services',
12
+ :Following => 'following',
13
+ :Sources => 'sources'
14
+
15
+ DEFAULT_REPO_OPTIONS = {
16
+ "website" => "",
17
+ "is_private" => false,
18
+ "has_issues" => false,
19
+ "has_wiki" => false,
20
+ "scm" => "git",
21
+ "no_public_forks" => false
22
+ }.freeze
23
+
24
+ VALID_REPO_OPTIONS = %w[
25
+ owner
26
+ name
27
+ description
28
+ website
29
+ is_private
30
+ has_issues
31
+ has_wiki
32
+ no_public_forks
33
+ language
34
+ scm
35
+ ].freeze
36
+
37
+ # Creates new Repositories API
38
+ def initialize(options = { })
39
+ super(options)
40
+ end
41
+
42
+ # Access to Repos::Commits API
43
+ def changesets
44
+ @changesets ||= ApiFactory.new 'Repos::Changesets'
45
+ end
46
+
47
+ # Access to Repos::Keys API
48
+ def keys
49
+ @keys ||= ApiFactory.new 'Repos::Keys'
50
+ end
51
+
52
+ # Access to Repos::Watchin API
53
+ def following
54
+ @following ||= ApiFactory.new 'Repos::Following'
55
+ end
56
+
57
+ # Access to Repos::Commits API
58
+ def sources
59
+ @sources ||= ApiFactory.new 'Repos::Sources'
60
+ end
61
+
62
+ # Access to Repos::Services API
63
+ def services
64
+ @services ||= ApiFactory.new 'Repos::Services'
65
+ end
66
+
67
+ # List branches
68
+ #
69
+ # = Examples
70
+ #
71
+ # bitbucket = BitBucket.new
72
+ # bibucket.repos.branches 'user-name', 'repo-name'
73
+ #
74
+ # repos = BitBucket::Repos.new
75
+ # repos.branches 'user-name', 'repo-name'
76
+ #
77
+ def branches(user_name, repo_name, params={ })
78
+ _update_user_repo_params(user_name, repo_name)
79
+ _validate_user_repo_params(user, repo) unless (user? && repo?)
80
+ normalize! params
81
+
82
+ response = get_request("/repositories/#{user}/#{repo.downcase}/branches/", params)
83
+ return response unless block_given?
84
+ response.each { |el| yield el }
85
+ end
86
+
87
+ alias :list_branches :branches
88
+
89
+ # Create a new repository for the authenticated user.
90
+ #
91
+ # = Parameters
92
+ # <tt>:name</tt> - Required string
93
+ # <tt>:description</tt> - Optional string
94
+ # <tt>:website</tt> - Optional string
95
+ # <tt>:is_private</tt> - Optional boolean - <tt>true</tt> to create a private repository, <tt>false</tt> to create a public one.
96
+ # <tt>:has_issues</tt> - Optional boolean - <tt>true</tt> to enable issues for this repository, <tt>false</tt> to disable them
97
+ # <tt>:has_wiki</tt> - Optional boolean - <tt>true</tt> to enable the wiki for this repository, <tt>false</tt> to disable it. Default is <tt>true</tt>
98
+ # <tt>:owner</tt> Optional string - The team in which this repository will be created
99
+ #
100
+ # = Examples
101
+ # bitbucket = BitBucket.new
102
+ # bitbucket.repos.create "name" => 'repo-name'
103
+ # "description": "This is your first repo",
104
+ # "website": "https://bitbucket.com",
105
+ # "is_private": false,
106
+ # "has_issues": true,
107
+ # "has_wiki": true
108
+ #
109
+ # Create a new repository in this team. The authenticated user
110
+ # must be a member of this team
111
+ #
112
+ # Examples:
113
+ # bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
114
+ # bitbucket.repos.create :name => 'repo-name', :owner => 'team-name'
115
+ #
116
+ def create(*args)
117
+ params = args.extract_options!
118
+ normalize! params
119
+ filter! VALID_REPO_OPTIONS + %w[ org ], params
120
+ assert_required_keys(%w[ name ], params)
121
+
122
+ # Requires authenticated user
123
+ post_request("/repositories/", DEFAULT_REPO_OPTIONS.merge(params))
124
+ end
125
+
126
+ # Edit a repository
127
+ #
128
+ # = Parameters
129
+ # * <tt>:name</tt> Required string
130
+ # * <tt>:description</tt> Optional string
131
+ # * <tt>:website</tt> Optional string
132
+ # * <tt>:private</tt> - Optional boolean - <tt>false</tt> to create public reps, <tt>false</tt> to create a private one
133
+ # * <tt>:has_issues</tt> Optional boolean - <tt>true</tt> to enable issues for this repository, <tt>false</tt> to disable them
134
+ # * <tt>:has_wiki</tt> Optional boolean - <tt>true</tt> to enable the wiki for this repository, <tt>false</tt> to disable it. Default is <tt>true</tt>
135
+ # * <tt>:has_downloads</tt> Optional boolean - <tt>true</tt> to enable downloads for this repository
136
+ #
137
+ # = Examples
138
+ #
139
+ # bitbucket = BitBucket.new
140
+ # bitbucket.repos.edit 'user-name', 'repo-name',
141
+ # :name => 'hello-world',
142
+ # :description => 'This is your first repo',
143
+ # :website => "https://bitbucket.com",
144
+ # :public => true, :has_issues => true
145
+ #
146
+ def edit(user_name, repo_name, params={ })
147
+ _update_user_repo_params(user_name, repo_name)
148
+ _validate_user_repo_params(user, repo) unless user? && repo?
149
+
150
+ normalize! params
151
+ filter! VALID_REPO_OPTIONS, params
152
+
153
+ put_request("/repositories/#{user}/#{repo.downcase}/", DEFAULT_REPO_OPTIONS.merge(params))
154
+ end
155
+
156
+ # Get a repository
157
+ #
158
+ # = Examples
159
+ # bitbucket = BitBucket.new
160
+ # bitbucket.repos.get 'user-name', 'repo-name'
161
+ #
162
+ def get(user_name, repo_name, params={ })
163
+ _update_user_repo_params(user_name, repo_name)
164
+ _validate_user_repo_params(user, repo) unless user? && repo?
165
+ normalize! params
166
+
167
+ get_request("/repositories/#{user}/#{repo.downcase}", params)
168
+ end
169
+
170
+ alias :find :get
171
+
172
+ # Delete a repository
173
+ #
174
+ # = Examples
175
+ # @bitbucket = BitBucket.new
176
+ # @bitbucket.repos.delete 'user-name', 'repo-name'
177
+ #
178
+ def delete(user_name, repo_name)
179
+ _update_user_repo_params(user_name, repo_name)
180
+ _validate_user_repo_params(user, repo) unless user? && repo?
181
+
182
+ delete_request("/repositories/#{user}/#{repo.downcase}")
183
+ end
184
+
185
+ # List repositories for the authenticated user
186
+ #
187
+ # = Examples
188
+ # bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
189
+ # bitbucket.repos.list
190
+ # bitbucket.repos.list { |repo| ... }
191
+ #
192
+ # List public repositories for the specified user.
193
+ #
194
+ # = Examples
195
+ # bitbucket = BitBucket.new
196
+ # bitbucket.repos.list :user => 'user-name'
197
+ # bitbucket.repos.list :user => 'user-name', { |repo| ... }
198
+ def list(*args)
199
+ params = args.extract_options!
200
+ normalize! params
201
+ _merge_user_into_params!(params) unless params.has_key?('user')
202
+ filter! %w[ user type ], params
203
+
204
+ response = #if (user_name = params.delete("user"))
205
+ # get_request("/users/#{user_name}", params)
206
+ #else
207
+ # For authenticated user
208
+ get_request("/user/repositories", params)
209
+ #end
210
+ return response unless block_given?
211
+ response.each { |el| yield el }
212
+ end
213
+
214
+ alias :all :list
215
+
216
+ # List tags
217
+ #
218
+ # = Examples
219
+ # bitbucket = BitBucket.new
220
+ # bitbucket.repos.tags 'user-name', 'repo-name'
221
+ # bitbucket.repos.tags 'user-name', 'repo-name' { |tag| ... }
222
+ #
223
+ def tags(user_name, repo_name, params={ })
224
+ _update_user_repo_params(user_name, repo_name)
225
+ _validate_user_repo_params(user, repo) unless user? && repo?
226
+ normalize! params
227
+
228
+ response = get_request("/repositories/#{user}/#{repo.downcase}/tags/", params)
229
+ return response unless block_given?
230
+ response.each { |el| yield el }
231
+ end
232
+
233
+ alias :list_tags :tags
234
+ alias :repo_tags :tags
235
+ alias :repository_tags :tags
236
+
237
+ end # Repos
238
+ end # BitBucket
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+ require 'base64'
5
+
6
+ module BitBucket
7
+ module Request
8
+ class BasicAuth < Faraday::Middleware
9
+ dependency 'base64'
10
+
11
+ def call(env)
12
+ env[:request_headers].merge!('Authorization' => "Basic #{@auth}\"")
13
+
14
+ @app.call env
15
+ end
16
+
17
+ def initialize(app, *args)
18
+ @app = app
19
+ credentials = ""
20
+ options = args.extract_options!
21
+ if options.has_key? :login
22
+ credentials = "#{options[:login]}:#{options[:password]}"
23
+ elsif options.has_key? :basic_auth
24
+ credentials = "#{options[:basic_auth]}"
25
+ end
26
+ @auth = Base64.encode64(credentials)
27
+ @auth.gsub!("\n", "")
28
+ end
29
+ end # BasicAuth
30
+ end # Request
31
+ end # BitBucket
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module BitBucket
6
+ class Request::Jsonize < Faraday::Middleware
7
+
8
+ CONTENT_TYPE = 'Content-Type'.freeze
9
+ MIME_TYPE = 'application/json'.freeze
10
+
11
+ dependency 'multi_json'
12
+
13
+ def call(env)
14
+ if request_with_body?(env)
15
+ env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
16
+ env[:body] = encode_body env unless env[:body].respond_to?(:to_str)
17
+ end
18
+ @app.call env
19
+ end
20
+
21
+ def encode_body(env)
22
+ if MultiJson.respond_to?(:dump)
23
+ MultiJson.dump env[:body]
24
+ else
25
+ MultiJson.encode env[:body]
26
+ end
27
+ end
28
+
29
+ def request_with_body?(env)
30
+ type = request_type(env)
31
+ has_body?(env) and (type.empty? or type == MIME_TYPE)
32
+ end
33
+
34
+ # Don't encode bodies in string form
35
+ def has_body?(env)
36
+ body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
37
+ end
38
+
39
+ def request_type(env)
40
+ type = env[:request_headers][CONTENT_TYPE].to_s
41
+ type = type.split(';', 2).first if type.index(';')
42
+ type
43
+ end
44
+
45
+ end # Request::Jsonize
46
+ end # BitBucket
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module BitBucket
6
+ module Request
7
+ class OAuth < Faraday::Middleware
8
+ include BitBucket::Utils::Url
9
+
10
+ AUTH_HEADER = 'Authorization'.freeze
11
+
12
+ dependency 'simple_oauth'
13
+
14
+ def call(env)
15
+ # Extract parameters from the query
16
+ params = { }.update query_params(env[:url])
17
+
18
+ if (@token and @secret) and (!@token.empty? and !@secret.empty?)
19
+ access_token = ::OAuth::AccessToken.new(@consumer, @token, @secret)
20
+ env[:url].query = build_query params
21
+
22
+ puts oauth_helper.header
23
+ puts oauth_helper.header.class
24
+ env[:request_headers].merge!(AUTH_HEADER => oauth_helper.header)
25
+ end
26
+
27
+ env[:url].query = build_query params
28
+
29
+
30
+
31
+ @app.call env
32
+ end
33
+
34
+ def initialize(app, *args)
35
+ super app
36
+ @app = app
37
+ @consumer = args.shift
38
+ @token = args.shift
39
+ @secret = args.shift
40
+ end
41
+
42
+ def query_params(url)
43
+ if url.query.nil? or url.query.empty?
44
+ {}
45
+ else
46
+ parse_query url.query
47
+ end
48
+ end
49
+ end # OAuth
50
+ end # Request
51
+ end # BitBucket
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ # Defines HTTP verbs
5
+ module Request
6
+
7
+ METHODS = [:get, :post, :put, :delete, :patch]
8
+ METHODS_WITH_BODIES = [ :post, :put, :patch ]
9
+
10
+ def get_request(path, params={}, options={})
11
+ request(:get, path, params, options)
12
+ end
13
+
14
+ def patch_request(path, params={}, options={})
15
+ request(:patch, path, params, options)
16
+ end
17
+
18
+ def post_request(path, params={}, options={})
19
+ request(:post, path, params, options)
20
+ end
21
+
22
+ def put_request(path, params={}, options={})
23
+ request(:put, path, params, options)
24
+ end
25
+
26
+ def delete_request(path, params={}, options={})
27
+ request(:delete, path, params, options)
28
+ end
29
+
30
+ def request(method, path, params, options)
31
+ if !METHODS.include?(method)
32
+ raise ArgumentError, "unkown http method: #{method}"
33
+ end
34
+ # _extract_mime_type(params, options)
35
+
36
+ puts "EXECUTED: #{method} - #{path} with #{params} and #{options}" if ENV['DEBUG']
37
+
38
+ conn = connection(options)
39
+ path = (conn.path_prefix + path).gsub(/\/\//,'/') if conn.path_prefix != '/'
40
+
41
+ response = conn.send(method) do |request|
42
+ case method.to_sym
43
+ when *(METHODS - METHODS_WITH_BODIES)
44
+ request.body = params.delete('data') if params.has_key?('data')
45
+ request.url(path, params)
46
+ when *METHODS_WITH_BODIES
47
+ request.path = path
48
+ request.body = extract_data_from_params(params) unless params.empty?
49
+ end
50
+ end
51
+ response.body
52
+ end
53
+
54
+ private
55
+
56
+ def extract_data_from_params(params) # :nodoc:
57
+ return params['data'] if params.has_key?('data') and !params['data'].nil?
58
+ return params
59
+ end
60
+
61
+ def _extract_mime_type(params, options) # :nodoc:
62
+ options['resource'] = params['resource'] ? params.delete('resource') : ''
63
+ options['mime_type'] = params['resource'] ? params.delete('mime_type') : ''
64
+ end
65
+
66
+ end # Request
67
+ end # BitBucket