Class: Bgg::User

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_data) ⇒ User

Returns a new instance of User.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bgg/user.rb', line 7

def initialize(user_data)
  @id = user_data['id']
  @first_name = user_data['firstname'][0]['value']
  @last_name = user_data['lastname'][0]['value']
  @name = user_data['name']

  @avatar_link = user_data['avatarlink'][0]['value']
  @country = user_data['country'][0]['value']
  @last_login = user_data['lastlogin'][0]['value']
  @state = user_data['stateorprovince'][0]['value']
  @year_registered = user_data['yearregistered'][0]['value']
end

Instance Attribute Details

Returns the value of attribute avatar_link.



3
4
5
# File 'lib/bgg/user.rb', line 3

def avatar_link
  @avatar_link
end

#countryObject (readonly)

Returns the value of attribute country.



3
4
5
# File 'lib/bgg/user.rb', line 3

def country
  @country
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



3
4
5
# File 'lib/bgg/user.rb', line 3

def first_name
  @first_name
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/bgg/user.rb', line 3

def id
  @id
end

#last_loginObject (readonly)

Returns the value of attribute last_login.



3
4
5
# File 'lib/bgg/user.rb', line 3

def 
  @last_login
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



3
4
5
# File 'lib/bgg/user.rb', line 3

def last_name
  @last_name
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/bgg/user.rb', line 3

def name
  @name
end

#stateObject (readonly)

Returns the value of attribute state.



3
4
5
# File 'lib/bgg/user.rb', line 3

def state
  @state
end

#year_registeredObject (readonly)

Returns the value of attribute year_registered.



3
4
5
# File 'lib/bgg/user.rb', line 3

def year_registered
  @year_registered
end

Class Method Details

.find_by_id(user_id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bgg/user.rb', line 20

def self.find_by_id(user_id)
  user_id = Integer(user_id)
  if user_id < 1
    raise ArgumentError.new('user_id must be greater than zero!')
  end

  user_data = BggApi.user({id: user_id})
  if user_data['id'].empty?
    raise ArgumentError.new('User does not exist')
  end

  Bgg::User.new(user_data)
end

.find_by_name(user_name) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bgg/user.rb', line 34

def self.find_by_name(user_name)
  if user_name !~ /^\w+$/
    raise ArgumentError.new('find_by_name must be passed a string!')
  end

  user_data = BggApi.user({name: user_name})
  if user_data['id'].empty?
    raise ArgumentError.new('User does not exist')
  end

  Bgg::User.new(user_data)
end

Instance Method Details

#collectionObject



52
53
54
# File 'lib/bgg/user.rb', line 52

def collection
  Bgg::Collection.find_by_username(self.name)
end

#play_countObject



47
48
49
50
# File 'lib/bgg/user.rb', line 47

def play_count
  some_plays = BggApi.plays({username: self.name, page: 1})
  some_plays.fetch('total', 0).to_i
end

#playsObject



56
57
58
# File 'lib/bgg/user.rb', line 56

def plays
  Bgg::Plays.find_by_username(self.name)
end