Class: FoldingAtHomeClient::Team

Inherits:
Object
  • Object
show all
Extended by:
Request
Includes:
Request
Defined in:
lib/folding_at_home_client/team.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, team: nil, name: nil, wus: nil, rank: nil, trank: nil, credit: nil, tscore: nil, twus: nil, founder: nil, score: nil, url: nil, logo: nil, last: nil, active_7: nil, active_50: nil, error: nil) ⇒ Team

rubocop:disable Lint/UnusedMethodArgument



20
21
22
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
# File 'lib/folding_at_home_client/team.rb', line 20

def initialize(
  id: nil,
  team: nil,
  name: nil,
  wus: nil,
  rank: nil,
  trank: nil,
  credit: nil,
  tscore: nil,
  twus: nil,
  founder: nil,
  score: nil,
  url: nil,
  logo: nil,
  last: nil,
  active_7: nil,
  active_50: nil,
  error: nil
)
  @id = id || team.to_i if id || team
  @name = name if name

  @rank = trank || rank if trank || rank

  @score = tscore || credit if tscore || credit
  @wus = twus || wus if twus || wus

  @founder = founder if founder
  @url = url if url
  @logo =  if 

  @user_score = score if !tscore.nil? && score
  @user_wus = wus if !twus.nil? && wus

  @error = error if error
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



9
10
11
# File 'lib/folding_at_home_client/team.rb', line 9

def error
  @error
end

#founderObject (readonly)

Returns the value of attribute founder.



9
10
11
# File 'lib/folding_at_home_client/team.rb', line 9

def founder
  @founder
end

#idObject

Returns the value of attribute id.



8
9
10
# File 'lib/folding_at_home_client/team.rb', line 8

def id
  @id
end

#logoObject (readonly)

Returns the value of attribute logo.



9
10
11
# File 'lib/folding_at_home_client/team.rb', line 9

def 
  @logo
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/folding_at_home_client/team.rb', line 8

def name
  @name
end

#rankObject (readonly)

Returns the value of attribute rank.



9
10
11
# File 'lib/folding_at_home_client/team.rb', line 9

def rank
  @rank
end

#scoreObject (readonly)

Returns the value of attribute score.



9
10
11
# File 'lib/folding_at_home_client/team.rb', line 9

def score
  @score
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/folding_at_home_client/team.rb', line 9

def url
  @url
end

#user_scoreObject (readonly)

Returns the value of attribute user_score.



9
10
11
# File 'lib/folding_at_home_client/team.rb', line 9

def user_score
  @user_score
end

#user_wusObject (readonly)

Returns the value of attribute user_wus.



9
10
11
# File 'lib/folding_at_home_client/team.rb', line 9

def user_wus
  @user_wus
end

#wusObject (readonly)

Returns the value of attribute wus.



9
10
11
# File 'lib/folding_at_home_client/team.rb', line 9

def wus
  @wus
end

Class Method Details

.escape_param(name) ⇒ Object



112
113
114
# File 'lib/folding_at_home_client/team.rb', line 112

def self.escape_param(name)
  name.gsub(' ', '%20').gsub('&', '%26').gsub(',', '%2C')
end

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

rubocop:enable Lint/UnusedMethodArgument



58
59
60
61
62
63
64
65
66
67
68
69
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
# File 'lib/folding_at_home_client/team.rb', line 58

def self.find_by(id: nil, name: nil)
  team = allocate

  team.id ||= id if id
  team.name ||= name if name

  endpoint_and_params = '/team'

  if team.id && !team.id.to_s.empty?
    endpoint_and_params += "/#{team.id}"
  elsif team.name && !team.name.empty?
    endpoint_and_params += "/find?name=#{escape_param(team.name)}"
  else
    raise ArgumentError, 'Required: id or name of team'
  end

  team_hash = nil

  begin
    team_hash = request_unencoded(endpoint_and_params:).first
    raise StandardError if team_hash[:error]
  rescue StandardError
    if team.name
      query_endpoint_and_params = "/team?q=#{escape_param(team.name)}"
      query_team_hash = request_unencoded(endpoint_and_params: query_endpoint_and_params).first

      team.id = query_team_hash&.fetch(:id, nil)
      endpoint = "/#{team.id}"

      team_hash = request(endpoint:).first if team.id
    end
  end

  error = team_hash[:error]
  team_hash.delete(:status) if error

  team.send(:initialize, **team_hash)

  team
end

Instance Method Details

#membersObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/folding_at_home_client/team.rb', line 99

def members
  endpoint = "/team/#{@id}/members"

  members_array = request(endpoint:)
  keys = members_array.shift.map(&:to_sym)

  members_array.map do |member_array|
    member_hash = Hash[keys.zip(member_array)]

    User.new(**member_hash)
  end
end