class MetallicaLogo::Client

HTTP client for requesting logo generation and also if desired, downloading to a local destination

Constants

MAX_CHARACTERS

Attributes

base_url[R]
rest_klass[R]

Public Class Methods

new(base_url: MetallicaLogo::BASE_URL, rest_klass: RestClient) click to toggle source
# File lib/metallica_logo/client.rb, line 11
def initialize(base_url: MetallicaLogo::BASE_URL, rest_klass: RestClient)
  @rest_klass = rest_klass
  @base_url = base_url
end

Public Instance Methods

Private Instance Methods

download(url) click to toggle source
# File lib/metallica_logo/client.rb, line 36
def download(url)
  rest_klass.get(url)
rescue rest_klass::ExceptionWithResponse => e
  validate_response(e.response)
end
post(url, payload) click to toggle source
# File lib/metallica_logo/client.rb, line 42
def post(url, payload)
  response = rest_klass.post(url, payload)
  validate_response(response)
rescue rest_klass::ExceptionWithResponse => e
  validate_response(e.response)
end
valid_json?(value) click to toggle source
# File lib/metallica_logo/client.rb, line 62
def valid_json?(value)
  result = JSON.parse(value)
  result.is_a?(Hash)
rescue JSON::ParserError
  false
end
validate_response(response) click to toggle source
# File lib/metallica_logo/client.rb, line 49
def validate_response(response)
  unless valid_json?(response.body)
    raise ServerError, 'Invalid JSON returned'
  end

  case response.code.to_s
  when /2\d\d/ then response
  when /5\d\d/ then raise ServerError, response.body
  else
    raise RequestError, response.body
  end
end