Class: Comet::API

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

Constant Summary collapse

RUBYGEMS_URL =
'https://rubygems.org/api/v1/gems/comet.json'

Class Method Summary collapse

Class Method Details

.download_archive(download_link, dest) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/comet/api.rb', line 34

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_kata(config, id) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/comet/api.rb', line 22

def self.get_kata(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_katas(config) ⇒ Object



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

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

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

.latest_gem_versionObject



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

def self.latest_gem_version
  results = JSON.parse(RestClient.get(RUBYGEMS_URL))
  results['version']
end

.submit(config, kata_id, file_path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/comet/api.rb', line 50

def self.submit(config, kata_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]' => kata_id,
      'submission[file_name]' => file_name,
      'submission[body]' => body
    })

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