Files
old-pruning-excersise/lib/repos/tree.rb
2018-08-13 18:54:12 +02:00

42 lines
972 B
Ruby

require 'rest-client'
require 'retries'
require 'json'
require_relative '../exceptions'
NUMBER_OF_RETRIES = 3
module Pruning
module Repos
# Handles communication with origin server
# and all its problems
class Tree
def initialize(client, base_url)
@client = client
@base_url = base_url
end
def get(name)
with_retries(max_tries: NUMBER_OF_RETRIES, rescue: [Pruning::Exceptions::ServerErrorOnOrigin]) do
begin
resp = @client.get(url(name))
rescue RestClient::ExceptionWithResponse => e
raise Pruning::Exceptions::ServerErrorOnOrigin if e.response.code != 404
raise Pruning::Exceptions::OriginCannotFindTheResource
end
return JSON(resp.body)
end
raise Pruning::Exceptions::UnexpectedError
end
private
def url(name)
"#{@base_url}/tree/#{name}"
end
end
end
end