Class: Rubyku::User

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyku/user.rb

Overview

This class represents a user within the Jaiku system. It has the following attributes:

  • avatar_url: The url for locating the user’s avatar picture.

  • first_name: The user’s given first name.

  • last_name: The user’s given last name.

  • nick: The user’s unique username on Jaiku.

  • jaiku_url: The url where the user’s jaiku account can be found.

  • contacts: A list of Rubyku::User objects representing this user’s contacts.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#avatar_urlObject

Returns the value of attribute avatar_url.



12
13
14
# File 'lib/rubyku/user.rb', line 12

def avatar_url
  @avatar_url
end

#contacts(jaiku_credentials = Rubyku::Settings.jaiku_credentials) ⇒ Object

Returns the list of contacts for the current user. Contacts is the only property of User that is lazy-loaded. This is because if it wasn’t lazy-loaded, you’d be perpetually creating user objects.

returns: Array{Rubyku::User} the users that this user follows on Jaiku.



27
28
29
30
31
32
33
34
35
# File 'lib/rubyku/user.rb', line 27

def contacts(jaiku_credentials=Rubyku::Settings.jaiku_credentials)
  log = Rubyku::Settings.logger
  unless @contacts
    log.info("contacts for user #{@nick} requested - attempting to fetch.")
    @contacts = Rubyku::User.fetch(@nick, jaiku_credentials).contacts
  end
  
  @contacts
end

#first_nameObject

Returns the value of attribute first_name.



13
14
15
# File 'lib/rubyku/user.rb', line 13

def first_name
  @first_name
end

#jaiku_urlObject

Returns the value of attribute jaiku_url.



16
17
18
# File 'lib/rubyku/user.rb', line 16

def jaiku_url
  @jaiku_url
end

#last_nameObject

Returns the value of attribute last_name.



14
15
16
# File 'lib/rubyku/user.rb', line 14

def last_name
  @last_name
end

#nickObject

Returns the value of attribute nick.



15
16
17
# File 'lib/rubyku/user.rb', line 15

def nick
  @nick
end

Class Method Details

.build_user_from_json(json) ⇒ Object

Given a hash representing the JSON data for a user from Jaiku, this method maps those JSON properties to a Rubyku::User object and returns it. Contacts are loaded if present.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rubyku/user.rb', line 84

def build_user_from_json(json)
  user = Rubyku::User.new
  user.nick= json['nick']
  user.first_name= json['first_name']
  user.last_name= json['last_name']
  user.jaiku_url = json['url']
  user.avatar_url = json['avatar']
  user.contacts = json['contacts'] ? json['contacts'].map { |u| build_user_from_json(u)} : nil
  
  user
end

.fetch(username, jaiku_credentials = Rubyku::Settings.jaiku_credentials) ⇒ Object

Retrieves a user’s information from Jaiku.

returns: Rubyku::User the requested user.



72
73
74
75
76
# File 'lib/rubyku/user.rb', line 72

def fetch(username,jaiku_credentials=Rubyku::Settings.jaiku_credentials)  
  req_url = "http://%s.jaiku.com/json" % username
  json = Rubyku::Request.retrieve_json_object(req_url, jaiku_credentials)
  build_user_from_json(json)
end

Instance Method Details

#stream(jaiku_credentials = Rubyku::Settings.jaiku_credentials) ⇒ Object

Returns the stream for this user (updates from their contacts, as they see on the home page). This method will hit the server everytime, so cache the results if you need to.

returns: Array{Rubyku::Message} the messages in this user’s stream.



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

def stream(jaiku_credentials=Rubyku::Settings.jaiku_credentials)
  log = Rubyku::Settings.logger
  log.info("Requesting stream for user #{@nick}")
  url = "http://%s.jaiku.com/contacts/feed/json" % @nick
  stream_json = Rubyku::Request.retrieve_json_object(url, jaiku_credentials)
  stream_json['stream'].map { |m| Rubyku::Message.build_message_from_json(m) }
end

#updates(jaiku_credentials = Rubyku::Settings.jaiku_credentials) ⇒ Object

Returns the user’s most recent updates. This method will hit the server every time, so cache the results if you need to.

returns: Array{Rubyku::Message} this user’s most recent updates



43
44
45
46
47
48
49
# File 'lib/rubyku/user.rb', line 43

def updates(jaiku_credentials=Rubyku::Settings.jaiku_credentials)
  log = Rubyku::Settings.logger
  log.info("Requesting updates from user #{@nick}")
  url = "http://%s.jaiku.com/feed/json" % @nick
  updates_json = Rubyku::Request.retrieve_json_object(url, jaiku_credentials)
  updates_json['stream'].map { |m| Rubyku::Message.build_message_from_json(m) }
end