Class: EncodingDotCom::Media

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location_or_id, options = {}) ⇒ Media

Returns a new instance of Media.



10
11
12
13
14
15
16
17
18
# File 'lib/encoding_dot_com/media.rb', line 10

def initialize(location_or_id, options = {})
  if location_or_id.is_a?(Fixnum)
    @id = location_or_id
  else
    @source_location = URI.parse(location_or_id.to_s)
    @notify_url = options[:notify_url]
  end
  @formats = []
end

Instance Attribute Details

#formatsObject (readonly)

Returns the value of attribute formats.



8
9
10
# File 'lib/encoding_dot_com/media.rb', line 8

def formats
  @formats
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/encoding_dot_com/media.rb', line 8

def id
  @id
end

#notify_urlObject (readonly)

Returns the value of attribute notify_url.



8
9
10
# File 'lib/encoding_dot_com/media.rb', line 8

def notify_url
  @notify_url
end

#source_locationObject (readonly)

Returns the value of attribute source_location.



8
9
10
# File 'lib/encoding_dot_com/media.rb', line 8

def source_location
  @source_location
end

Class Method Details

.media_url_string(url) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/encoding_dot_com/media.rb', line 96

def self.media_url_string(url)
  if Connection.s3_key && Connection.s3_secret && url.host =~ /s3.amazonaws.com/
    url.user = ERB::Util.url_encode(Connection.s3_key)
    url.password = ERB::Util.url_encode(Connection.s3_secret)
  end
  url.to_s
end

Instance Method Details

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/encoding_dot_com/media.rb', line 32

def create
  request = to_xml('AddMediaBenchmark') { |xml|
    xml.source(self.class.media_url_string(@source_location))
    xml.notify(@notify_url) if @notify_url
    formats.each do |format|
      format.to_xml(:builder => xml)
    end
  }

  response = Connection.call(request)

  if response['message'] == 'Added' && response['MediaID']
    @id = response['MediaID'].to_i
    true
  else
    false
  end
end

#create_or_updateObject



28
29
30
# File 'lib/encoding_dot_com/media.rb', line 28

def create_or_update
  new_record? ? create : update
end

#infoObject



61
62
63
# File 'lib/encoding_dot_com/media.rb', line 61

def info
  Connection.call(to_xml('GetMediaInfo'))
end

#new_record?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/encoding_dot_com/media.rb', line 20

def new_record?
  @id.nil?
end

#saveObject



24
25
26
# File 'lib/encoding_dot_com/media.rb', line 24

def save
  create_or_update
end

#status(reload = false) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/encoding_dot_com/media.rb', line 65

def status(reload = false)
  if !@status || reload
    @status = Connection.call(to_xml('GetStatus'))
  end

  @status
end

#to_xml(action, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/encoding_dot_com/media.rb', line 73

def to_xml(action, options = {})
  xml = options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent])

  xml.instruct!
  xml.query do
    xml.userid(Connection.user_id)
    xml.userkey(Connection.user_key)
    xml.action(action)
    xml.mediaid(@id) if @id
    yield(xml) if block_given?
  end
end

#updateObject



51
52
53
54
55
56
57
58
59
# File 'lib/encoding_dot_com/media.rb', line 51

def update
  request = to_xml('UpdateMedia') { |xml|
    formats.each do |format|
      format.to_xml(:builder => xml)
    end
  }

  response = Connection.call(request)
end