Class: Forrst::User
- Inherits:
-
Object
- Object
- Forrst::User
- Defined in:
- lib/forrst/user.rb
Overview
Forrst::User is the class used for retrieving details about a particular user. A user is retrieved by calling Forrst::User[] and passing a username or ID to it:
user = Forrst::User['yorickpeterse'] => #<Forrst::User:0x1234567>
Once an instance has been created you can retrieve various details, such as the username, simply by calling methods on the resulting object:
user.username => 'YorickPeterse'
When retrieving a user you can retrieve the user’s posts by calling Forrst::User#posts as following:
user.posts.each do |post|
puts post.title
end
When retrieving the user’s posts the library will use the class Forrst::Post which allows you to retrieve post related data such as all the comments:
user.posts.each do |post|
post.comments.each do |comment|
puts comment.body
end
end
Note that comments are lazy loaded. This means that for each iteration in the posts array a GET request is fired to the Forrst API. Currently there’s no way to work around this so you’ll have to be careful with using this method.
This class also provides the following shortcut methods for figuring out the type of user:
-
developer?
-
designer?
-
developer_and_designer?
These can be used as following:
if user.developer?
puts "It's a nerd!"
elsif user.designer?
puts "It's a hippie!"
end
Constant Summary collapse
- InfoURL =
URL relative to Forrst::URL for retrieving user information.
'/users/info'- PostsURL =
URL relative to Forrst::URL for retrieving the posts of a user.
'/users/posts'
Instance Attribute Summary collapse
-
#bio ⇒ Object
readonly
The description of the user.
-
#homepage ⇒ Object
readonly
The URL to the user’s website.
-
#listed ⇒ Object
readonly
A boolean that indicates if the user is listed in the Forrst.me directory or not.
-
#name ⇒ Object
readonly
The real name of the user.
-
#photos ⇒ Object
readonly
A hash containing all the photos of the user.
-
#statistics ⇒ Object
readonly
A hash containing various statistics such as the amount of comments, likes, etc.
-
#tags ⇒ Object
readonly
An array of tags for the user.
-
#twitter ⇒ Object
readonly
The Twitter username of the user.
-
#type ⇒ Object
readonly
An array containing the types of the user.
-
#url ⇒ Object
readonly
The URL to the Forrst profile of the user.
-
#user_id ⇒ Object
readonly
The user’s ID.
-
#username ⇒ Object
readonly
The username as used on Forrst.
Class Method Summary collapse
-
.[](options) ⇒ Forrst::User
Retrieves a single user by it’s username or ID and returns a new instance of Forrst::User with these details.
Instance Method Summary collapse
-
#designer? ⇒ TrueClass/FalseClass
Checks if the user is a designer or not.
-
#developer? ⇒ TrueClass/FalseClass
Checks if the user is a developer or not.
-
#developer_and_designer? ⇒ TrueClass/FalseClass
Checks if the user is a developer and a designer.
-
#initialize(response) ⇒ Forrst::User
constructor
Given a JSON response (as a string) this method will parse it using the JSON gem and return a new instance of Forrst::User with all the details set.
-
#posts(options = {}) ⇒ Array
Retrieves all the posts for the current user.
Constructor Details
#initialize(response) ⇒ Forrst::User
Given a JSON response (as a string) this method will parse it using the JSON gem and return a new instance of Forrst::User with all the details set.
Forrst server or an instance of Hash in case the response has already been parsed.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/forrst/user.rb', line 207 def initialize(response) if response.class != Hash response = JSON.load(response) response = response['resp'] end # We're not directly setting Forrst::User#id as that will trigger warnings in both # JRuby and Rubinius. @user_id = response['id'] @username = response['username'] @name = response['name'] @url = response['url'] @homepage = response['homepage_url'] @listed = response['in_directory'] @twitter = response['twitter'] @bio = response['bio'] @statistics = { :comments => response['comments'].to_i, :likes => response['likes'].to_i, :followers => response['followers'].to_i, :following => response['following'].to_i, :posts => response['posts'].to_i } # Photos come as a hash with rather wacky keys so those need to be changed as well. @photos = { :extra_large => response['photos']['xl_url'], :large => response['photos']['large_url'], :medium => response['photos']['medium_url'], :small => response['photos']['small_url'], :thumbnail => response['photos']['thumb_url'] } # Tags aren't always present if response.key?('tag_string') @tags = response['tag_string'].split(',') else @tags = [] end # Last but not least, the user type! @type = response['is_a'].split('&').map { |i| i.strip } end |
Instance Attribute Details
#bio ⇒ Object (readonly)
The description of the user.
115 116 117 |
# File 'lib/forrst/user.rb', line 115 def bio @bio end |
#homepage ⇒ Object (readonly)
The URL to the user’s website.
152 153 154 |
# File 'lib/forrst/user.rb', line 152 def homepage @homepage end |
#listed ⇒ Object (readonly)
A boolean that indicates if the user is listed in the Forrst.me directory or not.
160 161 162 |
# File 'lib/forrst/user.rb', line 160 def listed @listed end |
#name ⇒ Object (readonly)
The real name of the user.
90 91 92 |
# File 'lib/forrst/user.rb', line 90 def name @name end |
#photos ⇒ Object (readonly)
A hash containing all the photos of the user. All the keys of this hash are symbols just like the statistics hash.
132 133 134 |
# File 'lib/forrst/user.rb', line 132 def photos @photos end |
#statistics ⇒ Object (readonly)
A hash containing various statistics such as the amount of comments, likes, etc. Note that the keys of this hash are symbols, not strings.
107 108 109 |
# File 'lib/forrst/user.rb', line 107 def statistics @statistics end |
#tags ⇒ Object (readonly)
An array of tags for the user.
168 169 170 |
# File 'lib/forrst/user.rb', line 168 def @tags end |
#twitter ⇒ Object (readonly)
The Twitter username of the user.
123 124 125 |
# File 'lib/forrst/user.rb', line 123 def twitter @twitter end |
#type ⇒ Object (readonly)
An array containing the types of the user. Can be any of the following:
- ‘developer’
- ‘designer’
- ‘developer’, ‘designer’
144 145 146 |
# File 'lib/forrst/user.rb', line 144 def type @type end |
#url ⇒ Object (readonly)
The URL to the Forrst profile of the user.
98 99 100 |
# File 'lib/forrst/user.rb', line 98 def url @url end |
#user_id ⇒ Object (readonly)
The user’s ID.
74 75 76 |
# File 'lib/forrst/user.rb', line 74 def user_id @user_id end |
#username ⇒ Object (readonly)
The username as used on Forrst.
82 83 84 |
# File 'lib/forrst/user.rb', line 82 def username @username end |
Class Method Details
.[](options) ⇒ Forrst::User
Retrieves a single user by it’s username or ID and returns a new instance of Forrst::User with these details.
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/forrst/user.rb', line 183 def self.[]() if .class == String = {:username => } elsif .class == Fixnum = {:id => .to_s} else raise(TypeError, "Expected Hash or Fixnum but got #{.class} instead") end response = Forrst.oauth.request(:get, InfoURL, ) return User.new(response) end |
Instance Method Details
#designer? ⇒ TrueClass/FalseClass
Checks if the user is a designer or not.
298 299 300 |
# File 'lib/forrst/user.rb', line 298 def designer? return @type === ['designer'] end |
#developer? ⇒ TrueClass/FalseClass
Checks if the user is a developer or not.
287 288 289 |
# File 'lib/forrst/user.rb', line 287 def developer? return @type === ['developer'] end |
#developer_and_designer? ⇒ TrueClass/FalseClass
Checks if the user is a developer and a designer.
309 310 311 312 313 314 315 316 317 |
# File 'lib/forrst/user.rb', line 309 def developer_and_designer? # The shorthand "return A and B" generates a void error so we'll have to do it this # way. if @type.include?('designer') and @type.include?('developer') return true else return false end end |
#posts(options = {}) ⇒ Array
Retrieves all the posts for the current user. Each post is an instance of Forrst::Post.
posts. :question. after the given ID.
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/forrst/user.rb', line 267 def posts( = {}) = { :username => @username }.merge(.subset(:limit, :type, :after)) response = Forrst.oauth.request(:get, PostsURL, ) response = JSON.load(response) return response['resp'].map do |post| Forrst::Post.new(post) end end |