class Exercism::Api
Attributes
project_dir[R]
url[R]
user[R]
Public Class Methods
new(url, user, project_dir = nil)
click to toggle source
# File lib/exercism/api.rb, line 7 def initialize(url, user, project_dir = nil) @url = url @user = user @project_dir = project_dir end
Public Instance Methods
apply_stash(action, filename)
click to toggle source
# File lib/exercism/api.rb, line 61 def apply_stash(action, filename) get_stash(action, filename) end
conn()
click to toggle source
# 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
current()
click to toggle source
# 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
demo()
click to toggle source
# File lib/exercism/api.rb, line 20 def demo get_and_save('assignments/demo') end
fetch()
click to toggle source
# File lib/exercism/api.rb, line 24 def fetch get_and_save('user/assignments/current') end
list_stash(action)
click to toggle source
# 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
peek()
click to toggle source
# File lib/exercism/api.rb, line 28 def peek get_and_save('user/assignments/next') end
save_stash(action, filename)
click to toggle source
# 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)
click to toggle source
# 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
unsubmit()
click to toggle source
# File lib/exercism/api.rb, line 43 def unsubmit json_request(:delete, 'user/assignments', { :key => user.key }) end
Private Instance Methods
endpoint(action = nil)
click to toggle source
# File lib/exercism/api.rb, line 114 def endpoint(action = nil) "/api/v1/#{action}".chomp('/') end
get_and_save(action)
click to toggle source
# File lib/exercism/api.rb, line 91 def get_and_save(action) response = conn.get do |req| req.url endpoint(action) req.params['key'] = user.key end save response.body end
get_stash(action, filename)
click to toggle source
# File lib/exercism/api.rb, line 82 def get_stash(action, filename) response = conn.get do |req| req.url endpoint(action) req.params['key'] = user.key req.params['filename'] = filename end Stash.new(JSON.parse(response.body)) end
json_request(verb, path, body)
click to toggle source
# File lib/exercism/api.rb, line 99 def json_request(verb, path, body) response = conn.send(verb) do |request| request.url endpoint(path) request.headers['Accept'] = 'application/json' request.headers['Content-Type'] = 'application/json' request.body = body.to_json end response end
save(body)
click to toggle source
# File lib/exercism/api.rb, line 118 def save(body) Assignment.save(JSON.parse(body), project_dir) end
user_agent()
click to toggle source
# File lib/exercism/api.rb, line 110 def user_agent "github.com/kytrinyx/exercism CLI v#{Exercism::VERSION}" end