Skip to content

Commit 9ff5102

Browse files
committed
Merge pull request piotrmurach#56 from larrylv/support-params-for-listing-repos
Support parameters for listing repositories.
2 parents 0d472d1 + 7abd8a0 commit 9ff5102

3 files changed

Lines changed: 69 additions & 4 deletions

File tree

lib/github_api/repos.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,17 @@ def languages(user_name, repo_name, params={})
313313
def list(*args)
314314
params = args.extract_options!
315315
normalize! params
316-
filter! %w[ org user type ], params
316+
317+
valid_user_repos_params = %w(type sort direction)
318+
valid_org_repos_params = %w(type)
317319

318320
response = if (user_name = params.delete("user"))
319-
get_request("/users/#{user_name}/repos", params)
321+
get_request("/users/#{user_name}/repos", params.delete_if { |k, v| !valid_user_repos_params.include? k })
320322
elsif (org_name = params.delete("org"))
321-
get_request("/orgs/#{org_name}/repos", params)
323+
get_request("/orgs/#{org_name}/repos", params.delete_if { |k, v| !valid_org_repos_params.include? k })
322324
else
323325
# For authenticated user
324-
get_request("/user/repos", params)
326+
get_request("/user/repos", params.delete_if { |k, v| !valid_user_repos_params.include? k })
325327
end
326328
return response unless block_given?
327329
response.each { |el| yield el }
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[
2+
{
3+
"url": "https://api.github.com/repos/octocat/Hello-World-2",
4+
"html_url": "https://github.com/octocat/Hello-World-2",
5+
"clone_url": "https://github.com/octocat/Hello-World-2.git",
6+
"git_url": "git://github.com/octocat/Hello-World-2.git",
7+
"ssh_url": "[email protected]:octocat/Hello-World-2.git",
8+
"svn_url": "https://svn.github.com/octocat/Hello-World-2",
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-2",
16+
"description": "This your second 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-02-26T19:06:43Z",
27+
"created_at": "2011-02-26T19:01:12Z"
28+
},
29+
{
30+
"url": "https://api.github.com/repos/octocat/Hello-World",
31+
"html_url": "https://github.com/octocat/Hello-World",
32+
"clone_url": "https://github.com/octocat/Hello-World.git",
33+
"git_url": "git://github.com/octocat/Hello-World.git",
34+
"ssh_url": "[email protected]:octocat/Hello-World.git",
35+
"svn_url": "https://svn.github.com/octocat/Hello-World",
36+
"owner": {
37+
"login": "octocat",
38+
"id": 1,
39+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
40+
"url": "https://api.github.com/users/octocat"
41+
},
42+
"name": "Hello-World",
43+
"description": "This your first repo!",
44+
"homepage": "https://github.com",
45+
"language": null,
46+
"private": false,
47+
"fork": false,
48+
"forks": 9,
49+
"watchers": 80,
50+
"size": 108,
51+
"master_branch": "master",
52+
"open_issues": 0,
53+
"pushed_at": "2011-01-26T19:06:43Z",
54+
"created_at": "2011-01-26T19:01:12Z"
55+
}
56+
]

spec/github/repos_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@
442442
github.oauth_token = OAUTH_TOKEN
443443
stub_get("/user/repos?access_token=#{OAUTH_TOKEN}").
444444
to_return(:body => fixture('repos/repos.json'), :status => 200,:headers => {:content_type => "application/json; charset=utf-8"} )
445+
stub_get("/user/repos?access_token=#{OAUTH_TOKEN}&sort=pushed").
446+
to_return(:body => fixture('repos/repos_sorted_by_pushed.json'), :status => 200,:headers => {:content_type => "application/json; charset=utf-8"} )
445447
end
446448

447449
it "fails if user is unauthenticated" do
@@ -462,6 +464,11 @@
462464
repositories.should have(1).items
463465
end
464466

467+
it "should return array of resources sorted by pushed_at time" do
468+
repositories = github.repos.list(:sort => 'pushed')
469+
repositories.first.name.should == "Hello-World-2"
470+
end
471+
465472
it "should get resource information" do
466473
repositories = github.repos.list
467474
repositories.first.name.should == 'Hello-World'

0 commit comments

Comments
 (0)