Class: Forrst::User

Inherits:
Object
  • Object
show all
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

Author:

  • Yorick Peterse

Since:

  • 0.1a

Constant Summary collapse

InfoURL =

URL relative to Forrst::URL for retrieving user information.

Author:

  • Yorick Peterse

Since:

  • 0.1a

'/users/info'
PostsURL =

URL relative to Forrst::URL for retrieving the posts of a user.

Author:

  • Yorick Peterse

Since:

  • 0.1a

'/users/posts'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • response (String/Hash)

    A string containing a JSON response sent back by the

Author:

  • Yorick Peterse

Since:

  • 0.1a



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

#bioObject (readonly)

The description of the user.

Author:

  • Yorick Peterse

Since:

  • 0.1a



115
116
117
# File 'lib/forrst/user.rb', line 115

def bio
  @bio
end

#homepageObject (readonly)

The URL to the user’s website.

Author:

  • Yorick Peterse

Since:

  • 0.1a



152
153
154
# File 'lib/forrst/user.rb', line 152

def homepage
  @homepage
end

#listedObject (readonly)

A boolean that indicates if the user is listed in the Forrst.me directory or not.

Author:

  • Yorick Peterse

Since:

  • 0.1a



160
161
162
# File 'lib/forrst/user.rb', line 160

def listed
  @listed
end

#nameObject (readonly)

The real name of the user.

Author:

  • Yorick Peterse

Since:

  • 0.1a



90
91
92
# File 'lib/forrst/user.rb', line 90

def name
  @name
end

#photosObject (readonly)

A hash containing all the photos of the user. All the keys of this hash are symbols just like the statistics hash.

Author:

  • Yorick Peterse

Since:

  • 0.1a



132
133
134
# File 'lib/forrst/user.rb', line 132

def photos
  @photos
end

#statisticsObject (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.

Author:

  • Yorick Peterse

Since:

  • 0.1a



107
108
109
# File 'lib/forrst/user.rb', line 107

def statistics
  @statistics
end

#tagsObject (readonly)

An array of tags for the user.

Author:

  • Yorick Peterse

Since:

  • 0.1a



168
169
170
# File 'lib/forrst/user.rb', line 168

def tags
  @tags
end

#twitterObject (readonly)

The Twitter username of the user.

Author:

  • Yorick Peterse

Since:

  • 0.1a



123
124
125
# File 'lib/forrst/user.rb', line 123

def twitter
  @twitter
end

#typeObject (readonly)

An array containing the types of the user. Can be any of the following:

  • ‘developer’
  • ‘designer’
  • ‘developer’, ‘designer’

Author:

  • Yorick Peterse

Since:

  • 0.1a



144
145
146
# File 'lib/forrst/user.rb', line 144

def type
  @type
end

#urlObject (readonly)

The URL to the Forrst profile of the user.

Author:

  • Yorick Peterse

Since:

  • 0.1a



98
99
100
# File 'lib/forrst/user.rb', line 98

def url
  @url
end

#user_idObject (readonly)

The user’s ID.

Author:

  • Yorick Peterse

Since:

  • 0.1a



74
75
76
# File 'lib/forrst/user.rb', line 74

def user_id
  @user_id
end

#usernameObject (readonly)

The username as used on Forrst.

Author:

  • Yorick Peterse

Since:

  • 0.1a



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.

Examples:

Forrst::User['yorickpeterse']
Forrst::User[6998]

Parameters:

  • options (String/Fixnum)

Returns:

Author:

  • Yorick Peterse

Since:

  • 0.1a



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/forrst/user.rb', line 183

def self.[](options)
  if options.class == String
    options = {:username => options}
  elsif options.class == Fixnum
    options = {:id => options.to_s}
  else
    raise(TypeError, "Expected Hash or Fixnum but got #{options.class} instead")
  end

  response = Forrst.oauth.request(:get, InfoURL, options)

  return User.new(response)
end

Instance Method Details

#designer?TrueClass/FalseClass

Checks if the user is a designer or not.

Returns:

  • (TrueClass/FalseClass)

Author:

  • Yorick Peterse

Since:

  • 0.1



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.

Returns:

  • (TrueClass/FalseClass)

Author:

  • Yorick Peterse

Since:

  • 0.1



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.

Returns:

  • (TrueClass/FalseClass)

Author:

  • Yorick Peterse

Since:

  • 0.1



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.

Parameters:

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

    A hash with additional options to use when retrieving all the

Options Hash (options):

  • :type (String/Symbol)

    The type of posts to retrieve such as :code or

  • :limit (Fixnum)

    The amount of posts to retrieve.

  • :after (After)

    An ID offset used for retrieving a set of posts

Returns:

  • (Array)

Author:

  • Yorick Peterse

Since:

  • 0.1a



267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/forrst/user.rb', line 267

def posts(options = {})
  options  = {
    :username => @username
  }.merge(options.subset(:limit, :type, :after))

  response = Forrst.oauth.request(:get, PostsURL, options)
  response = JSON.load(response)

  return response['resp'].map do |post|
    Forrst::Post.new(post)
  end
end