Module: Octokit::Client::Users

Included in:
Octokit::Client
Defined in:
lib/octokit/client/users.rb

Overview

Methods for the Users API

Instance Method Summary collapse

Instance Method Details

#add_email(email, _options = {}) ⇒ Array<String>

Add email address to user.

Requires authenticated client.

Examples:

@client.add_email('[email protected]')

Parameters:

  • email (String)

    Email address to add to the user.

Returns:

  • (Array<String>)

    Array of all email addresses of the user.

See Also:



339
340
341
342
# File 'lib/octokit/client/users.rb', line 339

def add_email(email, _options = {})
  email = Array(email)
  post 'user/emails', email
end

#add_key(title, key, options = {}) ⇒ Sawyer::Resource

Add public key to user account.

Requires authenticated client.

Examples:

@client.add_key('Personal projects key', 'ssh-rsa AAA...')

Parameters:

  • title (String)

    Title to give reference to the public key.

  • key (String)

    Public key.

Returns:

  • (Sawyer::Resource)

    Hash representing the newly added public key.

See Also:



282
283
284
# File 'lib/octokit/client/users.rb', line 282

def add_key(title, key, options = {})
  post 'user/keys', options.merge({ title: title, key: key })
end

#all_users(options = {}) ⇒ Array<Sawyer::Resource>

List all GitHub users

This provides a list of every user, in the order that they signed up for GitHub.

Parameters:

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

    Optional options.

Options Hash (options):

  • :since (Integer)

    The integer ID of the last User that you’ve seen.

Returns:

  • (Array<Sawyer::Resource>)

    List of GitHub users.

See Also:



21
22
23
# File 'lib/octokit/client/users.rb', line 21

def all_users(options = {})
  paginate 'users', options
end

#delete_user_migration_archive(id, options = {}) ⇒ Object

Deletes a previous migration archive.

Requires authenticated user.

Parameters:

  • id (Integer)

    ID number of the migration.

See Also:



425
426
427
# File 'lib/octokit/client/users.rb', line 425

def delete_user_migration_archive(id, options = {})
  delete "user/migrations/#{id}/archive", options
end

#emails(options = {}) ⇒ Array<String>

List email addresses for a user.

Requires authenticated client.

Examples:

@client.emails

Returns:

  • (Array<String>)

    Array of email addresses.

See Also:



326
327
328
# File 'lib/octokit/client/users.rb', line 326

def emails(options = {})
  paginate 'user/emails', options
end

#exchange_code_for_token(code, app_id = client_id, app_secret = client_secret, options = {}) ⇒ Sawyer::Resource

Retrieve the access_token.

Examples:

Octokit.exchange_code_for_token('aaaa', 'xxxx', 'yyyy', {:accept => 'application/json'})

Parameters:

  • code (String)

    Authorization code generated by GitHub.

  • app_id (String) (defaults to: client_id)

    Client Id we received when our application was registered with GitHub. Defaults to client_id.

  • app_secret (String) (defaults to: client_secret)

    Client Secret we received when our application was registered with GitHub. Defaults to client_secret.

Returns:

  • (Sawyer::Resource)

    Hash holding the access token.

See Also:



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/octokit/client/users.rb', line 46

def exchange_code_for_token(code, app_id = client_id, app_secret = client_secret, options = {})
  options = options.merge({
                            code: code,
                            client_id: app_id,
                            client_secret: app_secret,
                            headers: {
                              content_type: 'application/json',
                              accept: 'application/json'
                            }
                          })

  post "#{web_endpoint}login/oauth/access_token", options
end

#follow(user, options = {}) ⇒ Boolean

Follow a user.

Requires authenticatied client.

Examples:

@client.follow('holman')

Parameters:

  • user (String)

    Username of the user to follow.

Returns:

  • (Boolean)

    True if follow was successful, false otherwise.

See Also:



176
177
178
# File 'lib/octokit/client/users.rb', line 176

def follow(user, options = {})
  boolean_from_response :put, "user/following/#{user}", options
end

#followers(user = login, options = {}) ⇒ Array<Sawyer::Resource>

Get a user’s followers.

Examples:

Octokit.followers('pengwynn')

Parameters:

  • user (Integer, String) (defaults to: login)

    GitHub user login or id of the user whose list of followers you are getting.

