Class: MultiVideoStreaming::Drivers::Bunny

Inherits:
Protocols::Platform show all
Defined in:
lib/multi_video_streaming/drivers/bunny.rb

Constant Summary collapse

PARAMS_EQUIVALENT =
{
  "guid" => :video_id,
  "title" => :title,
  "status" => :status,
  "files" => :files,
  "availableResolutions" => :available_resolutions,
  "directPlay" => :direct_play,
  "dateUploaded" => :date_uploaded
}
STATUS_EQUIVALENT =
{
  "0" => "processing",
  "1" => "processing",
  "2" => "transcoding",
  "3" => "processing",
  "4" => "available",
  "5" => "failed"
}
BASE_URL =
"https://video.bunnycdn.com"

Instance Attribute Summary

Attributes inherited from Protocols::Platform

#dependency_params, #https_service_instance, #required_fields_validator_instance, #response_instance

Instance Method Summary collapse

Methods inherited from Protocols::Platform

#initialize

Methods included from Protocols::Interface

#method

Constructor Details

This class inherits a constructor from MultiVideoStreaming::Protocols::Platform

Instance Method Details

#delete_videoObject



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
# File 'lib/multi_video_streaming/drivers/bunny.rb', line 141

def delete_video
  begin
    video_id, library_id, api_key = self.dependency_params.values_at(:video_id, :library_id, :api_key)
    @original_status_code = nil

    self.required_fields_validator_instance.validate(library_id: library_id, video_id: video_id, api_key: api_key)

    response = self.https_service_instance.delete(uri: "#{BASE_URL}/library/#{library_id}/videos/#{video_id}", headers: { :AccessKey => api_key })

    @original_status_code = response["status_code"]

    if response["status_code"] != 200
      if response["status_code"] == 404
        raise MultiVideoStreaming::Helpers::GetDefaultErrorMessage.not_found(video_id: video_id)              
      end

      raise response["Message"] || response["message"] || MultiVideoStreaming::Helpers::GetDefaultErrorMessage.by_method(method: :delete)
    end

    return self.response_instance.add_response(
      platform_name: "bunny",
      status: "success",
      original_status_code: @original_status_code,
      data: {},
      message:  "Video deleted successfully."
    )
  rescue => exception
    return self.response_instance.add_response(
      platform_name: "bunny",
      status: "error",
      original_status_code: @original_status_code,
      data: {},
      message: exception.message
    )
  end
end

#get_videoObject



76
77
78
79
80
81
82
83
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
113
114
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
# File 'lib/multi_video_streaming/drivers/bunny.rb', line 76

def get_video
  begin
    video_id, library_id, pull_zone, api_key = self.dependency_params.values_at(:video_id, :library_id, :pull_zone, :api_key)
    @original_status_code = nil

    self.required_fields_validator_instance.validate(
      library_id: library_id,
      video_id: video_id,
      pull_zone: pull_zone,
      api_key: api_key
    )

    response = self.https_service_instance.get(uri: "#{BASE_URL}/library/#{library_id}/videos/#{video_id}", headers: { :AccessKey => api_key })

    @original_status_code = response["status_code"]

    if response["status_code"] != 200
      if response["status_code"] == 404
        raise MultiVideoStreaming::Helpers::GetDefaultErrorMessage.not_found(video_id: video_id)
      end

      raise response["Message"] || response["message"] || MultiVideoStreaming::Helpers::GetDefaultErrorMessage.by_method(method: :get)
    end

    response["directPlay"] = "https://video.bunnycdn.com/play/#{library_id}/#{video_id}"
    response["availableResolutions"] = response["availableResolutions"]&.split(",")

    files = []
    response["availableResolutions"]&.map do |available_resolution|
      files << 
      {
        file: "https://#{pull_zone}.b-cdn.net/#{response["guid"]}/play_#{available_resolution}.mp4",
        label: available_resolution
      }
    end

    MultiVideoStreaming::Helpers::SortFilesArrayByLabel.sort(array: files)
    response["files"] = files
    status = STATUS_EQUIVALENT[response["status"].to_s]

    response["status"] = STATUS_EQUIVALENT[response["status"].to_s]

    if response["status"] != "available"
      raise MultiVideoStreaming::Helpers::GetDefaultErrorMessage.not_available
    end

    return self.response_instance.add_response(
      platform_name: "bunny",
      status: "success",
      original_status_code: @original_status_code,
      data: response,
      params_equivalent: PARAMS_EQUIVALENT,
      message:  "Video found successfully."
    )
  rescue => exception
    return self.response_instance.add_response(
      platform_name: "bunny",
      status: "error",
      original_status_code: @original_status_code,
      data: {},
      message: exception.message
    )
  end
end

#upload_video(url:, title:) ⇒ Object



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
# File 'lib/multi_video_streaming/drivers/bunny.rb', line 27

def upload_video(url:, title:)
  begin
    library_id, api_key = self.dependency_params.values_at(:library_id, :api_key)
    @original_status_code = nil

    self.required_fields_validator_instance.validate(library_id: library_id, api_key: api_key)

    create_video_response = self.https_service_instance.post(uri: "#{BASE_URL}/library/#{library_id}/videos", headers: { :AccessKey => api_key }, body: { :title => title })

    if create_video_response["status_code"] != 200
      @original_status_code = create_video_response["status_code"]
      raise create_video_response["Message"] || create_video_response["message"] || MultiVideoStreaming::Helpers::GetDefaultErrorMessage.by_method(method: :upload)
    end

    video_id = create_video_response["guid"]
    upload_response = self.https_service_instance.post(uri: "#{BASE_URL}/library/#{library_id}/videos/#{video_id}/fetch", body: { url: url }, headers: { :AccessKey => api_key })

    @original_status_code = upload_response["status_code"]

    if upload_response["status_code"] != 200
      self.https_service_instance.delete(uri: "#{BASE_URL}/library/#{library_id}/videos/#{video_id}", headers: { :AccessKey => api_key })
      raise upload_response["Message"] || upload_response["message"] || MultiVideoStreaming::Helpers::GetDefaultErrorMessage.by_method(method: :upload)
    end

    create_video_response["availableResolutions"] = []
    create_video_response["status"] = STATUS_EQUIVALENT[create_video_response["status"].to_s]
    create_video_response["directPlay"] = "https://video.bunnycdn.com/play/#{library_id}/#{video_id}"
    create_video_response["files"] = []

    return self.response_instance.add_response(
      platform_name: "bunny",
      status: "success",
      original_status_code: @original_status_code,
      data: create_video_response,
      params_equivalent: PARAMS_EQUIVALENT,
      message: "Video uploaded successfully."
    )

  rescue => exception
    return self.response_instance.add_response(
      platform_name: "bunny",
      status: "error",
      original_status_code: @original_status_code,
      data: {},
      message: exception.message
    )
  end
end