gittycent 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/CHANGELOG +2 -0
  2. data/gittycent.gemspec +2 -2
  3. data/lib/gittycent.rb +116 -59
  4. metadata +3 -6
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.1.0. Changed class name to Gittycent.
2
+
1
3
  v0.0.4. Documentation, better support for repos, minor bug fixes and basic specs.
2
4
 
3
5
  v0.0.3. Less tagline lameness.
data/gittycent.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{gittycent}
5
- s.version = "0.0.4"
5
+ s.version = "0.1.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Elijah Miller"]
9
- s.date = %q{2010-05-24}
9
+ s.date = %q{2010-07-22}
10
10
  s.description = %q{A GitHub wrapper in Ruby.}
11
11
  s.email = %q{elijah.miller@gmail.com}
12
12
  s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.rdoc", "lib/gittycent.rb"]
data/lib/gittycent.rb CHANGED
@@ -3,17 +3,17 @@ require 'active_support/all'
3
3
  require 'httparty'
4
4
  require 'pp'
5
5
 
6
- class GitHub
6
+ class Gittycent
7
7
  API_VERSION = 'v2'
8
8
  FORMAT = 'yaml'
9
9
  NONE, WARNING, INFO, DEBUG = *1..10
10
-
10
+
11
11
  attr_accessor :options
12
-
12
+
13
13
  # Connects to GitHub using login/token. Yields or returns the new
14
14
  # connection.
15
15
  #
16
- # GitHub.connect(:login => 'jqr', :token => 'somesecretstuff123')
16
+ # Gittycent.connect(:login => 'jqr', :token => 'somesecretstuff123')
17
17
  def self.connect(options)
18
18
  connection = new(options)
19
19
  if block_given?
@@ -22,34 +22,34 @@ class GitHub
22
22
  connection
23
23
  end
24
24
  end
25
-
25
+
26
26
  # Simplified method for connecting with git config specified credentials.
27
27
  # Also allows for using an alternate section for easy handling of multiple
28
28
  # credentials. Just like connect this yields or returns the new connection.
29
29
  #
30
- # github = GitHub.connect_with_git_config
30
+ # gittycent = Gittycent.connect_with_git_config
31
31
  def self.connect_with_git_config(section = :github, options = {}, &block)
32
32
  config = Hash[*`git config -l`.scan(/^(.*?)=(.*)$/).flatten]
33
33
  connect(options.merge(:login => config["#{section}.user"], :token => config["#{section}.token"]), &block)
34
34
  end
35
-
36
- # Returns a new GitHub connection object, requires :login and :token
35
+
36
+ # Returns a new Gittycent connection object, requires :login and :token
37
37
  # options.
38
38
  #
39
- # github = GitHub.new(:login => 'jqr', :token => 'somesecretstuff123')
39
+ # gittycent = Gittycent.new(:login => 'jqr', :token => 'somesecretstuff123')
40
40
  def initialize(options)
41
41
  self.options = options
42
42
  debug "Login using login #{options[:login]} with token #{options[:token]}"
43
43
  end
44
-
44
+
45
45
  # Returns the verbosity level.
46
46
  def verbosity
47
47
  options[:verbosity] || NONE
48
48
  end
49
-
49
+
50
50
  # Finds a GitHub user by login.
51
51
  #
52
- # user = github.user('jqr')
52
+ # user = gittycent.user('jqr')
53
53
  def user(login)
54
54
  if login == options[:login]
55
55
  AuthenticatedUser.new(self, :login => options[:login])
@@ -57,7 +57,7 @@ class GitHub
57
57
  User.new(self, :login => login)
58
58
  end
59
59
  end
60
-
60
+
61
61
  # Finds GitHub users by a query string.
62
62
  def user_search(query)
63
63
  get("/user/search/#{query}")['users'].map { |u| User.new(self, u) }
@@ -67,11 +67,11 @@ class GitHub
67
67
  def authenticated_user
68
68
  @authenticated_user ||= user(options[:login])
69
69
  end
70
-
70
+
71
71
  def inspect # :nodoc:
72
72
  "#<#{self.class}#{' ' + options[:login] if options[:login].present?}>"
73
73
  end
74
-
74
+
75
75
  def default_http_options
76
76
  {
77
77
  :query => {
@@ -80,7 +80,7 @@ class GitHub
80
80
  }
81
81
  }
82
82
  end
83
-
83
+
84
84
  # Performs a GET request on path, automatically prepending api version and
85
85
  # format paramters. Will automatically retry if the request rate limiting is
86
86
  # hit.
@@ -95,7 +95,7 @@ class GitHub
95
95
  response
96
96
  end
97
97
  end
98
-
98
+
99
99
  # Performs a POST request on path, automatically prepending api version and
100
100
  # format paramters. Will automatically retry if the request rate limiting is
101
101
  # hit.
