github-api-client 0.1.1.4 → 0.1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ class CreateUsers < ActiveRecord::Migration
5
5
  t.string attr
6
6
  end
7
7
 
8
- %w(gravatar_id public_repo_count public_gist_count following_count followers_count score record).each do |attr|
8
+ %w(gravatar_id score record).each do |attr|
9
9
  t.integer attr
10
10
  end
11
11
 
@@ -0,0 +1,30 @@
1
+ class CreateRepos < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :repos, :force => true do |t|
4
+ %w(name description url language integrate_branch homepage source).each do |attr|
5
+ t.string attr
6
+ end
7
+
8
+ %w(owner parent).each do |attr|
9
+ t.references attr
10
+ end
11
+
12
+ # TODO: organization temporarily a string, only when there's no organization model
13
+ %w(size followers_count forks_count score organization_id forks watchers).each do |attr|
14
+ t.integer attr
15
+ end
16
+
17
+ %w(has_downloads fork deleted locked has_wiki private open_issues has_issues).each do |attr|
18
+ t.boolean attr
19
+ end
20
+
21
+ %w(created_at pushed_at).each do |attr|
22
+ t.datetime attr
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.down
28
+ drop_table :repos
29
+ end
30
+ end
@@ -15,7 +15,7 @@ module GitHub
15
15
  # Keeps all the configuration stuff
16
16
  module Config
17
17
  # Constant with defined all the paths used in the application
18
- Path = {:dir => ENV['HOME'] + "/.github", :dbfile => ENV['HOME'] + "/.github/github.db", :migrations => Gem.loaded_specs['github-api-client'].full_gem_path + "/db/migrate", :secrets => ENV['HOME'] + "/.github" + "/secrets.yml"}
18
+ Path = {:dir => ENV['HOME'] + "/.github", :dbfile => ENV['HOME'] + "/.github/github.db", :migrations => Gem.loaded_specs['github-api-client'].full_gem_path + "/db/migrate", :secrets => ENV['HOME'] + "/.github" + "/secrets.yml"}
19
19
 
20
20
  # Sets up the database and migrates it
21
21
  # @return [nil]
@@ -31,6 +31,7 @@ GitHub::Config.setup
31
31
 
32
32
  require 'github_api/base'
33
33
  require 'github_api/user'
34
+ require 'github_api/repo'
34
35
  require 'github_api/browser'
35
36
  require 'rainbow'
36
37
 
@@ -20,7 +20,7 @@ module GitHub
20
20
  count = users.count
21
21
  i = 1
22
22
  users.each do |user|
23
- puts "#{count.to_s} / #{i.to_s} - Updating records"
23
+ puts "#{count.to_s.color(:green).bright} / #{i.to_s.color(:blue).bright} - Updating records"
24
24
  i = i + 1
25
25
  # Disabled because of its length
26
26
  #user.get
@@ -30,14 +30,22 @@ module GitHub
30
30
  end
31
31
 
32
32
  # Converts pitfalls from GitHub API differences into normal data
33
+ # @param [Symbol] resource GitHub Resource to parse
34
+ # @option [Symbol] resource :user Parse attributes of User
35
+ # @option [Symbol] resource :repo Parse attributes of Repo
33
36
  # @param [Hash] attributes GitHub API retrieved attributes to be parsed
34
37
  # @return [Hash] parsed attributes, fully compatibile with local db
35
- def self.parse_attributes(attributes)
36
- {:name => :login, :username => :login, :fullname => :name, :followers => :followers_count, :repos => :public_repo_count, :created => :nil, :permission => :nil}.each do |k, v|
38
+ def self.parse_attributes(resource, attributes)
39
+ hash = case resource
40
+ when :user_get then {:public_repo_count => :nil, :public_gist_count => :nil, :created => :nil, :permission => :nil, :followers_count => :nil, :following_count => :nil}
41
+ when :user_search then {:name => :login, :username => :login, :fullname => :name, :followers => :nil, :repos => :public_repo_count, :created => :nil, :permission => :nil}
42
+ when :repo then {}
43
+ end
44
+ hash.each do |k, v|
37
45
  unless v == :nil
38
- attributes[v.to_sym] = attributes[k.to_sym]
46
+ attributes[v.to_s] = attributes[k.to_s]
39
47
  end
40
- attributes.delete k.to_sym
48
+ attributes.delete k.to_s
41
49
  end
42
50
  attributes
