Files
old-pruning-excersise/lib/repos/tree.rb
2018-08-13 15:01:28 +02:00

37 lines
896 B
Ruby

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