github-api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.DS_Store +0 -0
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +50 -0
- data/Rakefile +1 -0
- data/github-api.gemspec +23 -0
- data/lib/github-api.rb +7 -0
- data/lib/github-api/other.rb +99 -0
- data/lib/github-api/repo.rb +124 -0
- data/lib/github-api/user.rb +47 -0
- data/lib/github-api/version.rb +3 -0
- data/spec/blob_spec.rb +12 -0
- data/spec/repo_spec.rb +5 -0
- data/spec/user_spec.rb +12 -0
- metadata +123 -0
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
Installation
|
2
|
+
------------
|
3
|
+
|
4
|
+
NB: This is a super early version of this gem. Use only for testing purposes
|
5
|
+
|
6
|
+
To install the gem, simply run:
|
7
|
+
|
8
|
+
gem install 'github-api'
|
9
|
+
|
10
|
+
Or the bundler equivalent:
|
11
|
+
|
12
|
+
bundle install 'github-api'
|
13
|
+
|
14
|
+
Examples
|
15
|
+
--------
|
16
|
+
|
17
|
+
Use the gem to create / retrieve Github repo data. You can use my 'github-oauth' gem to get the token.
|
18
|
+
|
19
|
+
Use oauth token to create a user object
|
20
|
+
|
21
|
+
@user = GithubApi::User.new(ab3cd9j4ks73hf7)
|
22
|
+
|
23
|
+
Then create stuff
|
24
|
+
|
25
|
+
@user.has_repo?("my_repo_name")
|
26
|
+
|
27
|
+
repo = @user.create_repo("githunch_bookmarks", {
|
28
|
+
:description => "Repository for Githunch Bookmarks",
|
29
|
+
:homepage => "http://githunch.heroku.com",
|
30
|
+
:public => true,
|
31
|
+
:has_issues => false,
|
32
|
+
:has_wiki => false,
|
33
|
+
:has_downloads => false
|
34
|
+
})
|
35
|
+
|
36
|
+
file = GithubApi::Blob.new(:content => "this is my content", :path => "bookmarks.json")
|
37
|
+
tree = repo.create_tree([file])
|
38
|
+
|
39
|
+
commit = repo.create_initial_commit(tree.data["sha"], "This is my commit text")
|
40
|
+
|
41
|
+
reference = repo.create_ref("refs/heads/master", commit.data["sha"])
|
42
|
+
|
43
|
+
Or retrieve stuff
|
44
|
+
|
45
|
+
blob = repo.ref("heads/master").commit.tree.file("bookmarks.json")
|
46
|
+
puts blob.content
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/github-api.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "github-api"
|
6
|
+
s.version = "0.0.1"
|
7
|
+
s.authors = ["Rune Madsen"]
|
8
|
+
s.email = ["rune@runemadsen.com"]
|
9
|
+
s.homepage = ""
|
10
|
+
s.summary = %q{A simple gem to simplify requests to the Github API}
|
11
|
+
s.description = %q{A simple gem to simplify requests to the Github API}
|
12
|
+
|
13
|
+
s.rubyforge_project = "github-api"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency "json"
|
21
|
+
s.add_development_dependency "rspec", "~> 2.6"
|
22
|
+
s.add_dependency "httparty"
|
23
|
+
end
|
data/lib/github-api.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
module GithubApi
|
2
|
+
|
3
|
+
# TODO: Generally there's a problem with accessing data via data["sha"]. Find a way to streamline this to: commit.sha
|
4
|
+
# TODO: Repo JSON is different when creating, getting all or getting one. Find a way to merge this
|
5
|
+
|
6
|
+
# HTTP class
|
7
|
+
# -------------------------------------
|
8
|
+
|
9
|
+
class HTTP
|
10
|
+
include HTTParty
|
11
|
+
format :json
|
12
|
+
base_uri 'https://api.github.com'
|
13
|
+
end
|
14
|
+
|
15
|
+
# Reference class
|
16
|
+
# -------------------------------------
|
17
|
+
|
18
|
+
class Reference
|
19
|
+
|
20
|
+
attr_accessor :repo, :data
|
21
|
+
|
22
|
+
def initialize(repo, data)
|
23
|
+
@repo = repo
|
24
|
+
@data = data
|
25
|
+
end
|
26
|
+
|
27
|
+
def commit
|
28
|
+
# TODO: Object in ref is not always a commit.
|
29
|
+
@repo.commit(data["object"]["sha"])
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
# Commit class
|
35
|
+
# -------------------------------------
|
36
|
+
|
37
|
+
class Commit
|
38
|
+
|
39
|
+
attr_accessor :repo, :data
|
40
|
+
|
41
|
+
def initialize(repo, data)
|
42
|
+
@repo = repo
|
43
|
+
@data = data
|
44
|
+
end
|
45
|
+
|
46
|
+
def tree
|
47
|
+
@repo.tree(data["tree"]["sha"])
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
# Tree class
|
53
|
+
# -------------------------------------
|
54
|
+
|
55
|
+
class Tree
|
56
|
+
|
57
|
+
attr_accessor :repo, :data
|
58
|
+
|
59
|
+
def initialize(repo, data)
|
60
|
+
@repo = repo
|
61
|
+
@data = data
|
62
|
+
end
|
63
|
+
|
64
|
+
def file(path)
|
65
|
+
blob_ref = data["tree"].find { |b| b["path"] == path}
|
66
|
+
unless blob_ref.nil?
|
67
|
+
sha = blob_ref["sha"]
|
68
|
+
@repo.blob(sha)
|
69
|
+
else
|
70
|
+
# throw error because the blob is not there
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
# Blob class
|
77
|
+
# -------------------------------------
|
78
|
+
|
79
|
+
class Blob
|
80
|
+
|
81
|
+
attr_accessor :content, :encoding, :path
|
82
|
+
|
83
|
+
def initialize(attributes={})
|
84
|
+
@encoding = "utf-8"
|
85
|
+
attributes.each { |key, val| send("#{key}=", val) if respond_to?("#{key}=") }
|
86
|
+
end
|
87
|
+
|
88
|
+
def to_hash
|
89
|
+
{
|
90
|
+
:type => "blob",
|
91
|
+
:path => @path,
|
92
|
+
:content => @content,
|
93
|
+
:encoding => @encoding
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
module GithubApi
|
2
|
+
class Repo
|
3
|
+
|
4
|
+
attr_accessor :data, :name
|
5
|
+
|
6
|
+
def initialize(user, data)
|
7
|
+
@user = user
|
8
|
+
@data = data
|
9
|
+
@name = data["name"]
|
10
|
+
@trees = []
|
11
|
+
@commits = []
|
12
|
+
@refs = []
|
13
|
+
@blobs = []
|
14
|
+
end
|
15
|
+
|
16
|
+
# Trees
|
17
|
+
# ------------------------------------------------------------------------------
|
18
|
+
|
19
|
+
def tree(sha)
|
20
|
+
puts "SHA: #{sha}"
|
21
|
+
# TODO: it may be smart to just load all trees at one time
|
22
|
+
tre = @trees.find { |t| t.data["sha"] == sha }
|
23
|
+
if tre.nil?
|
24
|
+
response = GithubApi::HTTP.get("/repos/#{@user.data['login']}/#{@name}/git/trees/" + sha).parsed_response
|
25
|
+
tre = GithubApi::Tree.new(self, response)
|
26
|
+
@trees << tre
|
27
|
+
end
|
28
|
+
tre
|
29
|
+
end
|
30
|
+
|
31
|
+
def create_tree(blobs)
|
32
|
+
# TODO: Check if blobs have path names in them. It is required
|
33
|
+
# Throw error if not
|
34
|
+
response = GithubApi::HTTP.post("/repos/#{@user.data['login']}/#{@name}/git/trees",
|
35
|
+
:query => {
|
36
|
+
:access_token => @user.token
|
37
|
+
},
|
38
|
+
:body => {
|
39
|
+
:tree => blobs.map { |blob| blob.to_hash }
|
40
|
+
}.to_json
|
41
|
+
).parsed_response
|
42
|
+
|
43
|
+
@trees << Tree.new(self, response)
|
44
|
+
@trees.last
|
45
|
+
end
|
46
|
+
|
47
|
+
# Commits
|
48
|
+
# ------------------------------------------------------------------------------
|
49
|
+
|
50
|
+
def commit(sha)
|
51
|
+
# TODO: it may be smart to just load all commits at one time
|
52
|
+
com = @commits.find { |c| c.data["sha"] == sha }
|
53
|
+
if com.nil?
|
54
|
+
response = GithubApi::HTTP.get("/repos/#{@user.data['login']}/#{@name}/git/commits/" + sha).parsed_response
|
55
|
+
com = GithubApi::Commit.new(self, response)
|
56
|
+
@commits << com
|
57
|
+
end
|
58
|
+
com
|
59
|
+
end
|
60
|
+
|
61
|
+
def create_initial_commit(sha, message)
|
62
|
+
response = GithubApi::HTTP.post("/repos/#{@user.data['login']}/#{@name}/git/commits",
|
63
|
+
:query => {
|
64
|
+
:access_token => @user.token
|
65
|
+
},
|
66
|
+
:body => {
|
67
|
+
:message => message,
|
68
|
+
:tree => sha
|
69
|
+
}.to_json
|
70
|
+
).parsed_response
|
71
|
+
|
72
|
+
@commits << GithubApi::Commit.new(self, response)
|
73
|
+
@commits.last
|
74
|
+
end
|
75
|
+
|
76
|
+
# References
|
77
|
+
# ------------------------------------------------------------------------------
|
78
|
+
|
79
|
+
def ref(name)
|
80
|
+
# TODO: it may be smart to just load all refs at one time
|
81
|
+
reference = @refs.find { |r| r.data["ref"] == name }
|
82
|
+
if reference.nil?
|
83
|
+
response = GithubApi::HTTP.get("/repos/#{@user.data['login']}/#{@name}/git/refs/" + name).parsed_response
|
84
|
+
reference = GithubApi::Reference.new(self, response)
|
85
|
+
@refs << reference
|
86
|
+
end
|
87
|
+
reference
|
88
|
+
end
|
89
|
+
|
90
|
+
def create_ref(name, sha)
|
91
|
+
response = GithubApi::HTTP.post("/repos/#{@user.data['login']}/#{@name}/git/refs",
|
92
|
+
:query => {
|
93
|
+
:access_token => @user.token
|
94
|
+
},
|
95
|
+
:body => {
|
96
|
+
:ref => name,
|
97
|
+
:sha => sha
|
98
|
+
}.to_json
|
99
|
+
).parsed_response
|
100
|
+
|
101
|
+
@refs << GithubApi::Reference.new(self, response)
|
102
|
+
@refs.last
|
103
|
+
end
|
104
|
+
|
105
|
+
# Blobs
|
106
|
+
# ------------------------------------------------------------------------------
|
107
|
+
|
108
|
+
def blob(sha)
|
109
|
+
# TODO: it may be smart to just load all blobs at one time
|
110
|
+
blo = @blobs.find { |b| b.data["sha"] == sha }
|
111
|
+
if blo.nil?
|
112
|
+
response = GithubApi::HTTP.get("/repos/#{@user.data['login']}/#{@name}/git/blobs/" + sha,
|
113
|
+
:headers => {
|
114
|
+
'Content-Type' => 'application/vnd.github.beta.raw'
|
115
|
+
}
|
116
|
+
).parsed_response
|
117
|
+
blo = GithubApi::Blob.new(response)
|
118
|
+
@blobs << blo
|
119
|
+
end
|
120
|
+
blo
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module GithubApi
|
2
|
+
class User
|
3
|
+
|
4
|
+
attr_accessor :data, :token
|
5
|
+
|
6
|
+
def initialize(token)
|
7
|
+
@token = token
|
8
|
+
@data = GithubApi::HTTP.get("/user", :query => {:access_token => @token})
|
9
|
+
@repos = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def has_repo?(name)
|
13
|
+
load_repos if @repos.empty?
|
14
|
+
@repos.find { |repo| repo.name == name }
|
15
|
+
end
|
16
|
+
|
17
|
+
def repos
|
18
|
+
load_repos if @repos.empty?
|
19
|
+
@repos
|
20
|
+
end
|
21
|
+
|
22
|
+
def load_repos
|
23
|
+
response = GithubApi::HTTP.get("/user/repos", :query => {:access_token => @token}).parsed_response
|
24
|
+
response.each do |repo|
|
25
|
+
@repos << Repo.new(self, repo)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_repo(name, params)
|
30
|
+
body = {:name => name}.merge(params)
|
31
|
+
|
32
|
+
response = GithubApi::HTTP.post("/user/repos",
|
33
|
+
:query => { :access_token => @token},
|
34
|
+
:body => body.to_json
|
35
|
+
).parsed_response
|
36
|
+
|
37
|
+
@repos << Repo.new(self, response)
|
38
|
+
@repos.last
|
39
|
+
end
|
40
|
+
|
41
|
+
def repo(name)
|
42
|
+
load_repos if @repos.empty?
|
43
|
+
@repos.find { |repo| repo.name == name }
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
data/spec/blob_spec.rb
ADDED
data/spec/repo_spec.rb
ADDED
data/spec/user_spec.rb
ADDED
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: github-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Rune Madsen
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-11-04 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: json
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rspec
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 15
|
43
|
+
segments:
|
44
|
+
- 2
|
45
|
+
- 6
|
46
|
+
version: "2.6"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: httparty
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :runtime
|
62
|
+
version_requirements: *id003
|
63
|
+
description: A simple gem to simplify requests to the Github API
|
64
|
+
email:
|
65
|
+
- rune@runemadsen.com
|
66
|
+
executables: []
|
67
|
+
|
68
|
+
extensions: []
|
69
|
+
|
70
|
+
extra_rdoc_files: []
|
71
|
+
|
72
|
+
files:
|
73
|
+
- .DS_Store
|
74
|
+
- .gitignore
|
75
|
+
- Gemfile
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- github-api.gemspec
|
79
|
+
- lib/github-api.rb
|
80
|
+
- lib/github-api/other.rb
|
81
|
+
- lib/github-api/repo.rb
|
82
|
+
- lib/github-api/user.rb
|
83
|
+
- lib/github-api/version.rb
|
84
|
+
- spec/blob_spec.rb
|
85
|
+
- spec/repo_spec.rb
|
86
|
+
- spec/user_spec.rb
|
87
|
+
homepage: ""
|
88
|
+
licenses: []
|
89
|
+
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 3
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
requirements: []
|
114
|
+
|
115
|
+
rubyforge_project: github-api
|
116
|
+
rubygems_version: 1.8.6
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: A simple gem to simplify requests to the Github API
|
120
|
+
test_files:
|
121
|
+
- spec/blob_spec.rb
|
122
|
+
- spec/repo_spec.rb
|
123
|
+
- spec/user_spec.rb
|