Returns:

  • (Array<Sawyer::Resource>)

    Array of hashes representing users followers.

See Also:



126
127
128
# File 'lib/octokit/client/users.rb', line 126

def followers(user = , options = {})
  paginate "#{User.path user}/followers", options
end

#following(user = login, options = {}) ⇒ Array<Sawyer::Resource>

Get list of users a user is following.

Examples:

Octokit.following('pengwynn')

Parameters:

  • user (Intger, String) (defaults to: login)

    GitHub user login or id of the user who you are getting the list of the people they follow.

Returns:

  • (Array<Sawyer::Resource>)

    Array of hashes representing users a user is following.

See Also:



139
140
141
# File 'lib/octokit/client/users.rb', line 139

def following(user = , options = {})
  paginate "#{User.path user}/following", options
end

#follows?(target) ⇒ Boolean #follows?(user, target) ⇒ Boolean

Check if you are following a user. Alternatively, check if a given user is following a target user.

Requries an authenticated client.

Examples:

@client.follows?('pengwynn')
@client.follows?('catsby', 'pengwynn')

Overloads:

  • #follows?(target) ⇒ Boolean

    check if you are following.

    Parameters:

    • target (String)

      GitHub login of the user that you want to

  • #follows?(user, target) ⇒ Boolean

    Parameters:

    • user (Integer, String)

      GitHub user login or id of first user

    • target (String)

      GitHub login of the target user

Returns:

  • (Boolean)

    True following target user, false otherwise.

See Also:



161
162
163
164
165
# File 'lib/octokit/client/users.rb', line 161

def follows?(*args)
  target = args.pop
  user = args.first
  boolean_from_response :get, "#{User.path user}/following/#{target}"
end

#key(key_id, options = {}) ⇒ Sawyer::Resource

Get a public key.

Note, when using dot notation to retrieve the values, ruby will return the hash key for the public keys value instead of the actual value, use symbol or key string to retrieve the value. See example.

Requires authenticated client.

Examples:

@client.key(1)

Retrieve public key contents

public_key = @client.key(1)
public_key.key
# => Error

public_key[:key]
# => "ssh-rsa AAA..."

public_key['key']
# => "ssh-rsa AAA..."

Parameters:

  • key_id (Integer)

    Key to retreive.

Returns:

  • (Sawyer::Resource)

    Hash representing the key.

See Also:



244
245
246
# File 'lib/octokit/client/users.rb', line 244

def key(key_id, options = {})
  get "user/keys/#{key_id}", options
end

#keys(options = {}) ⇒ Array<Sawyer::Resource>

Get list of public keys for user.

Requires authenticated client.

Examples:

@client.keys

Returns:

  • (Array<Sawyer::Resource>)

    Array of hashes representing public keys.

See Also:



256
257
258
# File 'lib/octokit/client/users.rb', line 256

def keys(options = {})
  paginate 'user/keys', options
end

#refresh_access_token(code, app_id = client_id, app_secret = client_secret, options = {}) ⇒ Sawyer::Resource

Refresh a user’s access token with a refresh token.

Applications can refresh an access token without requiring a user to re-authorize using refresh access token.

Examples:

client = Octokit::Client.new(:client_id => 'abcdefg12345', :client_secret => 'secret')
client.refresh_access_token('40-character-refresh-token')

Parameters:

  • code (String)

    40 character GitHub OAuth refresh access token

Returns:

  • (Sawyer::Resource)

See Also:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/octokit/client/users.rb', line 72

def refresh_access_token(code, app_id = client_id, app_secret = client_secret, options = {})
  options = options.merge({
                            refresh_token: code,
                            client_id: app_id,
                            client_secret: app_secret,
                            grant_type: 'refresh_token',
                            headers: {
                              content_type: 'application/json',
                              accept: 'application/json'
                            }
                          })

  post "#{web_endpoint}login/oauth/access_token", options
end

#remove_email(email) ⇒ Array<String>

Remove email from user.

Requires authenticated client.

Examples:

@client.remove_email('[email protected]')

Parameters:

  • email (String)

    Email address to remove.

Returns:

  • (Array<String>)

    Array of all email addresses of the user.

See Also:



