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

#apply_stash(action, filename) ⇒ Object



61
62
63
# File 'lib/exercism/api.rb', line 61

def apply_stash(action, filename)
  get_stash(action, filename)
end

#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

#currentObject



73
74
75
76
77
78
# File 'lib/exercism/api.rb', line 73

def current
  conn.get do |req|
    req.url endpoint('user/assignments/current')
    req.params = {:key => user.key}
  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

#list_stash(action) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/exercism/api.rb', line 65

def list_stash(action)
  response = conn.get do |req|
    req.url endpoint(action)
    req.params['key'] = user.key
  end
  JSON.parse(response.body)
end

#peekObject



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

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

#save_stash(action, filename) ⇒ Object



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

def save_stash(action, filename)
  path = File.join(filename)
  contents = File.read path
  response = conn.post do |req|
    req.url endpoint(action)
    req.headers['Accept'] = 'application/json'
    req.headers['Content-Type'] = 'application/json'
    req.body = {:code => contents, :key => user.key, :filename => path}.to_json
  end
  response
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