github_api 0.1.0.pre → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,48 +3,92 @@
3
3
  module Github
4
4
  class Repos
5
5
  module Watching
6
-
6
+
7
7
  # List repo watchers
8
8
  #
9
- # GET /repos/:user/:repo/watchers
9
+ # = Examples
10
+ # @github = Github.new :user => 'user-name', :repo => 'repo-name'
11
+ # @github.repos.watchers
12
+ # @github.repos.watchers { |watcher| ... }
10
13
  #
11
- def watchers
12
- get("/repos/#{user}/#{repo}/watchers")
14
+ def watchers(user_name=nil, repo_name=nil, params={})
15
+ _update_user_repo_params(user_name, repo_name)
16
+ _validate_user_repo_params(user, repo) unless user? && repo?
17
+ _normalize_params_keys(params)
18
+
19
+ response = get("/repos/#{user}/#{repo}/watchers")
20
+ return response unless block_given?
21
+ response.each { |el| yield el }
13
22
  end
14
-
15
- # List repos being watched
16
- def watched(user=nil)
17
- if user
23
+
24
+ # List repos being watched by a user
25
+ #
26
+ # = Examples
27
+ # @github = Github.new :user => 'user-name'
28
+ # @github.repos.watched
29
+ #
30
+ # List repos being watched by the authenticated user
31
+ #
32
+ # = Examples
33
+ # @github = Github.new :oauth_token => '...'
34
+ # @github.repos.watched
35
+ #
36
+ def watched(user_name=nil, params={})
37
+ _update_user_repo_params(user_name)
38
+ _normalize_params_keys(params)
39
+
40
+ response = if user
18
41
  get("/users/#{user}/watched")
19
42
  else
20
43
  get("/user/watched")
21
44
  end
45
+ return response unless block_given?
46
+ response.each { |el| yield el }
22
47
  end
23
48
 
24
- # Check if you are watching a repo
49
+ # Check if you are watching a repository
25
50
  #
26
- # GET /user/watched/:user/:repo
51
+ # Returns <tt>true</tt> if this repo is watched by you, <tt>false</tt> otherwise
52
+ # = Examples
53
+ # @github = Github.new
54
+ # @github.repos.watching? 'user-name', 'repo-name'
27
55
  #
28
- def watching?(user, repo)
29
- get("/user/watched/#{user}/#{repo}")
56
+ def watching?(user_name, repo_name, params={})
57
+ _validate_presence_of user_name, repo_name
58
+ _normalize_params_keys(params)
59
+ get("/user/watched/#{user_name}/#{repo_name}")
60
+ true
61
+ rescue Github::ResourceNotFound
62
+ false
30
63
  end
31
64
 
32
- # Watch a repo
65
+ # Watch a repository
33
66
  #
34
- # PUT /user/watched/:user/:repo
67
+ # You need to be authenticated to watch a repository
35
68
  #
36
- def start_watching(user, repo)
37
- put("/user/watched/#{user}/#{repo}")
69
+ # = Examples
70
+ # @github = Github.new
71
+ # @github.repos.start_watching 'user-name', 'repo-name'
72
+ #
73
+ def start_watching(user_name, repo_name, params={})
74
+ _validate_presence_of user_name, repo_name
75
+ _normalize_params_keys(params)
76
+ put("/user/watched/#{user_name}/#{repo_name}")
38
77
  end
39
-
40
- # Stop watching a repo
78
+
79
+ # Stop watching a repository
41
80
  #
42
- # DELETE /user/watched/:user/:repo
81
+ # You need to be authenticated to stop watching a repository.
82
+ # = Examples
83
+ # @github = Github.new
84
+ # @github.repos.start_watching 'user-name', 'repo-name'
43
85
  #
44
- def stop_watching(user, repo)
45
- delete("/user/watched/#{user}/#{repo}")
86
+ def stop_watching(user_name, repo_name, params={})
87
+ _validate_presence_of user_name, repo_name
88
+ _normalize_params_keys(params)
89
+ delete("/user/watched/#{user_name}/#{repo_name}")
46
90
  end