353
354
355
356
# File 'lib/octokit/client/users.rb', line 353

def remove_email(email)
  email = Array(email)
  boolean_from_response :delete, 'user/emails', email
end

#remove_key(id, options = {}) ⇒ Boolean

Remove a public key from user account.

Requires authenticated client.

Examples:

@client.remove_key(1)

Parameters:

  • id (String)

    Id of the public key to remove.

Returns:

  • (Boolean)

    True if removal was successful, false otherwise.

See Also:



314
315
316
# File 'lib/octokit/client/users.rb', line 314

def remove_key(id, options = {})
  boolean_from_response :delete, "user/keys/#{id}", options
end

#starred(user = login, options = {}) ⇒ Array<Sawyer::Resource>

Get list of repos starred by a user.

Examples:

Octokit.starred('pengwynn')

Parameters:

  • user (Integer, String) (defaults to: login)

    GitHub user login of the user to get the list of their starred repositories.

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

    Optional options

Options Hash (options):

  • :sort (String) — default: created

    Sort: created or updated.

  • :direction (String) — default: desc

    Direction: asc or desc.

Returns:

  • (Array<Sawyer::Resource>)

    Array of hashes representing repositories starred by user.

See Also:



204
205
206
# File 'lib/octokit/client/users.rb', line 204

def starred(user = , options = {})
  paginate user_path(user, 'starred'), options
end

#starred?(repo, options = {}) ⇒ Boolean

Check if you are starring a repo.

Requires authenticated client.

Examples:

@client.starred?('pengwynn/octokit')

Parameters:

  • repo (String, Hash, Repository)

    A GitHub repository

Returns:

  • (Boolean)

    True if you are following the repo, false otherwise.

See Also:



217
218
219
# File 'lib/octokit/client/users.rb', line 217

def starred?(repo, options = {})
  boolean_from_response :get, "user/starred/#{Repository.new(repo)}", options
end

#start_user_migration(repositories, options = {}) ⇒ Sawyer::Resource

Initiates the generation of a migration archive.

Requires authenticated user.

Examples:

@client.start_migration(['octocat/hello-world'])

Parameters:

  • repositories (Array<String>)

    :repositories Repositories for the organization.

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

    a customizable set of options

Options Hash (options):

  • :lock_repositories (Boolean, optional)

    Indicates whether repositories should be locked during migration

  • :exclude_attachments (Boolean, optional)

    Exclude attachments fro the migration data

Returns:

  • (Sawyer::Resource)

    Hash representing the new migration.

See Also:



381
382
383
384
# File 'lib/octokit/client/users.rb', line 381

def start_user_migration(repositories, options = {})
  options[:repositories] = repositories
  post 'user/migrations', options
end

#subscriptions(user = login, options = {}) ⇒ Array<Sawyer::Resource> Also known as: watched

List repositories being watched by a user.

Examples:

@client.subscriptions("pengwynn")

Parameters:

  • user (Integer, String) (defaults to: login)

    GitHub user login or id.

Returns:

  • (Array<Sawyer::Resource>)

    Array of repositories.

See Also:



365
366
367
# File 'lib/octokit/client/users.rb', line 365

def subscriptions(user = , options = {})
  paginate user_path(user, 'subscriptions'), options
end

#unfollow(user, options = {}) ⇒ Boolean

Unfollow a user.

Requires authenticated client.

Examples:

@client.unfollow('holman')

Parameters:

  • user (String)

    Username of the user to unfollow.

Returns:

  • (Boolean)

    True if unfollow was successful, false otherwise.

See Also:



189
190
191
# File 'lib/octokit/client/users.rb', line 189

def unfollow(user, options = {})
  boolean_from_response :delete, "user/following/#{user}", options
end

#unlock_user_repository(id, repo, options = {}) ⇒ Object

Unlock a user repository which has been locked by a migration.

Requires authenticated user.

Parameters:

  • id (Integer)

    ID number of the migration.

  • repo (String)

    Name of the repository.

See Also:



446
447
448
# File 'lib/octokit/client/users.rb', line 446

def unlock_user_repository(id, repo, options = {})
  delete "user/migrations/#{id}/repos/#{repo}/lock", options
end

#update_key(key_id, options = {}) ⇒ Sawyer::Resource

