Class: Limelight

Inherits:
Object
  • Object
show all
Defined in:
lib/limelight_video.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Limelight

Returns a new instance of Limelight.

Raises:

  • (KeyError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/limelight_video.rb', line 9

def initialize(options = {})
  @organization = options.fetch(:organization, ENV['LIMELIGHT_ORGANIZATION'])
  raise KeyError.new("organization") if !@organization

  @access_key = options.fetch(:access_key, ENV['LIMELIGHT_ACCESS_KEY'])
  @secret = options.fetch(:secret, ENV['LIMELIGHT_SECRET'])

  @host = 'http://api.videoplatform.limelight.com'
  @base_url = "/rest/organizations/#{@organization}"
  @base_media_url = "#{@base_url}/media"
  @base_channels_url = "#{@base_url}/channels"
  @client = Faraday.new(@host) do |builder|
    builder.request :url_encoded
    builder.adapter :net_http
  end
end

Instance Method Details

#create_channel(name) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/limelight_video.rb', line 72

def create_channel(name)
  # http://api.videoplatform.limelight.com/rest/organizations/<org id>/channels.{XML,JSON}
  path = generate_encoded_path('post', @base_channels_url)
  response = @client.post(path, title: name)

  JSON.parse response.body
end

#create_metadata(names) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/limelight_video.rb', line 87

def (names)
  # http://api.videoplatform.limelight.com/rest/organizations/<org id>/media/properties/custom/<property name>
  Array(names).each do |name|
    path = generate_encoded_path('put', "#{@base_media_url}/properties/custom/#{name}")
    @client.put(path)
  end
end

#list_metadataObject



95
96
97
98
99
100
# File 'lib/limelight_video.rb', line 95

def 
  # http://api.videoplatform.limelight.com/rest/organizations/<orgid>/media/properties/custom.{XML,JSON}
  response = @client.get("#{@base_media_url}/properties/custom.json")
   = JSON.parse response.body
  ["custom_property_types"].map { |meta| meta["type_name"] }
end

#media_info(media_id) ⇒ Object



26
27
28
29
# File 'lib/limelight_video.rb', line 26

def media_info(media_id)
  response = @client.get("#{@base_media_url}/#{media_id}/properties.json")
  JSON.parse response.body
end

#publish_channel(id) ⇒ Object



80
81
82
83
84
85
# File 'lib/limelight_video.rb', line 80

def publish_channel(id)
  path = generate_encoded_path('put', "#{@base_channels_url}/#{id}/properties")
  response = @client.put(path, state: "Published")

  JSON.parse response.body
end

#remove_metadata(names) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/limelight_video.rb', line 102

def (names)
  # http://api.videoplatform.limelight.com/rest/organizations/<org id>/media/properties/custom/<property name>
  Array(names).each do |name|
    path = generate_encoded_path('delete', "#{@base_media_url}/properties/custom/#{name}")
    @client.delete(path)
  end
end

#upload(filename_or_io, attributes = {}) ⇒ Object



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
# File 'lib/limelight_video.rb', line 31

def upload(filename_or_io, attributes = {})
  case filename_or_io
    when String
      file = File.open(filename_or_io)
      filename = filename_or_io
      mime = MIME::Types.of(filename_or_io)
    when Tempfile, StringIO
      file = filename_or_io
      filename = attributes.fetch(:filename)
      mime = attributes[:type] || MIME::Types.of(filename)
    else
      raise Errno::ENOENT
    end

  media_file = Faraday::UploadIO.new(file, mime, filename)
  options = {
    title: attributes.fetch(:title, 'Unnamed'),
    media_file: media_file
  }
  if attributes[:metadata]
    custom_properties = attributes[:metadata]
    properties_to_create = custom_properties.keys.map(&:to_s) - 
    (properties_to_create)
  end

  options[:custom_property] = attributes.fetch(:metadata, {})
  response = @client.post(upload_path, options) do |req|
    req.options[:open_timeout] = 60*60
  end

  JSON.parse response.body
end

#upload_pathObject



68
69
70
# File 'lib/limelight_video.rb', line 68

def upload_path
  generate_encoded_path('post', @base_media_url)
end

#upload_urlObject



64
65
66
# File 'lib/limelight_video.rb', line 64

def upload_url
  @host + upload_path
end