Class: ET::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ API

Returns a new instance of API.



10
11
12
13
14
# File 'lib/et/api.rb', line 10

def initialize(options)
  @host = options[:host]
  @username = options[:username]
  @token = options[:token]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/et/api.rb', line 8

def host
  @host
end

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/et/api.rb', line 8

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



8
9
10
# File 'lib/et/api.rb', line 8

def username
  @username
end

Instance Method Details

#download_file(url) ⇒ Object



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

def download_file(url)
  uri = URI(url)
  dest = random_filename

  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_lesson(slug) ⇒ Object



21
22
23
24
25
# File 'lib/et/api.rb', line 21

def get_lesson(slug)
  response = Net::HTTP.get(lesson_url(slug))
  body = JSON.parse(response, symbolize_names: true)
  body[:lesson]
end

#list_lessonsObject



16
17
18
19
# File 'lib/et/api.rb', line 16

def list_lessons
  response = Net::HTTP.get(lessons_url)
  JSON.parse(response, symbolize_names: true)[:lessons]
end

#submit_lesson(lesson) ⇒ Object



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

def submit_lesson(lesson)
  submission_file = lesson.archive!
  url = submission_url(lesson.slug)

  File.open(submission_file) do |f|
    request = Net::HTTP::Post::Multipart.new(url.path,
      "submission[archive]" => UploadIO.new(f, "application/x-tar", "archive.tar.gz"))
    request["Authorization"] = auth_header

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

      http.request(request)
    end
  end
end