Module: VimeoUpload

Defined in:
lib/vimeo_upload.rb,
lib/vimeo_upload/version.rb

Constant Summary collapse

VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.change_title(filename, video_id, api_key) ⇒ Object

Change video title



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/vimeo_upload.rb', line 84

def self.change_title(filename, video_id, api_key)

  # Change video title of a video in vimeo
  # I prefer putting api_key as an environment variable. Use figaro gem if needed
  # Call function in the following format
  # VimeoUpload.change_title(filename, video_id, vimeo_key)
  # eg;
  # VimeoUpload.upload("'My Video', '123456789', ENV['api_key'])
  # Returns the video_id

  auth = "bearer #{api_key}"

  uri = URI("https://api.vimeo.com/videos/#{video_id}")

  patch_req = Net::HTTP::Patch.new("#{uri.path}?#{uri.query}", initheader = {"Authorization" => auth, "name" => "Test"})
  patch_req.set_form_data({"name" => filename})


  begin
    http=Net::HTTP.new(uri.host, uri.port)
    http.use_ssl=true
    patch_resp = http.request(patch_req)

  rescue Errno::EPIPE
    puts "Error occured"
  end
  
  return video_id
end

.upload(filepath, filename, api_key) ⇒ Object

Upload function



7
8
9
10
11
12
13
14
15
16
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vimeo_upload.rb', line 7

def self.upload(filepath, filename, api_key)

  # Upload a video to vimeo
  # I prefer putting api_key as an environment variable. Use figaro gem if needed
  # Call function in the following format
  # VimeoUpload.upload(absolute_filepath, filename, vimeo_key)
  # eg;
  # VimeoUpload.upload("#{Rails.root}/public/uploads/video.webm", 'My Video', ENV['api_key'])
  # Return a video_id that references to the video

  auth = "bearer #{api_key}"

  # create ticket
  resp = HTTParty.post "https://api.vimeo.com/me/videos", 
                        headers: {"Authorization" => auth, 
                                  "Accept" => "application/vnd.vimeo.*+json;version=3.2"}, 
                        body: {type: "streaming"}

  ticket = JSON.parse(resp.body)
  if resp.body['error']
    raise StandardError, ticket['error']
  end
  target = ticket["upload_link_secure"]
  size = File.size(filepath)
  last_byte = 0
  # create ticket end

  # upload video
  File.open(filepath, "rb") do |f|
    uri = URI(target)
    while last_byte < size do
      req = Net::HTTP::Put.new("#{uri.path}?#{uri.query}", initheader = {"Authorization" => auth, "Content-Type" => "video/mp4", "Content-Length" => size.to_s})
      req.body = f.read

      begin
        http=Net::HTTP.new(uri.host, uri.port)
        http.use_ssl=true
        response = http.request(req)

      rescue Errno::EPIPE
        puts "Error occured"
      end

      progress_resp = HTTParty.put target, headers: {"Content-Range" => 'bytes */*', "Authorization" => auth}
      last_byte = progress_resp.headers["range"].split("-").last.to_i
    end
  end
  # upload video end

  # delete ticket (required to view video in vimeo)
  complete_uri=ticket["complete_uri"]

  uri = URI("https://api.vimeo.com" + complete_uri)
  delete_req = Net::HTTP::Delete.new("#{uri.path}?#{uri.query}", initheader = {"Authorization" => auth})

  begin
    http=Net::HTTP.new(uri.host, uri.port)
    http.use_ssl=true
    delete_resp = http.request(delete_req)

  rescue Errno::EPIPE
    puts "Error occured"
  end

  video_location = delete_resp["location"]
  # delete ticket end

  video_id = video_location.split('/')[2]
  
  # change title
  change_title(filename, video_id, api_key)

  return video_id
end