git_hub 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/README.rdoc +16 -2
  2. data/VERSION +1 -1
  3. data/bin/git_hub +1 -2
  4. data/git_hub.gemspec +30 -3
  5. data/lib/git_hub/api.rb +6 -0
  6. data/lib/git_hub/base.rb +52 -10
  7. data/lib/git_hub/commit.rb +45 -0
  8. data/lib/git_hub/repo.rb +47 -51
  9. data/rdoc/classes/GitHub/Api.html +300 -0
  10. data/rdoc/classes/GitHub/Base.html +423 -0
  11. data/rdoc/classes/GitHub/Repo.html +738 -0
  12. data/rdoc/classes/GitHub.html +310 -0
  13. data/rdoc/created.rid +1 -0
  14. data/rdoc/files/README_rdoc.html +171 -0
  15. data/rdoc/files/lib/git_hub/api_rb.html +103 -0
  16. data/rdoc/files/lib/git_hub/base_rb.html +101 -0
  17. data/rdoc/files/lib/git_hub/repo_rb.html +91 -0
  18. data/rdoc/files/lib/git_hub_rb.html +91 -0
  19. data/rdoc/fr_class_index.html +33 -0
  20. data/rdoc/fr_file_index.html +35 -0
  21. data/rdoc/fr_method_index.html +81 -0
  22. data/rdoc/index.html +23 -0
  23. data/rdoc/rdoc-style.css +299 -0
  24. data/spec/git_hub/base_spec.rb +2 -2
  25. data/spec/git_hub/commit_spec.rb +53 -0
  26. data/spec/git_hub/repo_spec.rb +123 -85
  27. data/spec/spec_helper.rb +35 -8
  28. data/spec/stubs/api_route_error.res +14 -0
  29. data/spec/stubs/commits/list/joe007/fine_repo/master/README.res +28 -0
  30. data/spec/stubs/commits/list/joe007/fine_repo/master.res +84 -0
  31. data/spec/stubs/commits/show/joe007/fine_repo/5e61f0687c40ca48214d09dc7ae2d0d0d8fbfeb8.res +38 -0
  32. data/spec/stubs/repos/create.1.res +22 -0
  33. data/spec/stubs/repos/{create.res → create.2.res} +0 -0
  34. data/spec/stubs/repos/create.3.res +100 -0
  35. data/spec/stubs/repos/create.4.res +14 -0
  36. data/spec/stubs/repos/show/joe007/fine_repo/branches.res +15 -0
  37. data/spec/stubs/repos/show/joe007/fine_repo/tags.res +17 -0
  38. metadata +30 -3
data/README.rdoc CHANGED
@@ -18,14 +18,28 @@ Contributors always welcome!
18
18
  require 'git_hub'
19
19
  include GitHub
20
20
 
21
- # Find existing repos
21
+ # Find existing repos:
22
22
  repo = Repo.find(:user=>user_name, :repo=>repo_name)
23
23
  repos = Repo.find(:user=>user_name)
24
24
  search_repos = Repo.find(:query=>['search','terms'])
25
25
 
26
- # Create new repo (need to authenticate with your github credentials first)
26
+ # Retrieve tags, branches and commits for a repo:
27
+ puts repo.tags['v0.1.0']
28
+ puts repo.branches['master']
29
+ repo.commits.each {|commit| p commit.author}
30
+
31
+ # Retrieve info for a specific commit:
32
+ commits = Commit.find(:user=>user_name, :repo=>repo_name, :branch=>'master')
33
+ commits = Commit.find(:user=>user_name, :repo=>repo_name, :branch=>'master', :path=>'README.rdoc')
34
+ commit = Commit.find(:user=>user_name, :repo=>repo_name, :sha=>'commit_sha')
35
+ # or, if repo is already retrieved:
36
+ commit = repo.commits.last
37
+ commit = repo.tags['v0.1.0'] #TODO - now returns only sha, not Commit
38
+
39
+ # Create new repo or delete existing repo (need to authenticate with your github credentials first)
27
40
  Api.auth = {:user=>user_name, :token=>token}