@@ -111,19 +111,19 @@ class GitHub
111
111
  response
112
112
  end
113
113
  end
114
-
114
+
115
115
  def debug(message = "")
116
116
  puts message if verbosity >= DEBUG
117
117
  end
118
-
118
+
119
119
  def warn(message = "")
120
120
  puts message if verbosity >= WARNING
121
121
  end
122
-
122
+
123
123
  def debug_inspect(object)
124
124
  debug object.pretty_inspect
125
125
  end
126
-
126
+
127
127
  # Automatically retries a block of code if the returned value is rate
128
128
  # limited (HTTP Code 403) by GitHub.
129
129
  def with_retry
@@ -140,37 +140,37 @@ class GitHub
140
140
  end
141
141
  end
142
142
  end
143
-
143
+
144
144
  class Connectable
145
145
  class_inheritable_accessor :identified_by
146
- attr_accessor :connection, :name
147
-
146
+ attr_accessor :connection, :attributes
147
+
148
148
  def initialize(connection, options)
149
149
  self.connection = connection
150
- @attributes = options.dup
150
+ @attributes = options.symbolize_keys
151
151
  if self.class.identified_by && @attributes.include?(self.class.identified_by)
152
152
  send("#{identified_by}=", @attributes.delete(self.class.identified_by))
153
153
  end
154
154
  self.connection = connection
155
155
  end
156
-
156
+
157
157
  def get(*args)
158
158
  connection.get(*args)
159
159
  end
160
-
160
+
161
161
  def post(*args)
162
162
  connection.post(*args)
163
163
  end
164
-
164
+
165
165
  def debug(*args)
166
166
  connection.debug(*args)
167
167
  end
168
-
168
+
169
169
  def reload
170
170
  @attributes = {}
171
171
  load
172
172
  end
173
-
173
+
174
174
  def self.loadable_attributes(*attributes)
175
175
  (attributes - [identified_by]).each do |attribute|
176
176
  define_method(attribute) do
@@ -181,35 +181,41 @@ class GitHub
181
181
  end
182
182
  end
183
183
  end
184
-
185
- end
186
184
 
185
+ end
186
+
187
187
  class User < Connectable
188
188
  attr_accessor :login
189
189
  self.identified_by = :login
190
-
191
- loadable_attributes :created_at, :gravatar_id, :public_repo_count,
192
- :public_gist_count, :following_count, :followers_count
193
-
190
+
191
+
192
+ loadable_attributes :followers_count, :created_at, :company, :gravatar_id,
193
+ :public_repo_count, :location, :email, :public_gist_count, :blog,
194
+ :name, :following_count
195
+
194
196
  def to_s
195
197
  login.to_s
196
198
  end
197
-
199
+
198
200
  # Returns this user's repositories.
199
201
  def repos
200
202
  @repos ||= get("/repos/show/#{login}")['repositories'].map { |r| Repo.new(connection, r) }
201
203
  end
202
-
204
+
205
+ def repo(name)
206
+ repos.detect { |r| r.name == name }
207
+ end
208
+
203
209
  # Returns a list of all public repos thi user is watching.
204
210
  def watched_repos
205
211
  @repos ||= get("/repos/watched/#{login}")['repositories'].map { |r| Repo.new(connection, r) }
206
212
  end
207
-
213
+
208
214
  # Loads lazily-fetched attributes.
209
215
  def load
210
216
  @attributes = get("/user/show/#{login}")['user'].symbolize_keys
211
217
  end
212
-
218
+
213
219
  # Returns all the users that follow this user.
214
220
  def followers
215
221
  @followers ||= get("/user/show/#{login}/followers")['users'].map { |u| User.new(connection, :login => u) }
@@ -220,19 +226,16 @@ class GitHub
220
226
  @following||= get("/user/show/#{login}/following")['users'].map { |u| User.new(connection, :login => u) }
221
227
  end
222
228
  end
223
-
229
+
224
230
  class AuthenticatedUser < User
225
- loadable_attributes :followers_count, :owned_private_repo_count,
226
- :created_at, :company, :private_gist_count, :plan, :gravatar_id,
227
- :total_private_repo_count, :public_repo_count, :location, :email,
228
- :collaborators, :public_gist_count, :blog, :name, :following_count,
229
- :disk_usage
230
-
231
+ loadable_attributes :owned_private_repo_count, :created_at,
232
+ :private_gist_count, :plan, :collaborators, :disk_usage
233
+
231
234
  # Returns a list of all repos this user is watching.
232
235
  def watched_repos
233
236
  super
234
237
  end
235
-
238
+
236
239
  # Adds a new repository for this user. Supported options include: :name,
237
240
  # :description, :homepage, :public.
238
241
  #
@@ -242,7 +245,7 @@ class GitHub
242
245
  Repo.new(connection, post("/repos/create", options)['repository'])
243
246
  end
244
247
  end
