Class: HTTPThumbnailerClient

Inherits:
Object
  • Object
show all
Defined in:
lib/httpthumbnailer-client.rb,
lib/httpthumbnailer-client/uri_builder.rb,
lib/httpthumbnailer-client/thumbnail_spec.rb

Defined Under Namespace

Modules: Status, ThumbnailsInputIdentifyMeta Classes: ImageID, Thumbnail, ThumbnailSpec, URIBuilder

Constant Summary collapse

HTTPThumbnailerClientError =
Class.new(ArgumentError)
InvalidThumbnailSpecificationError =
Class.new(HTTPThumbnailerClientError)
ServerResourceNotFoundError =
Class.new(HTTPThumbnailerClientError)
UnsupportedMediaTypeError =
Class.new(HTTPThumbnailerClientError)
ImageTooLargeError =
Class.new(HTTPThumbnailerClientError)
RemoteServerError =
Class.new(HTTPThumbnailerClientError)
UnknownResponseType =
Class.new HTTPThumbnailerClientError
InvalidMultipartResponseError =
Class.new HTTPThumbnailerClientError
@@errors =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_url, options = {}) ⇒ HTTPThumbnailerClient

Returns a new instance of HTTPThumbnailerClient.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/httpthumbnailer-client.rb', line 97

def initialize(server_url, options = {})
  @server_url = server_url
  @client = HTTPClient.new

  # long timeouts for big image data
  @client.send_timeout = options[:send_timeout] || 300
  @client.receive_timeout = options[:receive_timeout] || 300

  # additional HTTP headers to be passed with requests
  @headers = options[:headers] || {}

  # don't use keep alive by default since backend won't support it any way unless fronted with nginx or similar
  @keep_alive = options[:keep_alive] || false
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



205
206
207
# File 'lib/httpthumbnailer-client.rb', line 205

def headers
  @headers
end

#keep_aliveObject (readonly)

Returns the value of attribute keep_alive.



113
114
115
# File 'lib/httpthumbnailer-client.rb', line 113

def keep_alive
  @keep_alive
end

#server_urlObject (readonly)

Returns the value of attribute server_url.



112
113
114
# File 'lib/httpthumbnailer-client.rb', line 112

def server_url
  @server_url
end

Class Method Details

.assign_status_code(error, status) ⇒ Object



40
41
42
43
44
45
# File 'lib/httpthumbnailer-client.rb', line 40

def self.assign_status_code(error, status)
  error.extend Status
  error.status = status
  @@errors[status] = error
  @@errors[error] = status
end

Instance Method Details

#error_for_status(status, body) ⇒ Object



53
54
55
# File 'lib/httpthumbnailer-client.rb', line 53

def error_for_status(status, body)
  @@errors.fetch(status){|err| RemoteServerError}.new(body)
end

#identify(data) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/httpthumbnailer-client.rb', line 185

def identify(data)
  response = @client.request('PUT', "#{@server_url}/identify", nil, data, {'Content-Type' => 'image/autodetect'}.merge(@headers))
  @client.reset_all unless @keep_alive

  content_type = response.header['Content-Type'].last

  image_id = case content_type
  when 'text/plain'
    raise error_for_status(response.status, response.body)
  when 'application/json'
    ImageID.new(response.body)
  else
    raise UnknownResponseType, content_type
  end

  return image_id
rescue HTTPClient::KeepAliveDisconnected
  raise RemoteServerError, 'empty response'
end

#inspectObject



213
214
215
# File 'lib/httpthumbnailer-client.rb', line 213

def inspect
  "#<#{self.class.name} server_url=#{server_url.inspect} keep_alive=#{keep_alive.inspect}>"
end

#thumbnail(data, *spec, &block) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/httpthumbnailer-client.rb', line 115

def thumbnail(data, *spec, &block)
  return_array = true
  uri = if spec.empty?
    URIBuilder.thumbnails(&block)
  elsif spec.all?{|s| s.kind_of? ThumbnailSpec}
    URIBuilder.specs(*spec)
  else
    return_array = false # for .thumbnail(data, 'crop', 60, 30, 'jpeg') kind of usage
    URIBuilder.thumbnail(*spec, &block)
  end

  response = @client.request('PUT', "#{@server_url}#{uri}", nil, data, {'Content-Type' => 'image/autodetect'}.merge(@headers))
  @client.reset_all unless @keep_alive

  content_type = response.header['Content-Type'].last

  thumbnails = case content_type
  when 'text/plain'
    raise error_for_status(response.status, response.body)
  when /^image\//
    thumb = Thumbnail.new(content_type, response.headers['X-Image-Width'], response.headers['X-Image-Height'], response.body)
    return_array ? [thumb] : thumb
  when /^multipart\/mixed/
    parts = []
    parser = MultipartParser::Reader.new(MultipartParser::Reader.extract_boundary_value(content_type))
    parser.on_part do |part|
      part_content_type = part.headers['content-type'] or raise InvalidMultipartResponseError, 'missing Content-Type header in multipart part'
      part_status = part.headers['status']
      data = ''

      part.on_data do |partial_data|
        data << partial_data
      end

      part.on_end do
        case part_content_type
        when 'text/plain'
          part_status or raise InvalidMultipartResponseError, 'missing Status header in error part (text/plain)'

          begin
            raise error_for_status(part_status.to_i, data.strip)
          rescue HTTPThumbnailerClientError => error # raise for stack trace
            parts << error
          end
        when /^image\//
          parts << Thumbnail.new(part_content_type, part.headers['x-image-width'], part.headers['x-image-height'], data)
        else
          raise UnknownResponseType, part_content_type
        end
      end
    end

    parser.write response.body
    parser.ended? or raise InvalidMultipartResponseError, 'truncated multipart message'
    parts
  else
    raise UnknownResponseType, content_type
  end

  thumbnails.extend(ThumbnailsInputIdentifyMeta)
  thumbnails.input_mime_type = response.header['X-Input-Image-Content-Type'].first unless response.header['X-Input-Image-Content-Type'].empty? # deprecated
  thumbnails.input_mime_type = response.header['X-Input-Image-Mime-Type'].first unless response.header['X-Input-Image-Mime-Type'].empty?
  thumbnails.input_width = response.header['X-Input-Image-Width'].first.to_i unless response.header['X-Input-Image-Width'].empty?
  thumbnails.input_height = response.header['X-Input-Image-Height'].first.to_i unless response.header['X-Input-Image-Height'].empty?

  return thumbnails
rescue HTTPClient::KeepAliveDisconnected
  raise RemoteServerError, 'empty response'
end

#with_headers(headers) ⇒ Object



207
208
209
210
211
# File 'lib/httpthumbnailer-client.rb', line 207

def with_headers(headers)
  n = self.dup
  n.headers = @headers.merge headers
  n
end