Sha256: 48b7b24989c2043b612a1bdebefacd514ad14808bcf6bd6000d36ee0728dea82

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

class Exercism
  class Api

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

    def conn
     conn = Faraday.new(:url => url) do |c|
        c.use Faraday::Adapter::NetHttp
      end
    end

    def fetch
      response = conn.get do |req|
        req.url endpoint('current')
        req.headers['User-Agent'] = user_agent
        req.params['key'] = user.key
      end
      save response.body
    end

    def peek
      response = conn.get do |req|
        req.url endpoint('next')
        req.headers['User-Agent'] = user_agent
        req.params['key'] = user.key
      end
      save response.body
    end

    def submit(filename)
      path = File.join(filename)
      contents = File.read path
      response = conn.post do |req|
        req.url endpoint
        req.headers['Accept'] = 'application/json'
        req.headers['Content-Type'] = 'application/json'
        req.headers['User-Agent'] = user_agent
        req.body = {code: contents, key: user.key, path: path}.to_json
      end
      response
    end

    private

    def user_agent
      "exercism-CLI v#{Exercism::VERSION}"
    end

    def endpoint(action = nil)
      "/api/v1/user/assignments/#{action}".chomp('/')
    end

    def save(body)
      Assignment.save(JSON.parse(body), project_dir)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
exercism-0.0.13 lib/exercism/api.rb
exercism-0.0.12 lib/exercism/api.rb