Deprecated.

This method is no longer supported in the API

Update a public key

Requires authenticated client

Examples:

@client.update_key(1, :title => 'new title', :key => "ssh-rsa BBB")

Parameters:

  • key_id (Integer)

    Id of key to update.

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

    Hash containing attributes to update.

Options Hash (options):

  • :title (String)
  • :key (String)

Returns:

  • (Sawyer::Resource)

    Hash representing the updated public key.

See Also:



301
302
303
# File 'lib/octokit/client/users.rb', line 301

def update_key(key_id, options = {})
  patch "user/keys/#{key_id}", options
end

#update_user(options) ⇒ Sawyer::Resource

Update the authenticated user

Examples:

Octokit.update_user(:name => "Erik Michaels-Ober", :email => "[email protected]", :company => "Code for America", :location => "San Francisco", :hireable => false)

Parameters:

  • options (Hash)

    A customizable set of options.

Options Hash (options):

  • :name (String)
  • :email (String)

    Publically visible email address.

  • :blog (String)
  • :company (String)
  • :location (String)
  • :hireable (Boolean)
  • :bio (String)

Returns:

  • (Sawyer::Resource)

See Also:



113
114
115
# File 'lib/octokit/client/users.rb', line 113

def update_user(options)
  patch 'user', options
end

#user(user = nil, options = {}) ⇒ Sawyer::Resource

Get a single user

Examples:

Octokit.user("sferik")

Parameters:

  • user (Integer, String) (defaults to: nil)

    GitHub user login or id.

Returns:

  • (Sawyer::Resource)

See Also:



33
34
35
# File 'lib/octokit/client/users.rb', line 33

def user(user = nil, options = {})
  get User.path(user), options
end

#user_keys(user, options = {}) ⇒ Array<Sawyer::Resource>

Get list of public keys for user.

Examples:

@client.user_keys('pengwynn')

Parameters:

  • user (Integer, String)

    GitHub user login or id.

Returns:

  • (Array<Sawyer::Resource>)

    Array of hashes representing public keys.

See Also:



267
268
269
270
# File 'lib/octokit/client/users.rb', line 267

def user_keys(user, options = {})
  # TODO: Roll this into .keys
  paginate "#{User.path user}/keys", options
end

#user_migration_archive_url(id, options = {}) ⇒ Object

Fetches the URL to a migration archive.

Requires authenticated user.

Parameters:

  • id (Integer)

    ID number of the migration.

See Also:



412
413
414
415
416
417
# File 'lib/octokit/client/users.rb', line 412

def user_migration_archive_url(id, options = {})
  url = "user/migrations/#{id}/archive"

  response = client_without_redirects(options).get(url)
  response.headers['location']
end

#user_migration_repositories(id, options = {}) ⇒ Object

List repositories for a user migration.

Requires authenticated user.

Parameters:

  • id (Integer)

    ID number of the migration.

See Also:



435
436
437
# File 'lib/octokit/client/users.rb', line 435

def user_migration_repositories(id, options = {})
  get "user/migrations/#{id}/repositories", options
end

#user_migration_status(id, options = {}) ⇒ Object

Fetches the status of a migration.

Requires authenticated user.

Parameters:

  • id (Integer)

    ID number of the migration.

See Also:



402
403
404
# File 'lib/octokit/client/users.rb', line 402

def user_migration_status(id, options = {})
  get "user/migrations/#{id}", options
end

#user_migrations(options = {}) ⇒ Array<Sawyer::Resource>

Lists the most recent migrations.

Requires authenticated user.

Returns:

  • (Array<Sawyer::Resource>)

    Array of migration resources.

See Also:



392
393
394
# File 'lib/octokit/client/users.rb', line 392

def user_migrations(options = {})
  paginate 'user/migrations', options
end

#validate_credentials(options = {}) ⇒ Boolean

Validate user username and password

Parameters:

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

    User credentials

Options Hash (options):

  • :login (String)

    GitHub login

  • :password (String)

    GitHub password

Returns:

  • (Boolean)

    True if credentials are valid



93
94
95
96
97
# File 'lib/octokit/client/users.rb', line 93

def validate_credentials(options = {})
  !self.class.new(options).user.nil?
rescue Octokit::Unauthorized
  false
end