Class: FoldingAtHomeClient::User

Inherits:
Object
  • Object
show all
Extended by:
Request
Includes:
Request
Defined in:
lib/folding_at_home_client/user.rb

Constant Summary

Constants included from Request

Request::API_URL, Request::HEADERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

connection, format_response, request, request_and_instantiate_objects, request_unencoded

Constructor Details

#initialize(id: nil, name: nil, work_unit: nil, work_units: nil, wu: nil, active7: nil, active50: nil, credit: nil, last: nil, rank: nil, score: nil, team: nil, teams: nil, users: nil, error: nil) ⇒ User

rubocop:disable Lint/UnusedMethodArgument, Naming/MethodParameterName



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/folding_at_home_client/user.rb', line 23

def initialize(
  id: nil,
  name: nil,
  work_unit: nil,
  work_units: nil,
  wu: nil,
  active7: nil,
  active50: nil,
  credit: nil,
  last: nil,
  rank: nil,
  score: nil,
  team: nil,
  teams: nil,
  users: nil,
  error: nil
)
  @id = id if id
  @name = name if name

  @work_units = work_units&.to_i || wu&.to_i unless work_units.nil? && wu.nil?

  @active7 = active7 if active7
  @active50 = active50 if active50
  @credit = credit if credit
  @last = last if last
  @rank = rank if rank
  @score = score.to_i if score

  teams = [team] if teams.nil? && team

  teams = teams&.map do |team_thing|
    if team_thing.is_a?(Hash)
      Team.new(**team_thing) unless team_thing[:score]&.zero?
    elsif team_thing.is_a?(String)
      Team.new(id: team_thing.to_i)
    else
      team_thing
    end
  end&.compact

  @teams = teams if teams

  @error = error if error
end

Instance Attribute Details

#active50Object (readonly)

Returns the value of attribute active50.



11
12
13
# File 'lib/folding_at_home_client/user.rb', line 11

def active50
  @active50
end

#active7Object (readonly)

Returns the value of attribute active7.



11
12
13
# File 'lib/folding_at_home_client/user.rb', line 11

def active7
  @active7
end

#creditObject (readonly)

Returns the value of attribute credit.



11
12
13
# File 'lib/folding_at_home_client/user.rb', line 11

def credit
  @credit
end

#errorObject (readonly)

Returns the value of attribute error.



11
12
13
# File 'lib/folding_at_home_client/user.rb', line 11

def error
  @error
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/folding_at_home_client/user.rb', line 10

def id
  @id
end

#lastObject (readonly)

Returns the value of attribute last.



11
12
13
# File 'lib/folding_at_home_client/user.rb', line 11

def last
  @last
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/folding_at_home_client/user.rb', line 10

def name
  @name
end

#rankObject (readonly)

Returns the value of attribute rank.



11
12
13
# File 'lib/folding_at_home_client/user.rb', line 11

def rank
  @rank
end

#scoreObject (readonly)

Returns the value of attribute score.



11
12
13
# File 'lib/folding_at_home_client/user.rb', line 11

def score
  @score
end

#teamsObject (readonly)

Returns the value of attribute teams.



11
12
13
# File 'lib/folding_at_home_client/user.rb', line 11

def teams
  @teams
end

#work_unitsObject (readonly)

Returns the value of attribute work_units.



11
12
13
# File 'lib/folding_at_home_client/user.rb', line 11

def work_units
  @work_units
end

#wus_dailyObject (readonly)

Returns the value of attribute wus_daily.



11
12
13
# File 'lib/folding_at_home_client/user.rb', line 11

def wus_daily
  @wus_daily
end

Class Method Details

.find_by(id: nil, name: nil, passkey: nil, team_id: nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/folding_at_home_client/user.rb', line 121

def self.find_by(id: nil, name: nil, passkey: nil, team_id: nil)
  user = allocate

  user.id ||= id if id
  user.name ||= name if name

  endpoint = user_endpoint(id: user.id, name: user.name)

  params = {}
  params[:passkey] = passkey if passkey
  params[:team] = team_id if team_id

  user_hash = nil

  begin
    user_hash = request(endpoint:, params:).first
  rescue JSON::ParserError
    if user.name
      query_endpoint = '/search/user'
      query_params = { query: user.name }

      query_user_hash = request(endpoint: query_endpoint, params: query_params).first
      @id = query_user_hash&.fetch(:id, nil)
      user_hash = request(endpoint: user_endpoint, params:).first if @id
    end
  end

  error = user_hash[:error]

  if error
    user_hash.delete(:status)

    user.send(:initialize, **user_hash)
    return user
  end

  @id = user_hash[:id]
  @name = user_hash[:name]
  @work_units = user_hash[:wus]
  @active7 = user_hash[:active7]
  @active50 = user_hash[:active50]
  @credit = user_hash[:credit]
  @last = user_hash[:last]
  @rank = user_hash[:rank]
  @score = user_hash[:score]

  teams = user_hash[:teams]&.map do |team_hash|
    Team.new(**team_hash) unless team_hash[:score]&.zero?
  end&.compact

  user_hash[:teams] = teams if teams

  user.send(:initialize, **user_hash)

  user
end

Instance Method Details

#bonuses(passkey: nil) ⇒ Object

Raises:

  • (ArgumentError)


199
200
201
202
203
204
205
206
207
208
209
# File 'lib/folding_at_home_client/user.rb', line 199

def bonuses(passkey: nil)
  raise ArgumentError, 'Required: name of user' unless @name

  endpoint = '/bonus'
  params = {
    user: @name,
  }
  params[:passkey] = passkey if passkey

  request(endpoint:, params:)
end

#lookup(passkey: nil, team_id: nil) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument, Naming/MethodParameterName



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/folding_at_home_client/user.rb', line 70

def lookup(passkey: nil, team_id: nil)
  endpoint = user_endpoint(id: @id, name: @name)

  params = {}
  params[:passkey] = passkey if passkey
  params[:team] = team_id if team_id

  user_hash = nil

  begin
    user_hash = request(endpoint:, params:).first
  rescue JSON::ParserError
    if @name
      query_endpoint = '/search/user'
      query_params = { query: @name }

      query_user_hash = request(endpoint: query_endpoint, params: query_params).first
      @id = query_user_hash&.fetch(:id, nil)
      user_hash = request(endpoint: user_endpoint, params:).first if @id
    end
  end

  error = user_hash[:error]

  if error
    @error = error
    return self
  end

  @id = user_hash[:id]
  @name = user_hash[:name]

  @work_units_daily = @work_units if user_hash[:wus] && !@work_units.nil?
  @work_units = user_hash[:wus]

  @active7 = user_hash[:active7]
  @active50 = user_hash[:active50]
  @credit = user_hash[:credit] if user_hash[:credit]
  @last = user_hash[:last]
  @rank = user_hash[:rank]
  @score = user_hash[:score]

  teams = user_hash[:teams]&.map do |team_hash|
    Team.new(**team_hash) unless team_hash[:score]&.zero?
  end&.compact

  @teams = teams if teams

  self
end

#projectsObject



192
193
194
195
196
197
# File 'lib/folding_at_home_client/user.rb', line 192

def projects
  endpoint = user_endpoint(name_required: true)
  endpoint += '/projects'

  request(endpoint:)
end

#teams_lookup(passkey: nil) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/folding_at_home_client/user.rb', line 178

def teams_lookup(passkey: nil)
  endpoint = user_endpoint(name_required: true)
  endpoint += '/teams'

  params = {}
  params[:passkey] = passkey if passkey

  request_and_instantiate_objects(
    endpoint:,
    params:,
    object_class: Team
  )
end