class Resizing::Video::Client

Public Class Methods

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

Public Instance Methods

build_prepare_url() click to toggle source
# File lib/resizing/video/client.rb, line 58
def build_prepare_url
  "#{config.video_host}/projects/#{config.project_id}/upload/videos/prepare"
end
delete(response_or_url) click to toggle source
# File lib/resizing/video/client.rb, line 36
def delete response_or_url
  url = url_from response_or_url, 'destroy_url'

  response = handle_faraday_error do
    http_client.put(url) do |request|
      request.headers['X-ResizingToken'] = config.generate_auth_header
    end
  end
  handle_upload_completed_response response
end
metadata(response_or_url) click to toggle source
# File lib/resizing/video/client.rb, line 47
def metadata response_or_url
  url = url_from response_or_url, 'self_url'

  response = handle_faraday_error do
    http_client.get(url) do |request|
      request.headers['X-ResizingToken'] = config.generate_auth_header
    end
  end
  handle_metadata_response response
end
prepare() click to toggle source
# File lib/resizing/video/client.rb, line 14
def prepare
  url = build_prepare_url

  response = handle_faraday_error do
    http_client.post(url) do |request|
      request.headers['X-ResizingToken'] = config.generate_auth_header
    end
  end
  handle_prepare_response response
end
upload_completed(response_or_url) click to toggle source
# File lib/resizing/video/client.rb, line 25
def upload_completed response_or_url
  url = url_from response_or_url, 'upload_completed_url'

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

Private Instance Methods

handle_error_response(response) click to toggle source
# File lib/resizing/video/client.rb, line 108
def handle_error_response response
  result = JSON.parse(response.body) rescue {}
  err = APIError.new("invalid http status code #{response.status}")
  err.decoded_body = result
  raise err
end
handle_metadata_response(response) click to toggle source
# File lib/resizing/video/client.rb, line 97
def handle_metadata_response response
  raise APIError, "no response is returned" if response.nil?

  case response.status
  when HTTP_STATUS_OK
    JSON.parse(response.body)
  else
    handle_error_response response
  end
end
handle_prepare_response(response) click to toggle source
# File lib/resizing/video/client.rb, line 75
def handle_prepare_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
    handle_error_response response
  end
end
handle_upload_completed_response(response) click to toggle source
# File lib/resizing/video/client.rb, line 86
def handle_upload_completed_response response
  raise APIError, "no response is returned" if response.nil?

  case response.status
  when HTTP_STATUS_OK
    JSON.parse(response.body)
  else
    handle_error_response response
  end
end
url_from(response_or_url, name) click to toggle source
# File lib/resizing/video/client.rb, line 64
def url_from response_or_url, name
  if response_or_url.kind_of? String
    response_or_url
  elsif response_or_url.kind_of? Hash
    response_or_url[name.to_s] || response_or_url[name.intern]
  else
    raise ArgumentError, "upload_completed is require Hash or String"
  end
end