Class: Omega::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/omega/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ Client

Returns a new instance of Client.



40
41
42
# File 'lib/omega/client.rb', line 40

def initialize(conf)
  @config = conf
end

Instance Method Details

#add_admin_group(contest, group) ⇒ Object



155
156
157
158
159
# File 'lib/omega/client.rb', line 155

def add_admin_group(contest, group)
  post('/api/contest/addGroupAdmin', { contest_alias: contest, group: })
rescue OmegaError
  # Omega seems to have a bug
end

#add_problem_to_contest(contest, problem, points = 100) ⇒ Object



178
179
180
# File 'lib/omega/client.rb', line 178

def add_problem_to_contest(contest, problem, points = 100)
  post('/api/contest/addProblem', contest_alias: contest, problem_alias: problem, points:)
end

#add_user_to_contest(user, contest) ⇒ Object



161
162
163
# File 'lib/omega/client.rb', line 161

def add_user_to_contest(user, contest)
  post('/api/contest/addUser', { contest_alias: contest, usernameOrEmail: user })
end

#add_user_to_group(group_name, username) ⇒ Object



182
183
184
185
186
# File 'lib/omega/client.rb', line 182

def add_user_to_group(group_name, username)
  post('/api/group/addUser', group_alias: group_name, usernameOrEmail: username)
rescue OmegaError => e
  raise if e.errorname != 'identityInGroup'
end

#clarifications(name) ⇒ Object



141
142
143
144
# File 'lib/omega/client.rb', line 141

def clarifications(name)
  data = post('/api/contest/clarifications/', { contest_alias: name })
  data[:clarifications]
end

#contest(name) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/omega/client.rb', line 74

def contest(name)
  data = post('/api/contest/details/', { contest_alias: name })
  Contest.new(self, data)
rescue OmegaError => e
  raise unless e.data[:errorname] == 'userNotAllowed'

  open_contest(name)
  retry
end

#contest_runs(contest, offset, page_size) ⇒ Object



173
174
175
176
# File 'lib/omega/client.rb', line 173

def contest_runs(contest, offset, page_size)
  data = post('/api/contest/runs/', { contest_alias: contest, offset:, rowcount: page_size })
  data[:runs].map { |run| ContestRun.new(self, run) }
end

#create_contest(name:, short_name:, description:, start_time:, finish_time:, public: false, penalty_calc_policy: 'sum', show_penalty: true, points_decay_factor: '0', submissions_gap: '60', languages: ALL_LANGS, feedback: 'none', penalty: '0', scoreboard: '100', penalty_type: 'none', default_show_all_contestants_in_scoreboard: false, show_scoreboard_after: true, score_mode: 'partial', needs_basic_information: false, requests_user_information: 'no', contest_for_teams: false) ⇒ Object



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/omega/client.rb', line 92

def create_contest(name:, short_name:, description:, start_time:, finish_time:,
                   public: false, penalty_calc_policy: 'sum', show_penalty: true,
                   points_decay_factor: '0', submissions_gap: '60', languages: ALL_LANGS, feedback: 'none',
                   penalty: '0', scoreboard: '100', penalty_type: 'none',
                   default_show_all_contestants_in_scoreboard:  false,
                   show_scoreboard_after: true,
                   score_mode:  'partial',
                   needs_basic_information: false,
                   requests_user_information: 'no',
                   contest_for_teams: false)
  post('/api/contest/create/', {
         admin: true,
         admission_mode:  (public ? 'public' : 'private'),
         alias: short_name,
         archived:  false,
         opened:  false,
         penalty_calc_policy:,
         show_penalty:,
         title: name,
         description:,
         has_submissions: false,
         start_time: start_time.to_time.to_i,
         finish_time: finish_time.to_time.to_i,
         points_decay_factor:,
         submissions_gap:,
         languages:,
         feedback:,
         penalty:,
         scoreboard:,
         penalty_type:,
         default_show_all_contestants_in_scoreboard:,
         show_scoreboard_after:,
         score_mode:,
         needs_basic_information:,
         requests_user_information:,
         contest_for_teams:
       })
  contest(short_name)
