Module: FoldingAtHomeClient::Users
- Extended by:
- Request
- Defined in:
- lib/folding_at_home_client/users.rb
Constant Summary
collapse
- DAILY_FILE =
'daily_user_summary.txt'
Constants included
from Request
Request::API_URL, Request::HEADERS
Class Method Summary
collapse
Methods included from Request
connection, format_response, request, request_and_instantiate_objects, request_unencoded
Class Method Details
.count ⇒ Object
12
13
14
15
16
|
# File 'lib/folding_at_home_client/users.rb', line 12
def self.count
endpoint = '/user-count'
request(endpoint:).first
end
|
.daily(params = {}) ⇒ Object
49
50
51
52
53
54
55
56
57
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/folding_at_home_client/users.rb', line 49
def self.daily(params = {})
filepath = params[:filepath] || DAILY_FILE
update_daily_file(filepath:)
= 2
position = params[:position]&.to_i || false
unless position
page = params[:page]&.to_i
per_page = params[:per_page]&.to_i
paginating = !page.nil? && !per_page.nil?
skip_line_index = paginating ? ((page - 1) * per_page) : false
unless paginating
limit = params[:limit]&.to_i || 10
name = params[:name] || false
team_id = params[:team_id]&.to_i || false
end
end
keys = []
users = []
File.foreach(filepath).each_with_index do |line, index|
next if index.zero?
line_array = line_to_array(line)
if index == 1
keys = line_array.map(&:to_sym)
next
elsif paginating && (index < (skip_line_index + ))
next
end
user_hash = Hash[keys.zip(line_array)]
if position
next unless (position + ) == index
return User.new(**user_hash)
elsif name || team_id
name_match = name ? name == user_hash[:name] : true
team_match = team_id ? team_id == user_hash[:team].to_i : true
users.push(User.new(**user_hash)) if name_match && team_match
else
users.push(User.new(**user_hash))
break if paginating && users.length >= per_page
end
break if users.length >= limit
end
sort_by = params[:sort_by] || false
users.sort_by! { |user| user.send(sort_by) } if sort_by
order = params[:order] || false
users.reverse! if order && order.to_s.downcase == 'desc'
users
end
|
.line_to_array(line) ⇒ Object
116
117
118
|
# File 'lib/folding_at_home_client/users.rb', line 116
def self.line_to_array(line)
line.chomp.split("\t")
end
|
.top(month: nil, year: nil) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/folding_at_home_client/users.rb', line 18
def self.top(month: nil, year: nil)
endpoint = '/user'
params = {}
if month && year
endpoint += '/monthly'
params = {
month:,
year:,
}
end
request_and_instantiate_objects(
endpoint:,
params:,
object_class: User
)
end
|
.update_daily_file(filepath: DAILY_FILE, force: false) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/folding_at_home_client/users.rb', line 37
def self.update_daily_file(filepath: DAILY_FILE, force: false)
last_timestamp = File.open(filepath, &:readline).chomp
time_difference_in_hours = (Time.now.utc - Time.parse(last_timestamp)) / 3600
return unless time_difference_in_hours >= 3.0 || force
IO.copy_stream(
URI.open('https://apps.foldingathome.org/daily_user_summary.txt'),
filepath
)
end
|