class Resizing::Client

Client class for Resizing

Public Class Methods

new(*attrs) click to toggle source
# File lib/resizing/client.rb, line 32
def initialize(*attrs)
  initialize_config(*attrs)
end

Public Instance Methods

delete(image_id) click to toggle source
# File lib/resizing/client.rb, line 84
def delete(image_id)
  url = build_delete_url(image_id)

  response = handle_faraday_error do
    http_client.delete(url) do |request|
      request.headers['X-ResizingToken'] = config.generate_auth_header
    end
  end

  result = handle_delete_response(response)
  result
end
get(image_id) click to toggle source
# File lib/resizing/client.rb, line 36
def get(image_id)
  raise NotImplementedError
end
metadata(image_id, options = {}) click to toggle source
# File lib/resizing/client.rb, line 97
def metadata(image_id, options = {})
  url = build_metadata_url(image_id)

  response = handle_faraday_error do
    http_client.get(url) do |request|
      request.headers['X-ResizingToken'] = config.generate_auth_header
    end
  end

  result = handle_metadata_response(response)
  result
end
post(file_or_binary, options = {}) click to toggle source
# File lib/resizing/client.rb, line 40
def post(file_or_binary, options = {})
  ensure_content_type(options)

  url = build_post_url

  filename = gather_filename file_or_binary, options

  body = to_io(file_or_binary)
  params = {
    image: Faraday::UploadIO.new(body, options[:content_type], filename)
  }

  response = handle_faraday_error do
    http_client.post(url, params) do |request|
      request.headers['X-ResizingToken'] = config.generate_auth_header
    end
  end

  result = handle_create_response(response)
  result
end
put(image_id, file_or_binary, options) click to toggle source
# File lib/resizing/client.rb, line 62
def put(image_id, file_or_binary, options)
  ensure_content_type(options)

  url = build_put_url(image_id)

  filename = gather_filename file_or_binary, options

  body = to_io(file_or_binary)
  params = {
    image: Faraday::UploadIO.new(body, options[:content_type], filename)
  }

  response = handle_faraday_error do
    http_client.put(url, params) do |request|
      request.headers['X-ResizingToken'] = config.generate_auth_header
    end
  end

  result = handle_create_response(response)
  result
end

Private Instance Methods

build_delete_url(image_id) click to toggle source
# File lib/resizing/client.rb, line 129
def build_delete_url(image_id)
  "#{config.image_host}/projects/#{config.project_id}/upload/images/#{image_id}"
end
build_get_url(image_id) click to toggle source
# File lib/resizing/client.rb, line 112
def build_get_url(image_id)
  "#{config.image_host}/projects/#{config.project_id}/upload/images/#{image_id}"
end
build_metadata_url(image_id) click to toggle source
# File lib/resizing/client.rb, line 133
def build_metadata_url(image_id)
  "#{config.image_host}/projects/#{config.project_id}/upload/images/#{image_id}/metadata"
end
build_post_url() click to toggle source
# File lib/resizing/client.rb, line 116
def build_post_url
  "#{config.image_host}/projects/#{config.project_id}/upload/images/"
end
build_put_url(image_id) click to toggle source
# File lib/resizing/client.rb, line 125
def build_put_url(image_id)
  "#{config.image_host}/projects/#{config.project_id}/upload/images/#{image_id}"
end
decode_error_from(response) click to toggle source
# File lib/resizing/client.rb, line 186
def decode_error_from response
  result = JSON.parse(response.body) rescue {}
  err = APIError.new(result['message'] || "invalid http status code #{response.status}")
  err.decoded_body = result
  err
end
ensure_content_type(options) click to toggle source
# File lib/resizing/client.rb, line 149
def ensure_content_type(options)
  raise ArgumentError, "need options[:content_type] for #{options.inspect}" unless options[:content_type]
end
gather_filename(file_or_binary, options) click to toggle source
# File lib/resizing/client.rb, line 120
def gather_filename file_or_binary, options
  filename = options[:filename]
  filename ||= file_or_binary.respond_to?(:path) ? File.basename(file_or_binary.path) : nil
end
handle_create_response(response) click to toggle source
# File lib/resizing/client.rb, line 153
def handle_create_response(response)
  raise APIError, "no response is returned" if response.nil?

  case response.status
  when HTTP_STATUS_OK, HTTP_STATUS_CREATED
    JSON.parse(response.body)
  else
    raise decode_error_from(response)
  end
end
handle_delete_response(response) click to toggle source
# File lib/resizing/client.rb, line 164
def handle_delete_response(response)
  raise APIError, "no response is returned" if response.nil?

  case response.status
  when HTTP_STATUS_OK, HTTP_STATUS_NOT_FOUND
    JSON.parse(response.body)
  else
    raise decode_error_from(response)
  end
end
handle_metadata_response(response) click to toggle source
# File lib/resizing/client.rb, line 175
def handle_metadata_response(response)
  raise APIError, "no response is returned" if response.nil?

  case response.status
  when HTTP_STATUS_OK, HTTP_STATUS_NOT_FOUND
    JSON.parse(response.body)
  else
    raise decode_error_from(response)
  end
end
to_io(data) click to toggle source
# File lib/resizing/client.rb, line 138
def to_io(data)
  return data.to_io if data.respond_to? :to_io

  case data
  when String
    StringIO.new(data)
  else
    raise ArgumentError, "file_or_binary is required IO class or String:#{data.class}"
  end
end