Class: Exercism::Api

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, user, project_dir = nil) ⇒ Api

Returns a new instance of Api.



7
8
9
10
11
# File 'lib/exercism/api.rb', line 7

def initialize(url, user, project_dir = nil)
  @url = url
  @user = user
  @project_dir = project_dir
end

Instance Attribute Details

#project_dirObject (readonly)

Returns the value of attribute project_dir.



6
7
8
# File 'lib/exercism/api.rb', line 6

def project_dir
  @project_dir
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/exercism/api.rb', line 6

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'lib/exercism/api.rb', line 6

def user
  @user
end

Instance Method Details

#connObject



13
14
15
16
17
18
# File 'lib/exercism/api.rb', line 13

def conn
  Faraday.new(:url => url) do |c|
    c.use Faraday::Adapter::NetHttp
    c.headers['User-Agent'] = user_agent
  end
end

#demoObject



20
21
22
# File 'lib/exercism/api.rb', line 20

def demo
  get_and_save('assignments/demo')
end

#fetchObject



24
25
26
# File 'lib/exercism/api.rb', line 24

def fetch
  get_and_save('user/assignments/current')
end

#peekObject



28
29
30
# File 'lib/exercism/api.rb', line 28

def peek
  get_and_save('user/assignments/next')
end

#submit(filename) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/exercism/api.rb', line 32

def submit(filename)
  path = File.join(filename)
  contents = File.read path

  json_request(:post, 'user/assignments', {
    :key  => user.key,
    :code => contents,
    :path => path
  })
end

#unsubmitObject



43
44
45
46
47
# File 'lib/exercism/api.rb', line 43

def unsubmit
  json_request(:delete, 'user/assignments', {
    :key => user.key
  })
end