28
41
  new_repo = Repo.create(:name=>repo_name, :desc=>'Description', :homepage=>'http://your_page.org', :private=> false)
42
+ new_repo.delete
29
43
 
30
44
  == REQUIREMENTS:
31
45
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/bin/git_hub CHANGED
@@ -1,7 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(
4
- File.join(File.dirname(__FILE__), %w[.. lib git_hub]))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib git_hub]))
5
4
 
6
5
  # Put your code here
7
6
 
data/git_hub.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{git_hub}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["arvicco"]
12
- s.date = %q{2010-01-07}
12
+ s.date = %q{2010-01-11}
13
13
  s.default_executable = %q{git_hub}
14
14
  s.description = %q{Simple interface to github API}
15
15
  s.email = %q{arvitallian@gmail.com}
@@ -31,19 +31,45 @@ Gem::Specification.new do |s|
31
31
  "lib/git_hub.rb",
32
32
  "lib/git_hub/api.rb",
33
33
  "lib/git_hub/base.rb",
34
+ "lib/git_hub/commit.rb",
34
35
  "lib/git_hub/repo.rb",
36
+ "rdoc/classes/GitHub.html",
37
+ "rdoc/classes/GitHub/Api.html",
38
+ "rdoc/classes/GitHub/Base.html",
39
+ "rdoc/classes/GitHub/Repo.html",
40
+ "rdoc/created.rid",
41
+ "rdoc/files/README_rdoc.html",
42
+ "rdoc/files/lib/git_hub/api_rb.html",
43
+ "rdoc/files/lib/git_hub/base_rb.html",
44
+ "rdoc/files/lib/git_hub/repo_rb.html",
45
+ "rdoc/files/lib/git_hub_rb.html",
46
+ "rdoc/fr_class_index.html",
47
+ "rdoc/fr_file_index.html",
48
+ "rdoc/fr_method_index.html",
49
+ "rdoc/index.html",
50
+ "rdoc/rdoc-style.css",
35
51
  "spec/git_hub/api_spec.rb",
36
52
  "spec/git_hub/base_spec.rb",
53
+ "spec/git_hub/commit_spec.rb",
37
54
  "spec/git_hub/repo_spec.rb",
38
55
  "spec/spec.opts",
39
56
  "spec/spec_helper.rb",
40
- "spec/stubs/repos/create.res",
57
+ "spec/stubs/api_route_error.res",
58
+ "spec/stubs/commits/list/joe007/fine_repo/master.res",
59
+ "spec/stubs/commits/list/joe007/fine_repo/master/README.res",
60
+ "spec/stubs/commits/show/joe007/fine_repo/5e61f0687c40ca48214d09dc7ae2d0d0d8fbfeb8.res",
61
+ "spec/stubs/repos/create.1.res",
62
+ "spec/stubs/repos/create.2.res",
63
+ "spec/stubs/repos/create.3.res",
64
+ "spec/stubs/repos/create.4.res",
41
65
  "spec/stubs/repos/delete/new_repo.1.res",
42
66
  "spec/stubs/repos/delete/new_repo.2.res",
43
67
  "spec/stubs/repos/search/joe+repo.res",
44
68
  "spec/stubs/repos/show/joe007.res",
45
69
  "spec/stubs/repos/show/joe007/err_repo.res",
46
70
  "spec/stubs/repos/show/joe007/fine_repo.res",
71
+ "spec/stubs/repos/show/joe007/fine_repo/branches.res",
72
+ "spec/stubs/repos/show/joe007/fine_repo/tags.res",
47
73
  "spec/stubs/repos/show/joe007/new_repo.res"
48
74
  ]
