github-v3-api 0.0.1 → 0.0.2

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{github-v3-api}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Wilger"]
12
- s.date = %q{2011-06-23}
12
+ s.date = %q{2011-06-24}
13
13
  s.description = %q{Ponies}
14
14
  s.email = %q{johnwilger@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -30,11 +30,16 @@ Gem::Specification.new do |s|
30
30
  "github-v3-api.gemspec",
31
31
  "lib/github-v3-api.rb",
32
32
  "lib/github_v3_api.rb",
33
+ "lib/github_v3_api/entity.rb",
33
34
  "lib/github_v3_api/org.rb",
34
35
  "lib/github_v3_api/orgs_api.rb",
36
+ "lib/github_v3_api/repo.rb",
37
+ "lib/github_v3_api/repos_api.rb",
35
38
  "spec/github_v3_api_spec.rb",
36
39
  "spec/org_spec.rb",
37
40
  "spec/orgs_api_spec.rb",
41
+ "spec/repo_spec.rb",
42
+ "spec/repos_api_spec.rb",
38
43
  "spec/spec_helper.rb"
39
44
  ]
40
45
  s.homepage = %q{http://github.com/jwilger/github-v3-api}
@@ -0,0 +1,58 @@
1
+ class GitHubV3API
2
+ # This is the base class used for value objects returned by the API. See
3
+ # descendent classes for more details.
4
+ class Entity
5
+ def self.new_with_all_data(api, data) #:nodoc:
6
+ entity = allocate
7
+ entity.initialize_fetched(api, data)
8
+ entity
9
+ end
10
+
11
+ # +api+:: an instance of the API class associated with the subclass of
12
+ # Entity being instantiated.
13
+ # +data+:: a Hash with keys corresponding to the data fields for the
14
+ # subclass of Entity being instantiated
15
+ def initialize(api, data)
16
+ @api = api
17
+ @data = data
18
+ end
19
+
20
+ def initialize_fetched(api, data) #:nodoc:
21
+ initialize(api, data)
22
+ @fetched = true
23
+ end
24
+
25
+ def [](key) #:nodoc:
26
+ fetch_data unless @fetched
27
+ @data[key]
28
+ end
29
+
30
+ def self.attr_reader(*fields) #:nodoc:
31
+ fields.each do |field|
32
+ define_method field do
33
+ self[field.to_s]
34
+ end
35
+ end
36
+ end
37
+
38
+ protected
39
+
40
+ # Provides access to the raw data hash for subclasses.
41
+ def data
42
+ @data
43
+ end
44
+
45
+ private
46
+
47
+ def fetch_data #:nodoc:
48
+ result = @api.get(*natural_key)
49
+ @data = result.data
50
+ @fetched = true
51
+ end
52
+
53
+ # Provides access to the api object for subclasses
54
+ def api
55
+ @api
56
+ end
57
+ end
58
+ end
@@ -1,58 +1,23 @@
1
1
  # See GitHubV3API documentation in lib/github_v3_api.rb
2
2
  class GitHubV3API
3
3
  # Represents a single GitHub Org and provides access to its data attributes.
4
- class Org
5
- def self.new_with_all_data(api, org_data) #:nodoc:
6
- org = self.allocate
7
- org.initialize_fetched(api, org_data)
8
- org
9
- end
10
-
11
- # +api+:: an instance of GitHubV3API::OrgsAPI
12
- # +org_data+:: a Hash with keys corresponding to the org data supported by
13
- # GitHub
14
- #
15
- def initialize(api, org_data)
16
- @api = api
17
- @org_data = org_data
18
- end
19
-
20
- def initialize_fetched(api, org_data) #:nodoc:
21
- initialize(api, org_data)
22
- @fetched = true
23
- end
24
-
25
- def [](key) #:nodoc:
26
- fetch_data unless @fetched
27
- @org_data[key]
28
- end
29
-
30
- def self.attr_reader(*fields) #:nodoc:
31
- fields.each do |field|
32
- define_method field do
33
- self[field.to_s]
34
- end
35
- end
36
- end
37
-
4
+ class Org < Entity
38
5
  attr_reader :avatar_url, :billing_email, :blog, :collaborators,
39
6
  :company, :created_at, :disk_usage, :email, :followers, :following,
40
7
  :html_url, :id, :location, :login, :name, :owned_private_repos, :plan,
41
8
  :private_gists, :private_repos, :public_gists, :public_repos, :space,
42
9
  :total_private_repos, :type, :url
43
10
 
44
- protected
45
-
46
- def org_data #:nodoc:
47
- @org_data
11
+ # Returns an array of GitHubV3API::Repo instances representing the repos
12
+ # that belong to this org
13
+ def repos
14
+ api.list_repos(login)
48
15
  end
49
16
 
50
- private
17
+ private
51
18
 
52
- def fetch_data #:nodoc:
53
- result = @api.get(@org_data['login'])
54
- @org_data = result.org_data
55
- @fetched = true
19
+ def natural_key
20
+ [data['login']]
56
21
  end
57
22
  end
58
23
  end
@@ -39,5 +39,15 @@ class GitHubV3API
39
39
  org_data = @connection.get("/orgs/#{org_login}")
40
40
  GitHubV3API::Org.new_with_all_data(self, org_data)
41
41
  end
42
+
43
+ # Returns an array of GitHubV3API::Repo instances representing the repos
44
+ # that belong to the specified org.
45
+ #
46
+ # +org_login+:: the string ID of the organization, e.g. "github"
47
+ def list_repos(org_login)
48
+ @connection.get("/orgs/#{org_login}/repos").map do |repo_data|
49
+ GitHubV3API::Repo.new(@connection.repos, repo_data)
50
+ end
51
+ end
42
52
  end
43
53
  end
@@ -0,0 +1,16 @@
1
+ # See GitHubV3API documentation in lib/github_v3_api.rb
2
+ class GitHubV3API
3
+ # Represents a single GitHub Repo and provides access to its data attributes.
4
+ class Repo < Entity
5
+ attr_reader :created_at, :description, :fork, :forks, :has_downloads,
6
+ :has_issues, :has_wiki, :homepage, :html_url, :language, :master_branch,
7
+ :name, :open_issues, :organization, :owner, :parent, :private, :pushed_at,
8
+ :size, :source, :url, :watchers
9
+
10
+ private
11
+
12
+ def natural_key
13
+ [data['owner']['login'], data['name']]
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,45 @@
1
+ # See GitHubV3API documentation in lib/github_v3_api.rb
2
+ class GitHubV3API
3
+ # Provides access to the GitHub Repos API (http://developer.github.com/v3/repos/)
4
+ #
5
+ # example:
6
+ #
7
+ # api = GitHubV3API.new(ACCESS_TOKEN)
8
+ #
9
+ # # get list of all of the user's public and private repos
10
+ # repos = api.repos.list
11
+ # #=> returns an array of GitHubV3API::Repo instances
12
+ #
13
+ # repo = api.repo.get('octocat', 'hello-world')
14
+ # #=> returns an instance of GitHubV3API::Repo
15
+ #
16
+ # repo.name
17
+ # #=> 'hello-world'
18
+ #
19
+ class ReposAPI
20
+ # Typically not used directly. Use GitHubV3API#repos instead.
21
+ #
22
+ # +connection+:: an instance of GitHubV3API
23
+ def initialize(connection)
24
+ @connection = connection
25
+ end
26
+
27
+ # Returns an array of GitHubV3API::Repo instances representing the
28
+ # user's public and private repos
29
+ def list
30
+ @connection.get('/user/repos').map do |repo_data|
31
+ GitHubV3API::Repo.new(self, repo_data)
32
+ end
33
+ end
34
+
35
+ # Returns a GitHubV3API::Repo instance for the specified +user+ and
36
+ # +repo_name+.
37
+ #
38
+ # +user+:: the string ID of the user, e.g. "octocat"
39
+ # +repo_name+:: the string ID of the repository, e.g. "hello-world"
40
+ def get(user, repo_name)
41
+ org_data = @connection.get("/repos/#{user}/#{repo_name}")
42
+ GitHubV3API::Repo.new_with_all_data(self, org_data)
43
+ end
44
+ end
45
+ end
data/lib/github_v3_api.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  require 'rest-client'
2
2
  require 'json'
3
+ require 'github_v3_api/entity'
3
4
  require 'github_v3_api/orgs_api'
4
5
  require 'github_v3_api/org'
6
+ require 'github_v3_api/repos_api'
7
+ require 'github_v3_api/repo'
5
8
 
6
9
  # This is the main entry-point to the GitHub v3 API.
7
10
  #
@@ -30,6 +33,14 @@ class GitHubV3API
30
33
  OrgsAPI.new(self)
31
34
  end
32
35
 
36
+ # Entry-point for access to the GitHub Repos API
37
+ #
38
+ # Returns an instance of GitHubV3API::ReposAPI that will use the access_token
39
+ # associated with this instance.
40
+ def repos
41
+ ReposAPI.new(self)
42
+ end
43
+
33
44
  def get(path) #:nodoc:
34
45
  result = RestClient.get("https://api.github.com" + path,
35
46
  {:accept => :json,
@@ -12,6 +12,13 @@ describe GitHubV3API do
12
12
  end
13
13
  end
14
14
 
15
+ describe '#repos' do
16
+ it 'returns an instance of GitHubV3API::ReposAPI' do
17
+ api = GitHubV3API.new('abcde')
18
+ api.repos.should be_kind_of GitHubV3API::ReposAPI
19
+ end
20
+ end
21
+
15
22
  describe '#get' do
16
23
  it 'does a get request to the specified path at the GitHub API server and adds the access token' do
17
24
  RestClient.should_receive(:get) \
data/spec/org_spec.rb CHANGED
@@ -35,4 +35,13 @@ describe GitHubV3API::Org do
35
35
  org['foo'].should be_nil
36
36
  end
37
37
  end
38
+
39
+ describe '#repos' do
40
+ it 'returns an array of Repo objects that belong to this org' do
41
+ api = mock(GitHubV3API::OrgsAPI)
42
+ api.should_receive(:list_repos).with('github').and_return([:repo1, :repo2])
43
+ org = GitHubV3API::Org.new_with_all_data(api, 'login' => 'github')
44
+ org.repos.should == [:repo1, :repo2]
45
+ end
46
+ end
38
47
  end
@@ -22,4 +22,21 @@ describe GitHubV3API::OrgsAPI do
22
22
  api.get('octocat').should == :org
23
23
  end
24
24
  end
25
+
26
+ describe '#list_repos' do
27
+ it 'returns the public and private repos for the specified org' do
28
+ connection = mock(GitHubV3API, :repos => :repos_api)
29
+ connection.should_receive(:get) \
30
+ .with('/orgs/github/repos') \
31
+ .and_return([:repo1_hash, :repo2_hash])
32
+ GitHubV3API::Repo.should_receive(:new) \
33
+ .with(:repos_api, :repo1_hash) \
34
+ .and_return(:repo1)
35
+ GitHubV3API::Repo.should_receive(:new) \
36
+ .with(:repos_api, :repo2_hash) \
37
+ .and_return(:repo2)
38
+ api = GitHubV3API::OrgsAPI.new(connection)
39
+ api.list_repos('github').should == [:repo1, :repo2]
40
+ end
41
+ end
25
42
  end
data/spec/repo_spec.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe GitHubV3API::Repo do
4
+ describe 'attr_readers' do
5
+ it 'should define attr_readers that pull values from the repo data' do
6
+ fields = %w(created_at description fork forks has_downloads has_issues
7
+ has_wiki homepage html_url language master_branch name open_issues
8
+ organization owner parent private pushed_at size source url watchers)
9
+ fields.each do |f|
10
+ repo = GitHubV3API::Repo.new_with_all_data(stub('api'), {f.to_s => 'foo'})
11
+ repo.methods.should include(f)
12
+ repo.send(f).should == 'foo'
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '#[]' do
18
+ it 'returns the repo data for the specified key' do
19
+ api = stub(GitHubV3API::ReposAPI)
20
+ repo = GitHubV3API::Repo \
21
+ .new_with_all_data(api, 'name' => 'hello-world', 'owner' => {'login' => 'octocat'})
22
+ repo['name'].should == 'hello-world'
23
+ end
24
+
25
+ it 'only fetches the data once' do
26
+ api = mock(GitHubV3API::ReposAPI)
27
+ api.should_receive(:get).once.with('octocat', 'hello-world') \
28
+ .and_return(GitHubV3API::Repo.new(api, 'name' => 'hello-world', 'owner' => {'login' => 'octocat'}, 'private' => true))
29
+ repo = GitHubV3API::Repo.new(api, 'name' => 'hello-world', 'owner' => {'login' => 'octocat'})
30
+ repo['name'].should == 'hello-world'
31
+ repo['owner'].should == {'login' => 'octocat'}
32
+ repo['private'].should == true
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe GitHubV3API::ReposAPI do
4
+ describe '#list' do
5
+ it 'returns the public and private repos for the authenticated user' do
6
+ connection = mock(GitHubV3API)
7
+ connection.should_receive(:get).with('/user/repos').and_return([:repo_hash1, :repo_hash2])
8
+ api = GitHubV3API::ReposAPI.new(connection)
9
+ GitHubV3API::Repo.should_receive(:new).with(api, :repo_hash1).and_return(:repo1)
10
+ GitHubV3API::Repo.should_receive(:new).with(api, :repo_hash2).and_return(:repo2)
11
+ repos = api.list
12
+ repos.should == [:repo1, :repo2]
13
+ end
14
+ end
15
+
16
+ describe '#get' do
17
+ it 'returns a fully-hydrated Repo object for the specified user and repo name' do
18
+ connection = mock(GitHubV3API)
19
+ connection.should_receive(:get).with('/repos/octocat/hello-world').and_return(:repo_hash)
20
+ api = GitHubV3API::ReposAPI.new(connection)
21
+ GitHubV3API::Repo.should_receive(:new_with_all_data).with(api, :repo_hash).and_return(:repo)
22
+ api.get('octocat', 'hello-world').should == :repo
23
+ end
24
+ end
25
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github-v3-api
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Wilger
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-23 00:00:00 -07:00
18
+ date: 2011-06-24 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -167,11 +167,16 @@ files:
167
167
  - github-v3-api.gemspec
168
168
  - lib/github-v3-api.rb
169
169
  - lib/github_v3_api.rb
170
+ - lib/github_v3_api/entity.rb
170
171
  - lib/github_v3_api/org.rb
171
172
  - lib/github_v3_api/orgs_api.rb
173
+ - lib/github_v3_api/repo.rb
174
+ - lib/github_v3_api/repos_api.rb
172
175
  - spec/github_v3_api_spec.rb
173
176
  - spec/org_spec.rb
174
177
  - spec/orgs_api_spec.rb
178
+ - spec/repo_spec.rb
179
+ - spec/repos_api_spec.rb
175
180
  - spec/spec_helper.rb
176
181
  has_rdoc: true
177
182
  homepage: http://github.com/jwilger/github-v3-api