Files
old-pruning-excersise/lib/repos/tree.rb

42 lines
972 B
Ruby
Raw Normal View History

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