49
75
  s.homepage = %q{http://github.com/arvicco/git_hub}
@@ -54,6 +80,7 @@ Gem::Specification.new do |s|
54
80
  s.test_files = [
55
81
  "spec/git_hub/api_spec.rb",
56
82
  "spec/git_hub/base_spec.rb",
83
+ "spec/git_hub/commit_spec.rb",
57
84
  "spec/git_hub/repo_spec.rb",
58
85
  "spec/spec_helper.rb"
59
86
  ]
data/lib/git_hub/api.rb CHANGED
@@ -15,6 +15,12 @@ module GitHub
15
15
  auth != {}
16
16
  end
17
17
 
18
+ def ensure_auth opts ={}
19
+ return if authenticated?
20
+ @auth = {'login'=>opts[:login], 'token'=>opts[:token]}
21
+ raise("Authentication failed") unless authenticated?
22
+ end
23
+
18
24
  # Turns string into appropriate class constant, returns nil if class not found
19
25
  def classify name
20
26
  klass = name.split("::").inject(Kernel) {|klass, const_name| klass.const_get const_name }
data/lib/git_hub/base.rb CHANGED
@@ -2,24 +2,25 @@ require 'yaml'
2
2
 
3
3
  module GitHub
4
4
  class Base
5
- @base_uri = ''
6
5
 
7
6
  def initialize(attributes={})
7
+ update_attributes attributes
8
+ end
9
+
10
+ def update_attributes attributes
8
11
  attributes.each do |key, value|
9
12
  raise "No attr_accessor for #{key} on #{self.class}" unless respond_to?("#{key}=")
10
- self.send("#{key}=", value)
13
+ self.send("#{key.to_s}=", value)
11
14
  end
12
15
  end
13
16
 
14
- def self.base_uri uri
15
- @base_uri = uri
16
- end
17
-
18
17
  class << self
19
18
  def request verb, uri, params = {}
20
- res = api.request verb, @base_uri+uri, params
21
- YAML::load(res.body) if res.respond_to?(:body) # res.kind_of?(Net::HTTPSuccess)
22
- #p "in show: #{res}: #{res.code}: #{res.http_version}: #{res.message}", res.body
19
+ path = uri[0] == '/' ? base_uri+uri : uri
20
+ #p "request: #{verb} #{path} #{params}"
21
+ res = api.request verb, path, params
22
+ YAML::load(res.body) if res.respond_to?(:body)
23
+ #p "response: #{res}: #{res.code}: #{res.http_version}: #{res.message}", res.body
23
24
  end
24
25
 
25
26
  def get uri, params ={}
@@ -31,7 +32,43 @@ module GitHub
31
32
  end
32
33
 
33
34
  def api
34
- @@api ||= GitHub::Api.instance
35
+ @@api ||= Api.instance
36
+ end
37
+
38
+ def set_resource base_uri, singulars, plurals
39
+ @base_uri = base_uri
40
+ @singulars = [singulars].flatten
41
+ @plurals = [plurals].flatten
42
+ end
43
+
44
+ def base_uri
45
+ @base_uri || ""
46
+ end
47
+
48
+ private
49
+
50
+ def normalize opts
51
+ opts[:user] ||= opts[:owner] || opts[:username] || opts[:login] || api.auth['login']
52
+ opts[:repo] ||= opts[:repository] || opts[:name] || opts[:project]
53
+ opts[:sha] ||= opts[:hash] || opts[:object_id] || opts[:id]
54
+ opts[:description] ||= opts[:descr] || opts[:desc]
55
+ opts[:query] ||= opts[:search]
56
+ opts[:branch] ||= 'master'
57
+ opts[:public] ||= !opts[:private] unless opts[:public] = false # defaults to true
58
+ end
59
+
60
+ def instantiate hash
61
+ if res = contains(hash, @singulars)
62
+ new res
63
+ elsif res = contains(hash, @plurals)
64
+ res.map {|r| new r}
65
+ else
66
+ hash
67
+ end
68
+ end
69
+
70
+ def contains hash, keys
71
+ keys.inject(nil) {|memo, key| memo ||= hash[key.to_s]}
35
72
  end
36
73
  end
37
74
 
@@ -46,5 +83,10 @@ module GitHub
46
83
  def api
47
84
  self.class.api
48
85
  end
86
+
87
+ def to_s
88
+ name
89
+ end
90
+
49
91
  end
50
92
  end
@@ -0,0 +1,45 @@
1
+ module GitHub
2
+ class Commit < Base
3
+
4
+ set_resource 'http://github.com/api/v2/yaml/commits', 'commit', ['commits', 'tags', 'branches']
5
+
6
+ attr_accessor :id, :author, :committer, :parents, :url, :committed_date, :authored_date, :message, :tree,
7
+ # :user, :name, :sha, :repo,
8
+ # retrieving commit for a specific sha - "/show/#{opts[:user]}/#{opts[:repo]}/#{opts[:sha]}"
9
+ :added, :modified, :removed
10
+
11
+ def initialize opts
12
+ super
13
+ raise "Unable to initialize #{self.class} without id(sha)" unless sha
14
+ end
15
+
16
+ alias name id
17
+ alias name= id=
18
+ alias sha id
19
+ alias sha= id=
20
+
21
+ class << self
22
+ # Find commits, accepts Hash with keys:
23
+ # :user/:owner/:username:: Github user name
24
+ # :repo/:repository/:project:: Repo name
25
+ # :branch:: Repo branch - default 'master'
26
+ # :path:: For specific path
27
+ # :sha/:hash/:id:: Unique commit id (sha)
28
+ def find(opts)
29
+ normalize opts
30
+ raise "Unable to find Commits for #{opts}" unless opts[:user] && opts[:repo]
31
+ path = if opts[:sha]
32
+ "/show/#{opts[:user]}/#{opts[:repo]}/#{opts[:sha]}"
33
+ elsif opts[:path]
34
+ "/list/#{opts[:user]}/#{opts[:repo]}/#{opts[:branch]}/#{opts[:path]}"
35
+ else
36
+ "/list/#{opts[:user]}/#{opts[:repo]}/#{opts[:branch]}"
37
+ end
38
+ instantiate get(path)
39
+ end
40
+
41
+ #alias show find
42
+ end
43
+ end
44
+
45
+ end
data/lib/git_hub/repo.rb CHANGED
@@ -24,7 +24,7 @@ module GitHub
24
24
  # twitter = username, password, digest
25
25
  }
