class GitHub::User

Basic model, stores retrieved user and his associations

Public Class Methods

get(login) click to toggle source

Static function, that gets information about GitHub::User by login.

Examples

GitHub::User.get('defunkt') #=> GitHub::User

@param [String] login GitHub user login to fetch @return [GitHub::User] Newly created, in local database user synced with github

# File lib/github-api-client/user.rb, line 27
def self.get(login)
  if u = GitHub::User.find_by_login(login)
    u.get
  else
    u = GitHub::User.new(:login => login).fetch(:self)
  end
end
get_followers(login) click to toggle source

Returns an array with logins of GitHub::User followers @param [String] login GitHub login of which followers will be mapped @return [Array<GitHub::User>] Followers @deprecated It should not support such way, there should be objects!

# File lib/github-api-client/user.rb, line 152
def self.get_followers(login)
  users = YAML::load(GitHub::Browser.get "/user/show/#{login}/followers")['users']
  
  # Loading each user (not effective with 688 followers like schacon has)
  objects = []
  i = 1
  users.each do |user|
    puts "#{users.length.to_s} / #{i.to_s} - Fetching followers"
    i = i + 1
    u = GitHub::User.find_or_create_by_login(user)
    objects << u
  end
  return objects
end

Public Instance Methods

auth_info() click to toggle source

Collects information from authenticated user. Used by post requests to authenticate @return [Hash] Collected from GitHub::User options for HTTP POST request authentication

# File lib/github-api-client/user.rb, line 170
def auth_info
  {:login => self.login, :token => self.token}
end
fetch(*things) click to toggle source

End-user way to fetch information @param [Array<Symbol>] things Things to fetch for GitHub::User @option things [Symbol] :self Sync with GitHub Database @option things [Symbol] :followers Map followers from GitHub Database @option things [Symbol] :followings Map user’s followings from GitHub @return [GitHub::User] Chainable, updated User @see GitHub::User#get @see GitHub::User#get_followers

# File lib/github-api-client/user.rb, line 62
def fetch(*things)
  things.each do |thing|
    case thing
      when :self then get
      when :followers then get_followers
      when :followings then get_followings
      when :organizations then get_organizations
      when :repos then get_repos
    end
  end
  self
end
get() click to toggle source

Fetches info about current_user from GitHub GitHub::User.new.build(:login => ‘asd’, :token => ‘token’).get #=> GitHub::User @return [GitHub::User] Chainable self object after syncing attributes with GitHub

# File lib/github-api-client/user.rb, line 14
def get
  self.update_attributes(
    GitHub::Base.parse_attributes(:user_get,
      YAML::load(
        GitHub::Browser.get("/user/show/#{self.login}"))['user']))
  self
end
post(login, options = {}) click to toggle source

Experimental, sets information about GitHub::User or returns authenticated :self @param [String] login Login to which post request will be sent @param [Hash] options Options to include to a post request @option options [Hash] email ‘values’ => ‘test@api.com’ - Sets user email to test@api.com if authenticated @return [String] Request retrieved data

# File lib/github-api-client/user.rb, line 179
def post(login, options = {})
  if [:self, :me].include? login
    login = self.login
  end
  return GitHub::Browser.post "/user/show/#{login}", options.merge(self.auth_info)
end
set(route = [], options = {}) click to toggle source

Experimental function, requests POST transmission to custom path of GitHub API @param [Array] route Route splitted like: [‘users’, ‘search’, ‘chacon’] @param [Hash] options Options to pass with the request @option [Hash] options ‘values’ => ‘test@api.com’

# File lib/github-api-client/user.rb, line 50
def set(route = [], options = {})
  return GitHub::Browser.post "/#{route.join('/')}", options.merge(self.auth_info)
end

Private Instance Methods

get_followers() click to toggle source

Executes when you got a real object @see GitHub::User#fetch @return GitHub::User Chainable after mapping followers association

# File lib/github-api-client/user.rb, line 79
def get_followers
  users = YAML::load(GitHub::Browser.get "/user/show/#{login}/followers")['users'] || []
  puts "Fetching followers for #{"user".color(:yellow).bright} #{self.login.dup.color(:green).bright}"
  i, count = 0, users.count.to_s.color(:green).bright
  self.transaction do
    users.each do |user|
      i += 1
      u = GitHub::User.find_or_create_by_login(user)
      self.association(:followers).find_or_create u
      print "\r#{i.to_s.color(:yellow).bright}/#{count}"
    end
  end
  puts nil
  self
end
get_followings() click to toggle source
# File lib/github-api-client/user.rb, line 95
def get_followings
  users = YAML::load(GitHub::Browser.get "/user/show/#{login}/following")['users'] || []
  puts "Fetching followings for #{"user".color(:yellow).bright} #{self.login.dup.color(:green).bright}"
  i, count = 0, users.count.to_s.color(:green).bright
  self.transaction do
    users.each do |user|
      i += 1
      u = GitHub::User.find_or_create_by_login(user)
      self.association(:followings).find_or_create u
      print "\r#{i.to_s.color(:yellow).bright}/#{count}"
    end
  end
  puts nil
  self
end
get_organizations() click to toggle source
# File lib/github-api-client/user.rb, line 111
def get_organizations
  organizations = YAML::load(GitHub::Browser.get "/user/show/#{login}/organizations")['organizations'] || []
  puts "Fetching organizations for #{"user".color(:yellow).bright} #{self.login.dup.color(:green).bright}"
  i, count = 0, organizations.count.to_s.color(:green).bright
  self.transaction do
    organizations.each do |org|
      i += 1
      u = GitHub::Organization.find_or_create_by_login(org['login'])
      self.association(:organizations).find_or_create u
      print "\r#{i.to_s.color(:yellow).bright}/#{count}"
    end
  end
  puts nil
  self
end
get_repos() click to toggle source

Executes when you got a real object @see GitHub::User#fetch @return GitHub::User Chainable after mapping followers association

# File lib/github-api-client/user.rb, line 130
def get_repos
  repos = YAML::load(GitHub::Browser.get "/repos/show/#{login}")['repositories'] || []
  puts "Fetching repositories for #{"user".color(:yellow).bright} #{self.login.dup.color(:green).bright}"
  i, count = 0, repos.count.to_s.color(:green).bright
  self.transaction do
    repos.each do |repo|
      i += 1
      u = GitHub::User.find_or_create_by_login(repo[:owner])
      r = GitHub::Repo.where(:owner_id => u.id, :name => repo[:name]).first
      r ||= GitHub::Repo.create(:owner => u, :name => repo[:name])
      print "\r#{i.to_s.color(:yellow).bright}/#{count}"
    end
  end
  puts nil
  self
end