Class: GitHub::User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/github_api/user.rb

Overview

Basic model, stores retrieved user and his associations

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get(login) ⇒ GitHub::User

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

Examples

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

Parameters:

  • login (String)

    GitHub user login to fetch

Returns:

  • (GitHub::User)

    Newly created, in local database user synced with github



24
25
26
27
28
29
30
# File 'lib/github_api/user.rb', line 24

def self.get()
  if u = GitHub::User.()
    u.get
  else
    u = GitHub::User.new(:login => ).fetch(:self)
  end
end

.get_followers(login) ⇒ Array<GitHub::User>

Deprecated.

It should not support such way, there should be objects!

Returns an array with logins of GitHub::User followers

Parameters:

  • login (String)

    GitHub login of which followers will be mapped

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/github_api/user.rb', line 105

def self.get_followers()
  users = YAML::load(GitHub::Browser.get "/user/show/#{}/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.(user)
    objects << u
  end
  return objects
end

.search(login) ⇒ Array<GitHub::User>

Searches for users in GitHub database

Parameters:

  • login (String)

    GitHub user login to search

Returns:



35
36
37
38
39
40
41
# File 'lib/github_api/user.rb', line 35

def self.search()
  users = []
  YAML::load(GitHub::Browser.get("/user/search/#{}"))['users'].each do |user|
    users << GitHub::User.(GitHub::Base.parse_attributes(:user, user))
  end
  users
end

Instance Method Details

#auth_infoHash

Collects information from authenticated user. Used by post requests to authenticate

Returns:

  • (Hash)

    Collected from GitHub::User options for HTTP POST request authentication



123
124
125
# File 'lib/github_api/user.rb', line 123

def auth_info
  {:login => self., :token => self.token}
end

#fetch(*things) ⇒ GitHub::User

End-user way to fetch information

Parameters:

  • things (Array<Symbol>)

    Things to fetch for GitHub::User

Options Hash (*things):

  • :self (Symbol)

    Sync with GitHub Database

  • :followers (Symbol)

    Map followers from GitHub Database

  • :followings (Symbol)

    Map user’s followings from GitHub

Returns:

See Also:

  • #get
  • #get_followers


59
60
61
62
63
64
65
66
67
68
# File 'lib/github_api/user.rb', line 59

def fetch(*things)
  things.each do |thing|
    case thing
      when :self then get
      when :followers then get_followers
      when :followings then get_followings
    end
  end
  self
end

#getGitHub::User

Fetches info about current_user from GitHub GitHub::User.new.build(:login => ‘asd’, :token => ‘token’).get #=> GitHub::User

Returns:

  • (GitHub::User)

    Chainable self object after syncing attributes with GitHub



11
12
13
14
15
16
17
# File 'lib/github_api/user.rb', line 11

def get
  self.update_attributes(
    GitHub::Base.parse_attributes(:user_get,
      YAML::load(
        GitHub::Browser.get("/user/show/#{self.}"))['user']))
  self
end

#post(login, options = {}) ⇒ String

Experimental, sets information about GitHub::User or returns authenticated :self

Parameters:

  • login (String)

    Login to which post request will be sent

  • options (Hash) (defaults to: {})

    Options to include to a post request

Options Hash (options):

Returns:

  • (String)

    Request retrieved data



132
133
134
135
136
137
# File 'lib/github_api/user.rb', line 132

def post(, options = {})
  if [:self, :me].include? 
     = self.
  end
  return GitHub::Browser.post "/user/show/#{}", options.merge(self.auth_info)
end

#set(route = [], options = {}) ⇒ Object

Experimental function, requests POST transmission to custom path of GitHub API

Parameters:

  • route (Array) (defaults to: [])

    Route splitted like: [‘users’, ‘search’, ‘chacon’]

  • options (Hash) (defaults to: {})

    Options to pass with the request

  • [Hash] (Hash)

    a customizable set of options



47
48
49
# File 'lib/github_api/user.rb', line 47

def set(route = [], options = {})
  return GitHub::Browser.post "/#{route.join('/')}", options.merge(self.auth_info)
end