Class: Comet::API

Inherits:
Object
  • Object
show all
Defined in:
lib/comet/api.rb

Class Method Summary collapse

Class Method Details

.download_archive(download_link, dest) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/comet/api.rb', line 26

def self.download_archive(download_link, dest)
  uri = URI(download_link)

  Net::HTTP.start(uri.host, uri.port,
    use_ssl: uri.scheme == 'https') do |http|

    resp = http.get(uri.path)

    open(dest, 'wb') do |file|
      file.write(resp.body)
    end
  end

  dest
end

.get_challenge(config, id) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/comet/api.rb', line 14

def self.get_challenge(config, id)
  response = request_with_token("#{config['server']}/api/v1/challenges/#{id}.json",
    config['token'])

  if response.code == '200'
    results = JSON.parse(response.body, symbolize_names: true)
    results[:challenge]
  else
    nil
  end
end

.get_challenges(config) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/comet/api.rb', line 6

def self.get_challenges(config)
  response = request_with_token("#{config['server']}/api/v1/challenges.json",
    config['token'])

  results = JSON.parse(response.body, symbolize_names: true)
  results[:challenges]
end

.submit(config, challenge_id, file_path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/comet/api.rb', line 42

def self.submit(config, challenge_id, file_path)
  uri = URI("#{config['server']}/api/v1/submissions.json")
  req = Net::HTTP::Post.new(uri)

  file_name = File.basename(file_path)
  body = File.read(file_path)

  req['Authorization'] = "Token #{config['token']}"

  req.set_form_data({
      'submission[challenge_id]' => challenge_id,
      'submission[file_name]' => file_name,
      'submission[body]' => body
    })

  res = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(req)
  end
end