43
51
  end
@@ -0,0 +1,30 @@
1
+ module GitHub
2
+ class Repo < ActiveRecord::Base
3
+ belongs_to :owner, :class_name => 'GitHub::User'
4
+ belongs_to :parent, :class_name => 'GitHub::Repo'
5
+
6
+ def self.get(information)
7
+ GitHub::Repo.find_or_create_by_id(GitHub::Base.parse_attributes(:repo, YAML::load(GitHub::Browser.get("/repos/show/#{information}"))['repository']))
8
+ end
9
+
10
+
11
+ def owner=(user)
12
+ self.owner_id = GitHub::User.find_or_create_by_login(user).id if user.class == String
13
+ if user.class == GitHub::User
14
+ self.owner_id = user.id
15
+ end
16
+ end
17
+
18
+ def parent=(permalink)
19
+ owner = GitHub::User.find_or_create_by_login permalink.split('/').first
20
+ name = permalink.split('/').last
21
+ repo = GitHub::Repo.where(:owner_id => owner.id, :name => name).first
22
+ repo ||= GitHub::Repo.create(:owner_id => owner.id, :name => name)
23
+ self.parent_id = repo.id
24
+ end
25
+
26
+ def permalink
27
+ "#{owner.login}/#{name}"
28
+ end
29
+ end
30
+ end
@@ -3,13 +3,16 @@ module GitHub
3
3
  class User < ActiveRecord::Base
4
4
  has_and_belongs_to_many :followers, :foreign_key => 'follower_id', :association_foreign_key => 'following_id', :join_table => 'followings', :class_name => 'User'
5
5
  has_and_belongs_to_many :followings, :foreign_key => 'following_id', :association_foreign_key => 'follower_id', :join_table => 'followings', :class_name => 'User'
6
+ has_many :repos, :class_name => 'GitHub::Repo', :foreign_key => 'owner_id'
6
7
 
7
8
  # Fetches info about current_user from GitHub
8
9
  # GitHub::User.new.build(:login => 'asd', :token => 'token').get #=> GitHub::User
9
10
  # @return [GitHub::User] Chainable self object after syncing attributes with GitHub
10
11
  def get
11
- self.update_attributes YAML::load(
12
- GitHub::Browser.get("/user/show/#{self.login}"))['user']
12
+ self.update_attributes(
13
+ GitHub::Base.parse_attributes(:user_get,
14
+ YAML::load(
15
+ GitHub::Browser.get("/user/show/#{self.login}"))['user']))
13
16
  self
14
17
  end
15
18
 
@@ -19,7 +22,11 @@ module GitHub
19
22
  # @param [String] login GitHub user login to fetch
20
23
  # @return [GitHub::User] Newly created, in local database user synced with github
21
24
  def self.get(login)
22
- return GitHub::User.find_or_create_by_login YAML::load(GitHub::Browser.get("/user/show/#{login}"))['user']
25
+ if u = GitHub::User.find_by_login(login)
26
+ u.get
27
+ else
28
+ u = GitHub::User.new(:login => login).fetch(:self)
29
+ end
23
30
  end
24
31
 
25
32
  # Searches for users in GitHub database
@@ -28,7 +35,7 @@ module GitHub
28
35
  def self.search(login)
29
36
  users = []
30
37
  YAML::load(GitHub::Browser.get("/user/search/#{login}"))['users'].each do |user|
31
- users << GitHub::User.find_or_create_by_login(GitHub::Base.parse_attributes(user))
38
+ users << GitHub::User.find_or_create_by_login(GitHub::Base.parse_attributes(:user, user))
32
39
  end
33
40
  users
34
41
  end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
+ - 2
8
9
  - 1
9
- - 4
10
- version: 0.1.1.4
10
+ version: 0.1.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Jakub Oko\xC5\x84ski"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-30 00:00:00 +01:00
18
+ date: 2010-12-02 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -73,8 +73,10 @@ extra_rdoc_files: []
73
73
  files:
74
74
  - lib/github_api.rb
75
75
  - lib/github_api/base.rb
76
+ - lib/github_api/repo.rb
76
77
  - lib/github_api/browser.rb
77
78
  - lib/github_api/user.rb
79
+ - db/migrate/003_create_repos.rb
78
80
  - db/migrate/002_create_followings.rb
79
81
  - db/migrate/001_create_users.rb
80
82
  - lib/core_ext/habtm.rb