47
91
 
48
- end
49
- end
50
- end
92
+ end # Watching
93
+ end # Repos
94
+ end # Github
@@ -8,9 +8,9 @@ module Github
8
8
  dependency 'oauth2'
9
9
 
10
10
  def call(env)
11
- puts "ENV: #{env.inspect}"
12
- puts "TOKEN : #{@token}"
13
- puts "APP: #{@app}"
11
+ # puts "ENV: #{env.inspect}"
12
+ # puts "TOKEN : #{@token}"
13
+ # puts "APP: #{@app}"
14
14
 
15
15
  # Extract parameters from the query
16
16
  params = env[:url].query_values || {}
@@ -5,7 +5,7 @@ module Github
5
5
  MAJOR = 0
6
6
  MINOR = 1
7
7
  PATCH = 0
8
- BUILD = 'pre'
8
+ BUILD = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.');
11
11
  end
@@ -0,0 +1,6 @@
1
+ {
2
+ "url": "https://api.github.com/user/keys/1",
3
+ "id": 1,
4
+ "title": "octocat@octomac",
5
+ "key": "ssh-rsa AAA..."
6
+ }
@@ -0,0 +1,8 @@
1
+ [
2
+ {
3
+ "url": "https://api.github.com/user/keys/1",
4
+ "id": 1,
5
+ "title": "octocat@octomac",
6
+ "key": "ssh-rsa AAA..."
7
+ }
8
+ ]
@@ -0,0 +1,29 @@
1
+ [
2
+ {
3
+ "url": "https://api.github.com/repos/octocat/Hello-World",
4
+ "html_url": "https://github.com/octocat/Hello-World",
5
+ "clone_url": "https://github.com/octocat/Hello-World.git",
6
+ "git_url": "git://github.com/octocat/Hello-World.git",
7
+ "ssh_url": "git@github.com:octocat/Hello-World.git",
8
+ "svn_url": "https://svn.github.com/octocat/Hello-World",
9
+ "owner": {
10
+ "login": "octocat",
11
+ "id": 1,
12
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
13
+ "url": "https://api.github.com/users/octocat"
14
+ },
15
+ "name": "Hello-World",
16
+ "description": "This your first repo!",
17
+ "homepage": "https://github.com",
18
+ "language": null,
19
+ "private": false,
20
+ "fork": false,
21
+ "forks": 9,
22
+ "watchers": 80,
23
+ "size": 108,
24
+ "master_branch": "master",
25
+ "open_issues": 0,
26
+ "pushed_at": "2011-01-26T19:06:43Z",
27
+ "created_at": "2011-01-26T19:01:12Z"
28
+ }
29
+ ]
@@ -0,0 +1,8 @@
1
+ [
2
+ {
3
+ "login": "octocat",
4
+ "id": 1,
5
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
6
+ "url": "https://api.github.com/users/octocat"
7
+ }
8
+ ]
@@ -1,6 +1,25 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Github::API do
4
-
5
- pending
4
+
5
+ let(:api) { Github::API.new }
6
+
7
+ before(:each) do
8
+ @params = { 'a' => { :b => { 'c' => 1 }, 'd' => [ 'a', { :e => 2 }] } }
9
+ end
10
+
11
+ it "should stringify all the keys inside nested hash" do
12
+ actual = api.send(:_normalize_params_keys, @params)
13
+ expected = { 'a' => { 'b'=> { 'c' => 1 }, 'd' => [ 'a', { 'e'=> 2 }] } }
14
+ actual.should == expected
15
+ end
16
+
17
+ it "should filter param keys" do
18
+ valid = ['a', 'b', 'e']
19
+ hash = {'a' => 1, 'b' => 3, 'c' => 2, 'd'=> 4, 'e' => 5 }
20
+ actual = api.send(:_filter_params_keys, valid, hash)
21
+ expected = {'a' => 1, 'b' => 3, 'e' => 5 }
22
+ actual.should == expected
23
+ end
24
+
6
25
  end
