github_api 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,4 +1,4 @@
1
- = github
1
+ = github {<img src="http://travis-ci.org/peter-murach/github.png?branch=master" />}[http://travis-ci.org/peter-murach/github]
2
2
 
3
3
  A Ruby wrapper for the GitHub REST API v3.
4
4
 
@@ -8,11 +8,11 @@ Supports all the API methods(nearly 200). It's build in a modular way, that is,
8
8
 
9
9
  Grab the gem by issuing
10
10
 
11
- gem install github_api --pre
11
+ gem install github_api
12
12
 
13
13
  or in your Gemfile
14
14
 
15
- gem "github_api", "~> 0.1.0.pre"
15
+ gem "github_api", "~> 0.1.1"
16
16
 
17
17
  == Usage
18
18
 
@@ -20,7 +20,7 @@ Create a new client instance
20
20
 
21
21
  @github = Github.new
22
22
 
23
- At this stage you can also supply various configuration parameters, such as :user, :repo, :org etc..,
23
+ At this stage you can also supply various configuration parameters, such as :user, :repo, :org, :oauth_token, :login, :password or :basic_auth
24
24
  which are used thoughtout the API
25
25
 
26
26
  @github = Github.new :user => 'peter-murach', :repo => 'github-api'
@@ -33,6 +33,13 @@ In order to authenticate the user through OAuth2 on GitHub you need to
33
33
 
34
34
  Once you have your consumer and token keys, configure your github instance following instructions under Configuration.
35
35
 
36
+ You can also use basic authentication by passing your login and password credentials
37
+ @github = Github.new :login => 'peter-murach', :password => '...'
38
+
39
+ or use convenience method:
40
+
41
+ @github = Github.new :basic_auth => 'login:password'
42
+
36
43
  You can interact with GitHub interface, for example repositories, by issueing following calls
37
44
 
38
45
  @github.repos.commits
@@ -115,11 +122,13 @@ register an app at https://github.com/account/applications/
115
122
 
116
123
  Github.configure do |config|
117
124
  config.oauth_token = YOUR_OAUTH_ACCESS_TOKEN
125
+ config.basic_auth = 'login:password'
118
126
  end
119
127
 
120
128
  or
121
129
 
122
130
  Github.new(:oauth_token => YOUR_OAUTH_TOKEN)
131
+ Github.new(:basic_auth => 'login:password)
123
132
 
124
133
  All parameters can be overwirtten as per method call. By passing parameters hash...
125
134
 
data/lib/github_api.rb CHANGED
@@ -50,6 +50,7 @@ module Github
50
50
  :GitData => 'git_data',
51
51
  :Orgs => 'orgs',
52
52
  :PullRequests => 'pull_requests',
53
- :Users => 'users'
53
+ :Users => 'users',
54
+ :CoreExt => 'core_ext'
54
55
 
55
56
  end # Github
@@ -3,6 +3,8 @@
3
3
  require 'github_api/configuration'
4
4
  require 'github_api/connection'
5
5
  require 'github_api/request'
6
+ require 'github_api/core_ext/hash'
7
+ require 'github_api/core_ext/array'
6
8
 
7
9
  module Github
8
10
  # @private
@@ -34,13 +36,26 @@ module Github
34
36
  Configuration::VALID_OPTIONS_KEYS.each do |key|
35
37
  send("#{key}=", options[key])
36
38
  end
39
+ _process_basic_auth(options[:basic_auth])
37
40
  @cached = Hash.new
38
41
  end
39
42
 
40
- private
43
+ private
44
+
45
+ # Extract login and password from basic_auth parameter
46
+ def _process_basic_auth(auth)
47
+ case auth
48
+ when String
49
+ login = auth.split(':').first
50
+ password = auth.split(':').last
51
+ when Hash
52
+ login = auth[:login]
53
+ password = auth[:password]
54
+ end
55
+ end
41
56
 
42
57
  # Responds to attribute query
43
- def method_missing(method, *args, &block)
58
+ def method_missing(method, *args, &block) # :nodoc:
44
59
  if method.to_s =~ /^(.*)\?$/
45
60
  return !self.send($1.to_s).nil?
46
61
  else
@@ -48,38 +63,38 @@ module Github
48
63
  end
49
64
  end
50
65
 
51
- def _validate_inputs(required, provided)
66
+ def _validate_inputs(required, provided) # :nodoc:
52
67
  required.all? do |key|
53
68
  provided.has_key? key
54
69
  end
55
70
  end
56
71
 
57
- def _validate_presence_of(*params)
72
+ def _validate_presence_of(*params) # :nodoc:
58
73
  params.each do |param|
59
74
  raise ArgumentError, "parameter cannot be nil" if param.nil?
60
75
  end
61
76
  end
62
77
 
63
- def _validate_user_repo_params(user_name, repo_name)
78
+ def _validate_user_repo_params(user_name, repo_name) # :nodoc:
64
79
  raise ArgumentError, "[user] parameter cannot be nil" if user_name.nil?
65
80
  raise ArgumentError, "[repo] parameter cannot be nil" if repo_name.nil?
66
81
  end
67
82
 
68
- def _update_user_repo_params(user_name, repo_name=nil)
83
+ def _update_user_repo_params(user_name, repo_name=nil) # :nodoc:
69
84
  self.user = user_name || self.user
70
85
  self.repo = repo_name || self.repo
71
86
  end
72
87
 
73
- def _merge_user_into_params!(params)
88
+ def _merge_user_into_params!(params) # :nodoc:
74
89
  params.merge!({ 'user' => self.user }) if user?
75
90
  end
76
91
 
77
- def _merge_user_repo_into_params!(params)
92
+ def _merge_user_repo_into_params!(params) # :nodoc:
78
93
  { 'user' => self.user, 'repo' => self.repo }.merge!(params)
79
94
  end
80
95
 
81
96
  # Turns any keys from nested hashes including nested arrays into strings
82
- def _normalize_params_keys(params)
97
+ def _normalize_params_keys(params) # :nodoc:
83
98
  case params
84
99
  when Hash
85
100
  params.keys.each do |k|
@@ -96,7 +111,7 @@ module Github
96
111
  return params
97
112
  end
98
113
 
99
- def _filter_params_keys(keys, params)
114
+ def _filter_params_keys(keys, params) # :nodoc:
100
115
  params.reject! { |k,v| !keys.include? k }
101
116
  end
102
117
 
@@ -117,7 +132,7 @@ module Github
117
132
  return hash
118
133
  end
119
134
 
120
- def _validate_params_values(options, params)
135
+ def _validate_params_values(options, params) # :nodoc:
121
136
  params.each do |k, v|
122
137
  next unless options.keys.include?(k)
123
138
  if options[k].is_a?(Array) && !options[k].include?(params[k])
@@ -131,6 +146,7 @@ module Github
131
146
  def _merge_parameters(params)
132
147
  end
133
148
 
149
+ # TODO add to core extensions
134
150
  def _extract_parameters(array)
135
151
  if array.last.is_a?(Hash) && array.last.instance_of?(Hash)
136
152
  pop
@@ -140,9 +156,8 @@ module Github
140
156
  end
141
157
 
142
158
  # Passes configuration options to instantiated class
143
- # TODO implement
144
- # @private
145
- def _create_instance(klass)
159
+ def _create_instance(klass, options)
160
+ options.symbolize_keys!
146
161
  klass.new(options)
147
162
  end
148
163
 
@@ -4,36 +4,39 @@ module Github
4
4
  class Client < API
5
5
 
6
6
  def gists(options = {})
7
- @gists ||= Github::Gists.new(options)
7
+ @gists ||= _create_instance Github::Gists, options
8
8
  end
9
9
 
10
10
  # The Git Database API gives you access to read and write raw Git objects
11
11
  # to your Git database on GitHub and to list and update your references
12
12
  # (branch heads and tags).
13
13
  def git_data(options = {})
14
- @git_data ||= Github::GitData.new(options)
14
+ @git_data ||= _create_instance Github::GitData, options
15
15
  end
16
+ alias :git :git_data
16
17
 
17
18
  def issues(options = {})
18
- @issues ||= Github::Issues.new(options)
19
+ @issues ||= _create_instance Github::Issues, options
19
20
  end
20
21
 
21
22
  def orgs(options = {})
22
- @orgs ||= Github::Orgs.new(options)
23
+ @orgs ||= _create_instance Github::Orgs, options
23
24
  end
25
+ alias :organizations :orgs
24
26
 
25
27
  def pull_requests(options = {})
26
- @pull_requests ||= Github::PullRequests.new(options)
28
+ @pull_requests ||= _create_instance Github::PullRequests, options
27
29
  end
28
30
 
29
31
  def repos(options = {})
30
- @repos ||= Github::Repos.new(options)
32
+ @repos ||= _create_instance Github::Repos, options
31
33
  end
34
+ alias :repositories :repos
32
35
 
33
36
  # Many of the resources on the users API provide a shortcut for getting
34
37
  # information about the currently authenticated user.
35
38
  def users(options = {})
36
- @users ||= Github::Users.new(options)
39
+ @users ||= _create_instance Github::Users, options
37
40
  end
38
41
 
39
42
  end # Client
@@ -14,7 +14,10 @@ module Github
14
14
  :user_agent,
15
15
  :faraday_options,
16
16
  :repo,
17
- :user
17
+ :user,
18
+ :login,
19
+ :password,
20
+ :basic_auth
18
21
  ].freeze
19
22
 
20
23
  # Other adapters are :typhoeus, :patron, :em_synchrony, :excon, :test
@@ -29,6 +32,15 @@ module Github
29
32
  # By default, don't set a user oauth access token
30
33
  DEFAULT_OAUTH_TOKEN = nil
31
34
 
35
+ # By default, don't set a user login name
36
+ DEFAULT_LOGIN = nil
37
+
38
+ # By default, don't set a user password
39
+ DEFAULT_PASSWORD = nil
40
+
41
+ # By default, don't set a user basic authentication
42
+ DEFAULT_BASIC_AUTH = nil
43
+
32
44
  # The endpoint used to connect to GitHub if none is set
33
45
  DEFAULT_ENDPOINT = 'https://api.github.com'.freeze
34
46
 
@@ -77,6 +89,9 @@ module Github
77
89
  self.resource = DEFAULT_RESOURCE
78
90
  self.user = DEFAULT_USER
79
91
  self.repo = DEFAULT_REPO
92
+ self.login = DEFAULT_LOGIN
93
+ self.password = DEFAULT_PASSWORD
94
+ self.basic_auth = DEFAULT_BASIC_AUTH
80
95
  self
81
96
  end
82
97
 
@@ -6,6 +6,7 @@ require 'github_api/response/mashify'
6
6
  require 'github_api/response/jsonize'
7
7
  require 'github_api/response/raise_error'
8
8
  require 'github_api/request/oauth2'
9
+ require 'github_api/request/basic_auth'
9
10
 
10
11
  module Github
11
12
  module Connection
@@ -73,6 +74,7 @@ module Github
73
74
  builder.use Faraday::Response::Logger
74
75
 
75
76
  builder.use Github::Request::OAuth2, oauth_token if oauth_token?
77
+ builder.use Github::Request::BasicAuth, login, password if login? && password?
76
78
 
77
79
  unless options[:raw]
78
80
  builder.use Github::Response::Mashify
@@ -83,7 +85,6 @@ module Github
83
85
  builder.adapter adapter
84
86
  end
85
87
  end
86
-
87
88
  end
88
89
 
89
90
  end # Connection
@@ -0,0 +1,14 @@
1
+ class Array # :nodoc:
2
+
3
+ def except(*keys) # :nodoc:
4
+ puts "module except works!!!"
5
+ self.dup.except!(*keys)
6
+ end unless method_defined?(:except)
7
+
8
+ def except!(*items) # :nodoc:
9
+ copy = self.dup
10
+ copy.reject! { |item| items.include? item }
11
+ copy
12
+ end unless method_defined?(:except!)
13
+
14
+ end # Hash
@@ -0,0 +1,38 @@
1
+ class Hash # :nodoc:
2
+
3
+ def except(*items) # :nodoc:
4
+ puts "array except works!!!"
5
+ self.dup.except!(*items)
6
+ end unless method_defined?(:except)
7
+
8
+ def except!(*keys) # :nodoc:
9
+ copy = self.dup
10
+ keys.each { |key| copy.delete!(key) }
11
+ copy
12
+ end unless method_defined?(:except!)
13
+
14
+ def symbolize_keys # :nodoc:
15
+ inject({}) do |hash, (key, value)|
16
+ hash[(key.to_sym rescue key) || key] = value
17
+ hash
18
+ end
19
+ end
20
+
21
+ def symbolize_keys! # :nodoc:
22
+ hash = symbolize_keys
23
+ hash.each do |key, val|
24
+ hash[key] = case val
25
+ when Hash
26
+ val.symbolize_keys!
27
+ when Array
28
+ val.map do |item|
29
+ item.is_a?(Hash) ? item.symbolize_keys! : item
30
+ end
31
+ else
32
+ val
33
+ end
34
+ end
35
+ return hash
36
+ end unless method_defined?(:symbolize_keys!)
37
+
38
+ end # Hash
@@ -42,7 +42,7 @@ module Github
42
42
 
43
43
  VALID_REPO_TYPES = %w[ all public private member ]
44
44
 
45
- # Creates new Repos API
45
+ # Creates new Repositories API
46
46
  def initialize(options = {})
47
47
  super(options)
48
48
  end
@@ -60,11 +60,13 @@ module Github
60
60
  def branches(user_name=nil, repo_name=nil, params={})
61
61
  _update_user_repo_params(user_name, repo_name)
62
62
  _validate_user_repo_params(user, repo) unless (user? && repo?)
63
+ _normalize_params_keys(params)
63
64
 
64
65
  response = get("/repos/#{user}/#{repo}/branches", params)
65
66
  return response unless block_given?
66
67
  response.each { |el| yield el }
67
68
  end
69
+ alias :list_branches :branches
68
70
 
69
71
  # Create a new repository for the autheticated user.
70
72
  #
@@ -79,14 +81,20 @@ module Github
79
81
  #
80
82
  # = Examples
81
83
  # @github = Github.new
82
- # @github.repos.create_repo :name => 'my_repo_name'
84
+ # @github.repos.create_repo "name" => 'repo-name'
85
+ # "description": "This is your first repo",
86
+ # "homepage": "https://github.com",
87
+ # "public": true,
88
+ # "has_issues": true,
89
+ # "has_wiki": true,
90
+ # "has_downloads": true
83
91
  #
84
- # Create a new repository in this organisation. The authenticated user
92
+ # Create a new repository in this organisation. The authenticated user
85
93
  # must be a member of this organisation
86
94
  #
87
95
  # Examples:
88
96
  # @github = Github.new :oauth_token => '...'
89
- # @github.repos.create_repo(:name => 'my-repo-name', :org => 'my-organisation')
97
+ # @github.repos.create_repo :name => 'repo-name', :org => 'organisation-name'
90
98
  #
91
99
  def create_repo(*args)
92
100
  params = args.last.is_a?(Hash) ? args.pop : {}
@@ -102,6 +110,7 @@ module Github
102
110
  post("/user/repos", DEFAULT_REPO_OPTIONS.merge(params))
103
111
  end
104
112
  end
113
+ alias :create_repository :create_repo
105
114
 
106
115
  # List contributors
107
116
  #
@@ -111,8 +120,8 @@ module Github
111
120
  # = Examples
112
121
  #
113
122
  # @github = Github.new
114
- # @github.repos.contributors('user-name','repo-name')
115
- # @github.repos.contributors('user-name','repo-name') { |cont| ... }
123
+ # @github.repos.contributors 'user-name','repo-name'
124
+ # @github.repos.contributors 'user-name','repo-name' { |cont| ... }
116
125
  #
117
126
  def contributors(user_name=nil, repo_name=nil, params={})
118
127
  _update_user_repo_params(user_name, repo_name)
@@ -124,6 +133,8 @@ module Github
124
133
  return response unless block_given?
125
134
  response.each { |el| yield el }
126
135
  end
136
+ alias :list_contributors :contributors
137
+ alias :contribs :contributors
127
138
 
128
139
  # Edit a repository
129
140
  #
@@ -141,7 +152,7 @@ module Github
141
152
  # @github = Github.new
142
153
  # @github.repos.edit_repo('user-name', 'repo-name', { :name => 'hello-world', :description => 'This is your first repo', :homepage => "https://github.com", :public => true, :has_issues => true })
143
154
  #
144
- def edit_repo(user=nil, repo=nil, params={})
155
+ def edit_repo(user_name=nil, repo_name=nil, params={})
145
156
  _update_user_repo_params(user_name, repo_name)
146
157
  _validate_user_repo_params(user, repo) unless user? && repo?
147
158
 
@@ -152,6 +163,7 @@ module Github
152
163
 
153
164
  patch("/repos/#{user}/#{repo}", DEFAULT_REPO_OPTIONS.merge(params))
154
165
  end
166
+ alias :edit_repository :edit_repo
155
167
 
156
168
  # Get a repository
157
169
  #
@@ -166,6 +178,8 @@ module Github
166
178
 
167
179
  get("/repos/#{user}/#{repo}", params)
168
180
  end
181
+ alias :get_repository :get_repo
182
+
169
183
 
170
184
  # List languages
171
185
  #
@@ -183,40 +197,48 @@ module Github
183
197
  return response unless block_given?
184
198
  response.each { |el| yield el }
185
199
  end
200
+ alias :list_languages :languages
186
201
 
187
202
  # List repositories for the authenticated user
188
203
  #
189
204
  # = Examples
190
- # @github = Github.new { :consumer_key => ... }
205
+ # @github = Github.new :oauth_token => '...'
191
206
  # @github.repos.list_repos
207
+ # @github.repos.list_repos { |repo| ... }
192
208
  #
193
209
  # List public repositories for the specified user.
194
210
  #
195
211
  # = Examples
196
212
  # github = Github.new
197
- # github.repos.list_repos(:user => 'user-name')
213
+ # github.repos.list_repos :user => 'user-name'
214
+ # github.repos.list_repos :user => 'user-name', { |repo| ... }
198
215
  #
199
216
  # List repositories for the specified organisation.
200
217
  #
201
218
  # = Examples
202
219
  # @github = Github.new
203
- # @github.repos.list_repos(:org => 'org-name')
220
+ # @github.repos.list_repos :org => 'org-name'
221
+ # @github.repos.list_repos :org => 'org-name', { |repo| ... }
204
222
  #
205
- def list_repos(*args)
223
+ def repos(*args)
206
224
  params = args.last.is_a?(Hash) ? args.pop : {}
207
225
  _normalize_params_keys(params)
208
- _merge_user_into_params!(params) unless params.has_key?('user')
226
+ # _merge_user_into_params!(params) unless params.has_key?('user')
209
227
  _filter_params_keys(%w[ org user type ], params)
210
228
 
211
- if (user_name = params.delete("user"))
229
+ response = if (user_name = params.delete("user"))
212
230
  get("/users/#{user_name}/repos")
213
231
  elsif (org_name = params.delete("org"))
214
- get("/users/#{org_name}/repos", params)
232
+ get("/orgs/#{org_name}/repos", params)
215
233
  else
216
234
  # For authenticated user
217
235
  get("/user/repos", params)
218
236
  end
237
+ return response unless block_given?
238
+ response.each { |el| yield el }
219
239
  end
240
+ alias :list_repos :repos
241
+ alias :list_repositories :repos
220
242
 
221
243
  # List tags
222
244
  #
@@ -234,6 +256,9 @@ module Github
234
256
  return response unless block_given?
235
257
  response.each { |el| yield el }
236
258
  end
259
+ alias :list_tags :tags
260
+ alias :repo_tags :tags
261
+ alias :repository_tags :tags
237
262
 
238
263
  # List teams
239
264
  #
@@ -251,6 +276,9 @@ module Github
251
276
  return response unless block_given?
252
277
  response.each { |el| yield el }
253
278
  end
279
+ alias :list_teams :teams
280
+ alias :repo_teams :teams
281
+ alias :repository_teams :teams
254
282
 
255
283
  end # Repos
256
284
  end # Github