Class: Omega::Client
Instance Method Summary collapse
- #add_user_to_contest(user, contest) ⇒ Object
- #contest(name) ⇒ Object
-
#initialize(conf) ⇒ Client
constructor
A new instance of Client.
- #login ⇒ Object
- #open_contest(name) ⇒ Object
- #perform_request(method, endpoint, data) ⇒ Object
- #post(endpoint, data) ⇒ Object
- #problems_solved(user) ⇒ Object
- #scoreboard(name) ⇒ Object
- #user(user) ⇒ Object
Constructor Details
#initialize(conf) ⇒ Client
Returns a new instance of Client.
25 26 27 |
# File 'lib/omega/client.rb', line 25 def initialize(conf) @config = conf end |
Instance Method Details
#add_user_to_contest(user, contest) ⇒ Object
74 75 76 |
# File 'lib/omega/client.rb', line 74 def add_user_to_contest(user, contest) post('/api/contest/addUser', { contest_alias: contest, usernameOrEmail: user }) end |
#contest(name) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/omega/client.rb', line 54 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 |
#login ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/omega/client.rb', line 42 def login data = post('/api/user/login', usernameOrEmail: @config['user'], password: @config['pass']) @token = data[:auth_token] self.class..('ouat' => data[:auth_token]) end |
#open_contest(name) ⇒ Object
50 51 52 |
# File 'lib/omega/client.rb', line 50 def open_contest(name) post('/api/contest/open/', { contest_alias: name }) end |
#perform_request(method, endpoint, data) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/omega/client.rb', line 29 def perform_request(method, endpoint, data) url = "#{@config['endpoint']}#{endpoint}" response = self.class.send(method, url, body: data) body = JSON.parse(response.body, symbolize_names: true) raise OmegaError, body if body[:error] body end |
#post(endpoint, data) ⇒ Object
38 39 40 |
# File 'lib/omega/client.rb', line 38 def post(endpoint, data) perform_request(:post, endpoint, data) end |
#problems_solved(user) ⇒ Object
78 79 80 |
# File 'lib/omega/client.rb', line 78 def problems_solved(user) post('/api/user/problemsSolved/', { username: user }) end |
#scoreboard(name) ⇒ Object
64 65 66 67 |
# File 'lib/omega/client.rb', line 64 def scoreboard(name) data = post('/api/contest/scoreboard/', { contest_alias: name }) Scoreboard.new(self, data) end |