@@ -1,5 +1,205 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Github::Repos::Keys do
4
- pending
5
- end
4
+
5
+ let(:github) { Github.new }
6
+ let(:user) { 'peter-murach'}
7
+ let(:repo) { 'github' }
8
+
9
+ describe "keys" do
10
+
11
+ context "resource found" do
12
+ before do
13
+ stub_get("/repos/#{user}/#{repo}/keys").
14
+ to_return(:body => fixture("repos/keys.json"), :status => 200, :headers => {})
15
+ end
16
+
17
+ it "should fail to get resource without username" do
18
+ github.user, github.repo = nil, nil
19
+ expect { github.repos.keys }.to raise_error(ArgumentError)
20
+ end
21
+
22
+ it "should get the resources" do
23
+ github.repos.keys(user, repo)
24
+ a_get("/repos/#{user}/#{repo}/keys").should have_been_made
25
+ end
26
+
27
+ it "should return array of resources" do
28
+ keys = github.repos.keys(user, repo)
29
+ keys.should be_an Array
30
+ keys.should have(1).items
31
+ end
32
+
33
+ it "should get key information" do
34
+ keys = github.repos.keys(user, repo)
35
+ keys.first.title.should == 'octocat@octomac'
36
+ end
37
+ end
38
+
39
+ context "resource not found" do
40
+ before do
41
+ stub_get("/repos/#{user}/#{repo}/keys").
42
+ to_return(:body => fixture("repos/keys.json"), :status => 404)
43
+ end
44
+
45
+ it "should fail to retrieve resource" do
46
+ expect {
47
+ github.repos.keys user, repo
48
+ }.to raise_error(Github::ResourceNotFound)
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ describe "get_key" do
55
+ let(:key_id) { 1 }
56
+
57
+ context "resource found" do
58
+ before do
59
+ stub_get("/repos/#{user}/#{repo}/keys/#{key_id}").
60
+ to_return(:body => fixture("repos/key.json"), :status => 200)
61
+ end
62
+
63
+ it "should fail to get resource without key" do
64
+ expect {
65
+ github.repos.get_key(user, repo, nil)
66
+ }.to raise_error(ArgumentError)
67
+ end
68
+
69
+ it "should get the resource" do
70
+ github.repos.get_key(user, repo, key_id)
71
+ a_get("/repos/#{user}/#{repo}/keys/#{key_id}").should have_been_made
72
+ end
73
+
74
+ it "should get key information" do
75
+ key = github.repos.get_key(user, repo, key_id)
76
+ key.id.should == key_id
77
+ end
78
+ end
79
+
80
+ context "resource not found" do
81
+ before do
82
+ stub_get("/repos/#{user}/#{repo}/keys/#{key_id}").
83
+ to_return(:body => fixture("repos/keys.json"), :status => 404)
84
+ end
85
+
86
+ it "should fail to retrieve resource" do
87
+ expect {
88
+ github.repos.get_key(user, repo, key_id)
89
+ }.to raise_error(Github::ResourceNotFound)
90
+ end
91
+ end
92
+ end
93
+
94
+ describe "create_key" do
95
+ let(:inputs) { {:title => "octocat@octomac", :key => "ssh-rsa AAA..." } }
96
+
97
+ context "resource created" do
98
+ before do
99
+ stub_post("/repos/#{user}/#{repo}/keys").
100
+ to_return(:body => fixture("repos/key.json"), :status => 201)
101
+ end
102
+
103
+ it "should fail to create resource if 'title' input is missing" do
104
+ expect {
105
+ github.repos.create_key(user, repo, :key => 'ssh-rsa AAA...')
106
+ }.to raise_error(ArgumentError)
107
+ end
108
+
109
+ it "should fail to create resource if 'key' input is missing" do
110
+ expect {
111
+ github.repos.create_key(user, repo, :title => 'octocat@octomac')
112
+ }.to raise_error(ArgumentError)
113
+ end
114
+
115
+ it "should create the resource" do
116
+ github.repos.create_key(user, repo, inputs)
117
+ a_post("/repos/#{user}/#{repo}/keys").should have_been_made
118
+ end
119
+
120
+ it "should get the key information back" do
121
+ key = github.repos.create_key(user, repo, inputs)
122
+ key.title.should == 'octocat@octomac'
123
+ end
124
+ end
125
+
126
+ context "failed to create resource" do
127
+ before do
128
+ stub_post("/repos/#{user}/#{repo}/keys").
129
+ to_return(:body => fixture("repos/key.json"), :status => 404)
130
+ end
131
+
132
+ it "should fail to retrieve resource" do
133
+ expect {
134
+ github.repos.create_key(user, repo, inputs)
135
+ }.to raise_error(Github::ResourceNotFound)
136
+ end
137
+ end
138
+ end
139
+
140
+ describe "edit_key" do
141
+ let(:key_id) { 1 }
142
+ let(:inputs) { {:title => "octocat@octomac", :key => "ssh-rsa AAA..." } }
143
+
144
+ context "resource edited successfully" do
145
+ before do
146
+ stub_patch("/repos/#{user}/#{repo}/keys/#{key_id}").
147
+ to_return(:body => fixture("repos/key.json"), :status => 200)
148
+ end
149
+
150
+ it "should edit the resource" do
151
+ github.repos.edit_key(user, repo, key_id, inputs)
152
+ a_patch("/repos/#{user}/#{repo}/keys/#{key_id}").should have_been_made
153
+ end
154
+
155
+ it "should get the key information back" do
156
+ key = github.repos.edit_key(user, repo, key_id, inputs)
157
+ key.id.should == key_id
158
+ key.title.should == 'octocat@octomac'
159
+ end
160
+ end
161
+
162
+ context "failed to edit resource" do
163
+ before do
164
+ stub_patch("/repos/#{user}/#{repo}/keys/#{key_id}").
165
+ to_return(:body => fixture("repos/key.json"), :status => 404)
166
+ end
167
+
168
+ it "should fail to retrieve resource" do
169
+ expect {
170
+ github.repos.edit_key(user, repo, key_id, inputs)
171
+ }.to raise_error(Github::ResourceNotFound)
172
+ end
173
+ end
174
+
175
+ end
176
+
177
+ describe "delete_key" do
178
+ let(:key_id) { 1 }
179
+
180
+ context "resource found successfully" do
181
+ before do
182
+ stub_delete("/repos/#{user}/#{repo}/keys/#{key_id}").
183
+ to_return(:body => "", :status => 204)
184
+ end
185
+
186
+ it "should delete the resource" do
187
+ github.repos.delete_key(user, repo, key_id)
188
+ a_delete("/repos/#{user}/#{repo}/keys/#{key_id}").should have_been_made
189
+ end
190
+ end
191
+
192
+ context "failed to find resource" do
193
+ before do
194
+ stub_delete("/repos/#{user}/#{repo}/keys/#{key_id}").
195
+ to_return(:body => "", :status => 404)
196
+ end
197
+ it "should fail to find resource" do
198
+ expect {
199
+ github.repos.delete_key(user, repo, key_id)
200
+ }.to raise_error(Github::ResourceNotFound)
201
+ end
202
+ end
203
+ end
204
+
205
+ end # Github::Repos::Keys
@@ -1,5 +1,212 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Github::Repos::Watching do
4
- pending
5
- end
6
+
7
+ let(:github) { Github.new }
8
+ let(:user) { 'peter-murach' }
9
+ let(:repo) { 'github' }
10
+
11
+ describe "watchers" do
12
+ before do
13
+ stub_get("/repos/#{user}/#{repo}/watchers").
14
+ to_return(:body => fixture("repos/watchers.json"), :status => 200, :headers => {})
15
+ end
16
+
17
+ it "should fail to get resource without username" do
18
+ github.user, github.repo = nil, nil
19
+ expect { github.repos.watchers }.to raise_error(ArgumentError)
20
+ end
21
+
22
+ it "should yield iterator if block given" do
23
+ pending
24
+ block = lambda { ['a', 'b', 'c'] }
25
+ github.repos.watchers(user, repo, &block)
26
+ end
27
+
28
+ it "should get the resources" do
29
+ github.repos.watchers(user, repo)
30
+ a_get("/repos/#{user}/#{repo}/watchers").should have_been_made
31
+ end
32
+
33
+ it "should return array of resources" do
34
+ watchers = github.repos.watchers(user, repo)
35
+ watchers.should be_an Array
36
+ watchers.should have(1).items
37
+ end
38
+
39
+ it "should get watcher information" do
40
+ watchers = github.repos.watchers(user, repo)
41
+ watchers.first.login.should == 'octocat'
42
+ end
43
+
44
+ context "fail to find resource" do
45
+ before do
46
+ stub_get("/repos/#{user}/#{repo}/watchers").
47
+ to_return(:body => "", :status => 404)
48
+ end
49
+
50
+ it "should return 404 not found message" do
51
+ lambda { github.repos.watchers(user, repo) }.should raise_error(Github::ResourceNotFound)
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ describe "watched" do
59
+
60
+ context "if user unauthenticated" do
61
+ before do
62
+ WebMock.reset!
63
+ end
64
+
65
+ it "should fail to get resource without username " do
66
+ stub_get("/user/watched").
67
+ to_return(:body => fixture("repos/watched.json"), :status => 401, :headers => {})
68
+ expect {
69
+ github.user = nil
70
+ github.repos.watched
71
+ }.to raise_error(Github::Unauthorised)
72
+ end
73
+
74
+ it "should get the resource with username" do
75
+ stub_get("/users/#{user}/watched").
76
+ to_return(:body => fixture("repos/watched.json"), :status => 200, :headers => {})
77
+ github.repos.watched(user)
78
+ a_get("/users/#{user}/watched").should have_been_made
79
+ end
80
+ end
81
+
82
+ context "if user authenticated" do
83
+ before do
84
+ github.user = nil
85
+ github.oauth_token = OAUTH_TOKEN
86
+ stub_get("/user/watched?access_token=#{OAUTH_TOKEN}").
87
+ to_return(:body => fixture("repos/watched.json"), :status => 200, :headers => {})
88
+ end
89
+
90
+ it "should get the resources" do
91
+ github.repos.watched
92
+ a_get("/user/watched?access_token=#{OAUTH_TOKEN}").should have_been_made
93
+ end
94
+
95
+ it "should return array of resources" do
96
+ watched = github.repos.watched
97
+ watched.should be_an Array
98
+ watched.should have(1).items
99
+ end
100
+
101
+ it "should get watched information" do
102
+ watched = github.repos.watched
103
+ watched.first.name.should == 'Hello-World'
104
+ watched.first.owner.login.should == 'octocat'
105
+ end
106
+ end
107
+ end
108
+
109
+ describe "watching?" do
110
+
111
+ context "with username ane reponame passed" do
112
+
113
+ context "this repo is being watched by the user"
114
+ before do
115
+ github.oauth_token = nil
116
+ github.user = nil
117
+ stub_get("/user/watched/#{user}/#{repo}").
118
+ to_return(:body => "", :status => 404, :headers => {:user_agent => github.user_agent})
119
+ end
120
+
121
+ it "should return false if resource not found" do
122
+ watching = github.repos.watching? user, repo
123
+ watching.should be_false
124
+ end
125
+
126
+ it "should return true if resoure found" do
127
+ stub_get("/user/watched/#{user}/#{repo}").
128
+ to_return(:body => "", :status => 200, :headers => {:user_agent => github.user_agent})
129
+ watching = github.repos.watching? user, repo
130
+ watching.should be_true
131
+ end
132
+
133
+ end
134
+
135
+ context "without username and reponame passed" do
136
+ it "should fail validation " do
137
+ expect { github.repos.watching?(nil, nil) }.to raise_error(ArgumentError)
138
+ end
139
+ end
140
+ end
141
+
142
+ describe "start_watching" do
143
+
144
+ context "user authenticated" do
145
+
146
+ context "with correct information" do
147
+ before do
148
+ github.user, github.repo = nil, nil
149
+ github.oauth_token = OAUTH_TOKEN
150
+ stub_put("/user/watched/#{user}/#{repo}?access_token=#{OAUTH_TOKEN}").
151
+ to_return(:body => "", :status => 204, :headers => {})
152
+ end
153
+
154
+ it "should successfully watch a repo" do
155
+ github.repos.start_watching(user, repo)
156
+ a_put("/user/watched/#{user}/#{repo}?access_token=#{OAUTH_TOKEN}").should have_been_made
157
+ end
158
+ end
159
+
160
+ context "without correct information" do
161
+
162
+ end
163
+ end
164
+
165
+ context "user unauthenticated" do
166
+ it "should fail" do
167
+ github.oauth_token = nil
168
+ stub_put("/user/watched/#{user}/#{repo}").
169
+ to_return(:body => "", :status => 401, :headers => {})
170
+ expect {
171
+ github.repos.start_watching(user, repo)
172
+ }.to raise_error(Github::Unauthorised)
173
+ end
174
+ end
175
+ end
176
+
177
+ describe "stop_watching" do
178
+
179
+ context "user authenticated" do
180
+
181
+ context "with correct information" do
182
+ before do
183
+ github.user, github.repo = nil, nil
184
+ github.oauth_token = OAUTH_TOKEN
185
+ stub_delete("/user/watched/#{user}/#{repo}?access_token=#{OAUTH_TOKEN}").
186
+ to_return(:body => "", :status => 204, :headers => {})
187
+ end
188
+
189
+ it "should successfully watch a repo" do
190
+ github.repos.stop_watching(user, repo)
191
+ a_delete("/user/watched/#{user}/#{repo}?access_token=#{OAUTH_TOKEN}").should have_been_made
192
+ end
193
+ end
194
+
195
+ context "without correct information" do
196
+
197
+ end
198
+ end
199
+
200
+ context "user unauthenticated" do
201
+ it "should fail" do
202
+ github.oauth_token = nil
203
+ stub_delete("/user/watched/#{user}/#{repo}").
204
+ to_return(:body => "", :status => 401, :headers => {})
205
+ expect {
206
+ github.repos.stop_watching(user, repo)
207
+ }.to raise_error(Github::Unauthorised)
208
+ end
209
+ end
210
+ end
211
+
212
+ end # Github::Respos::Watching
@@ -1,9 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Github::Repos do
4
-
4
+
5
5
  let(:github) { Github.new }
6
- let(:repo) { mock('object').as_null_object }
6
+ let(:repo) { mock('object').as_null_object }
7
7
 
8
8
  before do
9
9
  github.stub(:repos).and_return(repo)
@@ -17,18 +17,21 @@ describe Github::Repos do
17
17
  end
18
18
 
19
19
  it "should raise error when no user" do
20
+ pending
20
21
  expect {
21
22
  Github.new.repos.branches
22
23
  }.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
23
24
  end
24
25
 
25
26
  it "should raise error when no repo" do
27
+ pending
26
28
  expect {
27
29
  Github.new(:user => 'peter-murach').repos.branches
28
30
  }.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
29
31
  end
30
32
 
31
33
  it "should list all branches" do
34
+ pending
32
35
  github.repos.should_receive(:branches).and_return(@branches)
33
36
  end
34
37
  end