Class: OctocatHerder::User

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/octocat_herder/user.rb

Overview

Interface to the GitHub v3 API for interacting with users.

Currently, this only supports retrieval of information about the requested user.

Instance Attribute Summary

Attributes included from Base

#connection, #raw

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#available_attributes, #initialize, #method_missing

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OctocatHerder::Base

Class Method Details

.fetch(user_name, conn = OctocatHerder::Connection.new) ⇒ OctocatHerder::User

Find a user by login name

Parameters:

  • user_name (String)

    The login name of the desired user.

  • conn (OctocatHerder::Connection) (defaults to: OctocatHerder::Connection.new)

    Defaults to unauthenticated requests.

Returns:

Since:

  • 0.0.1



24
25
26
27
28
# File 'lib/octocat_herder/user.rb', line 24

def self.fetch(user_name, conn = OctocatHerder::Connection.new)
  user = conn.get("/users/#{CGI.escape(user_name)}")

  new(user, conn)
end

.following?(user, connection) ⇒ true, false

Check if the user authenticated by the provided Connection is following the specified user.

Parameters:

Returns:

  • (true, false)

Raises:

  • (ArgumentError)

    If user is not a String or an OctocatHerder::User

  • (ArgumentError)

    If the connection will not make authenticated requests.

Since:

  • 0.1.0



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/octocat_herder/user.rb', line 109

def self.following?(user, connection)
  raise ArgumentError.new(
    "Provided user must be a String, or an OctocatHerder::User."
  ) unless user.is_a?(String) or user.is_a?(OctocatHerder::User)

  raise ArgumentError.new(
    "Provided connection must make authenticated requests."
  ) unless connection.authenticated_requests?

  user_name = user.is_a?(OctocatHerder::User) ? user. : user

  result = connection.raw_get("/user/following/#{CGI.escape(user_name)}")

  # The GitHub API gives us back a "204 No Content" if we are
  # following the user.
  result.response.code == "204"
end

Instance Method Details

#followersArray<OctocatHerder::User>

List of users following this user.

Returns:

Since:

  • 0.1.0



69
70
71
72
73
74
75
76
77
# File 'lib/octocat_herder/user.rb', line 69

def followers
  result = connection.get(
    "/users/#{CGI.escape()}/followers"
  )

  result.map do |follower|
    self.class.new(follower, connection)
  end
end

#followingArray<OctocatHerder::User>

List of users this user is following.

Returns:

Since:

  • 0.1.0



83
84
85
86
87
88
89
90
91
# File 'lib/octocat_herder/user.rb', line 83

def following
  users = connection.get(
    "/users/#{CGI.escape()}/following"
  )

  users.map do |stalkee|
    self.class.new(stalkee, connection)
  end
end

#repositoriesArray<OctocatHerder::Repository>

Note:

This is cached locally to the OctocatHerder::User instance, but at least one API request will be made to populate it initially.

All repositories owned by the user.

Returns:

Since:

  • 0.0.1



38
39
40
# File 'lib/octocat_herder/user.rb', line 38

def repositories
  @repositories ||= OctocatHerder::Repository.list_all(, user_type, connection)
end

#user_idInteger

The ID number of the user.

Returns:

  • (Integer)

Since:

  • 0.0.1



46
47
48
49
50
51
# File 'lib/octocat_herder/user.rb', line 46

def user_id
  # The user id can't be handled by the method_missing magic from
  # OctocatHerder::Base, since the id method returns the object
  # id.
  @raw['id']
end

#user_typeString

The type of account. Typically one of ‘User’, or ‘Organization’.

Returns:

  • (String)

Since:

  • 0.0.1



58
59
60
61
62
63
# File 'lib/octocat_herder/user.rb', line 58

def user_type
  # The user type can't be handled by the method_missing magic
  # from OctocatHerder::Base, since 'type' is the deprecated form
  # of the method 'class'.
  @raw['type']
end