Class: GitHub::User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/github-api-client/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



27
28
29
30
31
32
33
# File 'lib/github-api-client/user.rb', line 27

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:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/github-api-client/user.rb', line 152

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:



38
39
40
41
42
43
44
# File 'lib/github-api-client/user.rb', line 38

def self.search()
  users = []
  YAML::load(GitHub::Browser.get("/user/search/#{}"))['users'].each do |user|
    users << GitHub::User.(GitHub::Base.parse_attributes(:user_search, 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



170
171
172
# File 'lib/github-api-client/user.rb', line 170

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


62
63
64
65
66
67
68
69
70
71
72
73
# 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

#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



14
15
16
17
18
19
20
# 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.}"))['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



179
180
181
182
183
184
# File 'lib/github-api-client/user.rb', line 179

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



50
51
52
# 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