class ImageServer::Adapters::Http

Attributes

url[R]

Public Class Methods

new(namespace, source, configuration: ImageServer.configuration) click to toggle source
# File lib/image_server/adapters/http.rb, line 11
def initialize(namespace, source, configuration: ImageServer.configuration)
  @namespace = namespace
  @source = source
  @configuration = configuration
end

Public Instance Methods

height() click to toggle source
# File lib/image_server/adapters/http.rb, line 56
def height
  @body['height'].to_i
end
image_hash() click to toggle source
# File lib/image_server/adapters/http.rb, line 48
def image_hash
  @body['hash']
end
upload(upload_uri) click to toggle source
# File lib/image_server/adapters/http.rb, line 17
def upload(upload_uri)
  logger.info "ImageServer::Adapters::Http --> uploading to image server: [#{upload_uri}]"

  response = Net::HTTP.start(upload_uri.host, upload_uri.port) do |http|
    request = Net::HTTP::Post.new(upload_uri.request_uri)
    request['Accept'] = "application/json"
    request['Content-Type'] = "application/json"
    request.body = if source_is_url?
      '{}' # blank body
    elsif @source.is_a?(File)
      @source
    elsif @source.respond_to?(:path)
      File.open(@source.path).read
    else
      raise('Not supported')
    end

    http.read_timeout = 60
    http.request(request)
  end

  ErrorHandler.new(response).handle_errors!
  @body = JSON.parse(response.body)
rescue Errno::ECONNREFUSED => e
  raise ImageServerUnavailable
end
valid?() click to toggle source
# File lib/image_server/adapters/http.rb, line 44
def valid?
  @body && (image_hash && width && height)
end
width() click to toggle source
# File lib/image_server/adapters/http.rb, line 52
def width
  @body['width'].to_i
end

Private Instance Methods

logger() click to toggle source
# File lib/image_server/adapters/http.rb, line 62
def logger
  @@logger ||= ImageServer::Logger.new
end
source_is_url?() click to toggle source
# File lib/image_server/adapters/http.rb, line 66
def source_is_url?
  return false unless @source.is_a?(String)

  @source.start_with?('http') || @source.start_with?('//')
end