Class: ImageServer::Adapters::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/image_server/adapters/http.rb,
lib/image_server/adapters/http/error_handler.rb

Defined Under Namespace

Classes: ErrorHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, source, configuration: ImageServer.configuration) ⇒ Http

Returns a new instance of Http.



11
12
13
14
15
# File 'lib/image_server/adapters/http.rb', line 11

def initialize(namespace, source, configuration: ImageServer.configuration)
  @namespace = namespace
  @source = source
  @configuration = configuration
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/image_server/adapters/http.rb', line 9

def url
  @url
end

Instance Method Details

#heightObject



56
57
58
# File 'lib/image_server/adapters/http.rb', line 56

def height
  @body['height'].to_i
end

#image_hashObject



48
49
50
# File 'lib/image_server/adapters/http.rb', line 48

def image_hash
  @body['hash']
end

#upload(upload_uri) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/image_server/adapters/http.rb', line 44

def valid?
  @body && (image_hash && width && height)
end

#widthObject



52
53
54
# File 'lib/image_server/adapters/http.rb', line 52

def width
  @body['width'].to_i
end