26
26
 
27
- base_uri 'http://github.com/api/v2/yaml/repos'
27
+ set_resource 'http://github.com/api/v2/yaml/repos', 'repository', 'repositories'
28
28
 
29
29
  attr_accessor :name, :owner, :description, :url, :homepage, :open_issues, :watchers, :forks, :fork, :private,
30
30
  # additional attributes from search:
@@ -32,8 +32,8 @@ module GitHub
32
32
 
33
33
  def initialize options
34
34
  super
35
- raise "Unable to initialize #{self.class} without name" unless @name
36
- @url ||= "http://github.com/#{@owner}/#{@name}"
35
+ raise "Unable to initialize #{self.class} without name" unless user && name
36
+ @url ||= "http://github.com/#{user}/#{name}"
37
37
  @type ||= "repo"
38
38
  end
39
39
 
@@ -41,6 +41,8 @@ module GitHub
41
41
  alias followers watchers
42
42
  alias username= owner=
43
43
  alias username owner
44
+ alias user= owner=
45
+ alias user owner
44
46
 
45
47
  def fork?;
46
48
  !!self.fork
@@ -51,71 +53,65 @@ module GitHub
51
53
  end
52
54
 
53
55
  def clone_url
54
- url = private? || api.auth['login'] == self.owner ? "git@github.com:" : "git://github.com/"
55
- url += "#{self.owner}/#{self.name}.git"
56
+ url = private? || api.auth['login'] == self.user ? "git@github.com:" : "git://github.com/"
57
+ url += "#{self.user}/#{self.name}.git"
58
+ end
59
+
60
+ def tags
61
+ result = get "/show/#{self.user}/#{self.name}/tags"
62
+ result['tags'] || result
63
+ end
64
+
65
+ def branches
66
+ result = get "/show/#{self.user}/#{self.name}/branches"
67
+ result['branches'] || result
68
+ end
69
+
70
+ def commits opts = {}
71
+ Commit.find opts.merge(:user => self.user, :repo => self.name)
56
72
  end