rescue OmegaError => e
  raise e unless e.errorname == 'aliasInUse'

  contest(short_name)
end

#create_group(name, description: nil, alias: nil) ⇒ Object



88
89
90
# File 'lib/omega/client.rb', line 88

def create_group(name, description: nil, alias: nil)
  post('/api/group/create', name:, alias:, description:)
end

#get_payload_from_response_by_id(response_body, id) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/omega/client.rb', line 188

def get_payload_from_response_by_id(response_body, id)
  page = Nokogiri::HTML(response_body)
  script = page.at_css("script##{id}")
  unless script
    raise OmegaError.new(errorname: 'ScrapingError', errorcode: 404,
                         error: "Script with id #{id} not found")
  end

  script_content = script.content
  # Extrayendo el contenido JSON del script
  begin
    ::JSON.parse(script_content, symbolize_names: true)
  rescue JSON::ParserError
    raise OmegaError.new(errorname: 'ScrapingError', errorcode: 500, error: 'Unable to parse JSON data')
  end
end

#loginObject



62
63
64
65
66
67
68
# File 'lib/omega/client.rb', line 62

def 
  data = post('/api/user/login',
              usernameOrEmail: @config['user'],
              password: @config['pass'])
  @token = data[:auth_token]
  self.class.default_cookies.add_cookies('ouat' => data[:auth_token])
end

#open_contest(name) ⇒ Object



70
71
72
# File 'lib/omega/client.rb', line 70

def open_contest(name)
  post('/api/contest/open/', { contest_alias: name })
end

#perform_request(method, endpoint, data, retried = false) ⇒ Object

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/omega/client.rb', line 44

def perform_request(method, endpoint, data, retried = false)
  url = "#{@config['endpoint']}#{endpoint}"
  response = self.class.send(method, url, body: data)
  body = ::JSON.parse(response.body, symbolize_names: true)

  if body[:errorcode] == 401 && !retried
    
    return perform_request(method, endpoint, data, true)
  end
  raise OmegaError, body if body[:error]

  body
end

#post(endpoint, data) ⇒ Object



58
59
60
# File 'lib/omega/client.rb', line 58

def post(endpoint, data)
  perform_request(:post, endpoint, data)
end

#problem_details(problem_alias) ⇒ Object



84
85
86
# File 'lib/omega/client.rb', line 84

def problem_details(problem_alias)
  post('/api/problem/details', { problem_alias: })
end

#problems_solved(user) ⇒ Object



165
166
167
# File 'lib/omega/client.rb', line 165

def problems_solved(user)
  post('/api/user/problemsSolved/', { username: user })
end

#respond_clarif(id, response) ⇒ Object



146
147
148
# File 'lib/omega/client.rb', line 146

def respond_clarif(id, response)
  post('/api/clarification/update/', { clarification_id: id, answer: response })
end

#run_details(run) ⇒ Object



169
170
171
# File 'lib/omega/client.rb', line 169

def run_details(run)
  post('/api/run/details/', { run_alias: run })
end

#scoreboard(name) ⇒ Object



136
137
138
139
# File 'lib/omega/client.rb', line 136

def scoreboard(name)
  data = post('/api/contest/scoreboard/', { contest_alias: name })
  Scoreboard.new(self, data)
end

#scrap_get(endpoint, id = 'payload') ⇒ Object

Raises:



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/omega/client.rb', line 205

def scrap_get(endpoint, id = 'payload')
  url = "#{@config['endpoint']}#{endpoint}"
  response = self.class.get(url)
  unless response.success?
    raise OmegaError.new(errorname: 'HttpError', errorcode: response.code,
                         error: response.message)
  end

  body = get_payload_from_response_by_id(response.body, id)

  raise OmegaError, body if body[:error]

  body
end

#user(user) ⇒ Object



150
151
152
153
# File 'lib/omega/client.rb', line 150

def user(user)
  data = post('/api/user/profile/', { username: user })
  User.new(self, data)
end