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.



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

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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#download_file(url) ⇒ Object



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

def download_file(url)
  response = nil
  dest = random_filename

  download_client(url).open do |client|
    response = client.get(URI(url).path)
  end
  if response.status == 200
    open(dest, 'wb') do |file|
      file.write(response.body)
    end
    dest
  else
    nil
  end
end

#get_lesson(slug) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/et/api.rb', line 25

def get_lesson(slug)
  resp = nil
  api_client.open do |client|
    resp = client.get(lesson_url(slug), :submittable => 1)
  end
  resp.body['lesson']
end

#list_lessonsObject



17
18
19
20
21
22
23
# File 'lib/et/api.rb', line 17

def list_lessons
  response = nil
  api_client.open do |client|
    response = client.get('/lessons.json', :submittable => 1)
  end
  response.body['lessons']
end

#submit_lesson(lesson) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/et/api.rb', line 50

def submit_lesson(lesson)
  submission_file = lesson.archive!
  io = Faraday::UploadIO.new(submission_file, "application/x-tar")
  resp = nil
  api_client.open do |client|
    resp = client.post(submission_url(lesson.slug),
      "submission" => { "archive" => io})
  end
  resp
end