57
73
 
58
74
  class << self # Repo class methods
59
- # Find repo(s) of a (valid) github user
60
- # Accepts either options Hash (:owner/:user/:username and :repo/:repository/:project/:name)
61
- # or several search terms as strings or symbols
62
- def find(*params)
63
- if params.size == 1 && params.first.is_a?(Hash) # this is /show request
64
- opts = params.first
65
- owner = opts[:owner] || opts[:user] || opts[:username] || opts[:login] || api.auth['login'] || ''
66
- repo = opts[:repo] || opts[:repository] || opts[:name] || opts[:project]
67
- path = repo ? "/show/#{owner}/#{repo}" : "/show/#{owner}"
68
- else # this is /search request, params are search terms
69
- query = params.map(&:to_s).join('+')
70
- path = "/search/#{query}"
71
- end
72
- result = get(path)
73
- if result['repository']
74
- new result['repository']
75
- elsif result['repositories']
76
- result['repositories'].map {|r| new r}
75
+
76
+ # Find repo(s) of a (valid) github user, accepts Hash with keys:
77
+ # :owner/:user/:username:: Github user name
78
+ # :repo/:repository/:project/:name:: Repo name
79
+ # :query/:search:: Array of search terms as Strings or Symbols
80
+ def find(opts)
81
+ normalize opts
82
+ path = if opts[:query]
83
+ "/search/#{opts[:query].map(&:to_s).join('+')}"
84
+ elsif opts[:user] && opts[:repo]
85
+ "/show/#{opts[:user]}/#{opts[:repo]}"
86
+ elsif opts[:user]
87
+ "/show/#{opts[:user]}"
77
88
  else
78
- result
89
+ raise "Unable to find #{self.class}(s) for #{opts}"
79
90
  end
91
+ instantiate get(path)
80
92
  end
81
93
 
82
94
  alias show find
83
95
  alias search find
84
96
 
85
97
  # Create new github repo, accepts Hash with :repo, :description, :homepage, :public/:private
86
- # or repo name as a single parameter
87
- def create(*params)
88
- if params.size == 1 && params.first.is_a?(Hash)
89
- opts = params.first
90
- repo = opts[:repo] || opts[:repository] || opts[:name] || opts[:project]
91
- description = opts[:description] || opts[:descr] || opts[:desc]
92
- homepage = opts[:homepage]
93
- public = opts[:public] || !opts[:private] # default to true
94
- else # repo name as a single parameter
95
- repo = params.first.to_s
96
- description = nil
97
- homepage = nil
98
- public = false
99
- end
100
- raise("Unable to create #{self.class} without authorization") unless api.authenticated?
101
- result = post("/create", 'name' => repo, 'description' => description,
102
- 'homepage' => homepage, 'public' => (public ? 1 : 0))
103
- if result['repository']
104
- new result['repository']
105
- else
106
- result
107
- end
98
+ def create(opts)
99
+ normalize opts
100
+ api.ensure_auth opts
101
+ instantiate post("/create", 'name' => opts[:repo], 'description' => opts[:description],
102
+ 'homepage' => opts[:homepage], 'public' => (opts[:public] ? 1 : 0))
108
103
  end
109
104
  end
110
105
 
111
- # Delete github repo
112
- def delete
113
- result = post("/delete/#{@name}")
106
+ # Delete github repo, accepts optional Hash with authentication
107
+ def delete(opts = {})
108
+ api.ensure_auth opts
109
+ result = post("/delete/#{name}")
114
110
  if result['delete_token']
115
- post("/delete/#{@name}", 'delete_token' => result['delete_token'])
111
+ post("/delete/#{name}", 'delete_token' => result['delete_token'])
116
112
  else
117
113
  result
118
- end
114
+ end
119
115
  end
120
116
 
121
117
  def add_service