Class: USaidWat::Thing::User

Inherits:
Object
  • Object
show all
Defined in:
lib/usaidwat/thing.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, user_data, comment_data, post_data) ⇒ User

Returns a new instance of User.



18
19
20
21
22
23
# File 'lib/usaidwat/thing.rb', line 18

def initialize(username, user_data, comment_data, post_data)
  @username = username
  @user_data = user_data
  @comment_data = comment_data
  @post_data = post_data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/usaidwat/thing.rb', line 39

def method_missing(symbol, *args, &block)
  if symbol.to_s =~ /_data$/
    begin
      res = instance_variable_get("@#{symbol}")
      raise USaidWat::Client::NoSuchUserError, @username if res == :no_such_user
      raise USaidWat::Client::ReachabilityError if res == :server_error
      res['data']
    rescue NameError
      super
    end
  else
    super
  end
end

Instance Method Details

#aboutObject



25
26
27
28
29
# File 'lib/usaidwat/thing.rb', line 25

def about
  raise USaidWat::Client::NoSuchUserError, @username if @user_data == :no_such_user
  raise USaidWat::Client::ReachabilityError if @user_data == :server_error
  @about ||= About.new(@user_data)
end

#comments(n) ⇒ Object



31
32
33
# File 'lib/usaidwat/thing.rb', line 31

def comments(n)
  comment_data['children'].map { |d| Comment.new(d) }
end

#postsObject



35
36
37
# File 'lib/usaidwat/thing.rb', line 35

def posts
  post_data['children'].map { |d| Submission.new(d) }
end