245
-
248
+
246
249
  class Repo < Connectable
247
250
  attr_accessor :name
248
251
  self.identified_by = :name
@@ -253,17 +256,17 @@ class GitHub
253
256
  def to_s
254
257
  name.to_s
255
258
  end
256
-
259
+
257
260
  # Returns the owner of this repository.
258
261
  def owner
259
262
  @owner ||= User.new(connection, :login => @attributes[:owner])
260
263
  end
261
-
264
+
262
265
  # Returns a list of all collaborators.
263
266
  def collaborators
264
267
  @collaborators ||= get("/repos/show/#{owner.login}/#{name}/collaborators")['collaborators'] || []
265
268
  end
266
-
269
+
267
270
  # Sets the list of collaborators.
268
271
  def collaborators=(value)
269
272
  value = value.dup.uniq
@@ -279,7 +282,7 @@ class GitHub
279
282
  end
280
283
  @collaborators = nil
281
284
  end
282
-
285
+
283
286
  # Returns the shell commands used to do an initial push of this project to
284
287
  # GitHub.
285
288
  def initial_push_command
@@ -288,30 +291,84 @@ class GitHub
288
291
  "git config --add branch.master.remote origin &&\n" +
289
292
  "git config --add branch.master.merge refs/heads/master"
290
293
  end
291
-
294
+
292
295
  # Returns repositories in this repository's network.
293
296
  def network
294
297
  @network ||= get("/repos/show/#{owner.login}/#{name}/network")['network'].map { |r| Repo.new(connection, r) }
295
298
  end
296
-
299
+
297
300
  # Returns the languages detected.
298
301
  def languages
299
302
  get("/repos/show/#{owner.login}/#{name}/languages")['languages']
300
303
  end
301
-
304
+
302
305
  # Loads lazily-fetched attributes.
303
306
  def load
304
307
  @attributes = get("/repos/show/#{owner.login}/#{name}")['repository'].symbolize_keys
305
308
  end
306
-
309
+
307
310
  # Returns all tags of this repository.
308
311
  def tags
309
312
  get("/repos/show/#{owner.login}/#{name}/tags")['tags']
310
313
  end
311
-
314
+
312
315
  # Returns all branches of this repository.
313
316
  def branches
314
317
  get("/repos/show/#{owner.login}/#{name}/branches")['branches']
315
318
  end
319
+
320
+ # Returns all commits of the master branch.
321
+ def commits
322
+ get("/commits/list/#{owner.login}/#{name}/master")['commits'].map { |c| Commit.new(connection, c.merge(:repo => self)) }
323
+ end
324
+ end
325
+
326
+
327
+ class Commit < Connectable
328
+ attr_accessor :id
329
+ self.identified_by = :id
330
+
331
+ loadable_attributes :author, :parents, :url, :committed_date, :authored_date, :message, :committer, :tree
332
+
333
+ def to_s
334
+ id.to_s
335
+ end
336
+
337
+ # Returns the associated repository.
338
+ def repo
339
+ @attributes[:repo]
340
+ end
341
+
342
+ # Returns the parent commits of this commit.
343
+ def parents
344
+ load unless @attributes.include?(:parents)
345
+ @parents ||= @attributes[:parents].map { |p| Commit.new(connection, p.merge(:repo => repo)) }
346
+ end
347
+
348
+ # Returns the Person who authored this commit.
349
+ def author
350
+ @author ||= Person.new(connection, @attributes[:author])
351
+ end
352
+
353
+ # Returns the Person who committed this commit.
354
+ def committer
355
+ @committer ||= Person.new(connection, @attributes[:committer])
356
+ end
357
+
358
+ # Loads lazily-fetched attributes.
359
+ def load
360
+ @attributes = get("/commits/show/#{repo.owner.login}/#{repo.name}/#{id}")['commit'].symbolize_keys.merge(:repo => repo)
361
+ end
362
+
363
+ end
364
+
365
+ class Person < Connectable
366
+ loadable_attributes :name, :email, :login
367
+
368
+ def user
369
+ if login
370
+ connection.user(login)
371
+ end
372
+ end
316
373
  end
317
- end
374
+ end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gittycent
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
7
+ - 1
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ version: 0.1.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Elijah Miller
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-05-24 00:00:00 -04:00
17
+ date: 2010-07-22 00:00:00 -04:00
19
18
  default_executable:
20
19
  dependencies: []
21
20
 
@@ -57,7 +56,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
56
  requirements:
58
57
  - - ">="
59
58
  - !ruby/object:Gem::Version
60
- hash: 3
61
59
  segments:
62
60
  - 0
63
61
  version: "0"
@@ -66,7 +64,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
64
  requirements:
67
65
  - - ">="
68
66
  - !ruby/object:Gem::Version
69
- hash: 11
70
67
  segments:
71
68
  - 1
72
69
  - 2