class Adparlor::Facebook::GraphApi::AdVideo

Public Instance Methods

base_uri() click to toggle source
# File lib/adparlor/facebook/graph_api/ad_video.rb, line 13
def base_uri
  'https://graph-video.facebook.com'
end
path() click to toggle source
# File lib/adparlor/facebook/graph_api/ad_video.rb, line 21
def path
  raise FbError.new('missing one of account_id, user_id, event_id, page_id or group_id required for video creation', 500) unless @account_id || @user_id || @event_id || @page_id || @group_id
  if @account_id
    "/act_#{@account_id}/advideos"
  else
    "/#{@user_id || @event_id || @page_id || @group_id}/videos"
  end
end
post(path, options, _method = nil) click to toggle source
Calls superclass method
# File lib/adparlor/facebook/graph_api/ad_video.rb, line 30
def post(path, options, _method = nil)
  if @slideshow_spec
    super(path, options)
  else
    response = start_video_request(options)
    raise response['error']['message'] if response.key?('error')
    upload_session_id = response['upload_session_id']
    video_id = response['video_id']
    loop do
      response = upload_chunk(response['start_offset'], response['end_offset'], upload_session_id, options)
      break if response['start_offset'] == response['end_offset']
      sleep 5
    end
    response = finish_video_request(options, upload_session_id)
    if response['success']
      { id: video_id }
    else
      response
    end
  end
end
thumbnails() click to toggle source
# File lib/adparlor/facebook/graph_api/ad_video.rb, line 17
def thumbnails
  @thumbnails ||= CollectionProxy.new VideoThumbnail, "/#{id}/thumbnails", access_token
end

Private Instance Methods

create_file_from_offset(offset) click to toggle source
# File lib/adparlor/facebook/graph_api/ad_video.rb, line 54
def create_file_from_offset(offset)
  file = Tempfile.new('image_slice')
  file.binmode
  file.write(video.read(offset))
  file.rewind
  file
end
finish_video_request(options, upload_session_id) click to toggle source
# File lib/adparlor/facebook/graph_api/ad_video.rb, line 62
def finish_video_request(options, upload_session_id)
  options[:upload_phase] = 'finish'
  options[:upload_session_id] = upload_session_id

  conn.post full_url(path, options) do |request|
    request.headers['Content-Type'] = 'application/json'
    request.params = options
  end.body
end
normalized_file_name() click to toggle source

Remove any query string parameters before getting the mime type should probably be done in mime types gem

# File lib/adparlor/facebook/graph_api/ad_video.rb, line 74
def normalized_file_name
  @source.split('?').first
end
start_video_request(options) click to toggle source
# File lib/adparlor/facebook/graph_api/ad_video.rb, line 78
def start_video_request(options)
  options[:upload_phase] = 'start'
  options[:file_size] = video_size

  conn.post full_url(path, options) do |request|
    request.headers['Content-Type'] = 'application/json'
    request.params = options
  end.body
end
upload_chunk(start_offset, end_offset, upload_session_id, options) click to toggle source
# File lib/adparlor/facebook/graph_api/ad_video.rb, line 88
def upload_chunk(start_offset, end_offset, upload_session_id, options)
  read_length = end_offset.to_i - start_offset.to_i unless end_offset == video_size
  temp_file = create_file_from_offset(read_length)
  response = conn.post full_url(path, options) do |request|
    request.headers['Content-Type'] = 'multipart/form-data'
    request.params = options
    request.body = {
      upload_phase: 'transfer', upload_session_id: upload_session_id,
      start_offset: start_offset, video_file_chunk: Faraday::UploadIO.new(temp_file.path, MIME::Types.type_for(normalized_file_name))
    }
  end.body
  if response.key?('error') && response['error']['error_subcode'] == 1487742
    raise FbError.new('There have been too many calls from this ad-account. Wait a bit and try again.', 500)
  else
    response
  end
end
video() click to toggle source
# File lib/adparlor/facebook/graph_api/ad_video.rb, line 106
def video
  @video ||= if %r{https?} =~ URI.parse(@source).scheme
               tmp_file = Tempfile.new([File.basename(normalized_file_name), File.extname(normalized_file_name)])
               tmp_file.binmode
               open(@source) { |file| tmp_file.write(file.read) }
               tmp_file.rewind
               tmp_file
             else
               File.open(normalized_file_name, 'r')
             end
end
video_size() click to toggle source
# File lib/adparlor/facebook/graph_api/ad_video.rb, line 118
def video_size
  File.size(video.path)
end