streamio 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/HISTORY CHANGED
@@ -1,3 +1,11 @@
1
+ == 0.6.0 2011-06-17
2
+
3
+ * Model.create
4
+ * Model.destroy
5
+ * Model#reload
6
+ * Video #add_transcoding and #delete_transcoding now reloads the video instance
7
+ * Some refactoring behind the scenes
8
+
1
9
  == 0.5.1 2011-06-16
2
10
 
3
11
  * Added lots of (YARD style) documentation and updated README
@@ -1,23 +1,7 @@
1
1
  module Streamio
2
2
  class EncodingProfile < Model
3
- def self.resource
4
- RestClient::Resource.new("#{Streamio.authenticated_api_base}/encoding_profiles", :headers => {:accept => :json})
5
- end
6
-
7
- CREATEABLE_ATTRIBUTES = []
8
- ACCESSABLE_ATTRIBUTES = %w(title tags width desired_video_bitrate frame_rate audio_bitrate audio_sample_rate audio_channels)
9
- READABLE_ATTRIBUTES = %w(id created_at updated_at account_id)
10
-
11
- (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES + READABLE_ATTRIBUTES - CASTED_ATTRIBUTES).each do |attribute|
12
- define_method(attribute) do
13
- @attributes[attribute]
14
- end
15
- end
16
-
17
- (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES).each do |attribute|
18
- define_method("#{attribute}=") do |value|
19
- @attributes[attribute] = value
20
- end
21
- end
3
+ resource_name "encoding_profiles"
4
+ accessable_attributes %w(title tags width desired_video_bitrate frame_rate audio_bitrate audio_sample_rate audio_channels)
5
+ readable_attributes %w(id created_at updated_at account_id)
22
6
  end
23
7
  end
@@ -1,23 +1,8 @@
1
1
  module Streamio
2
2
  class Image < Model
3
- def self.resource
4
- RestClient::Resource.new("#{Streamio.authenticated_api_base}/images", :headers => {:accept => :json})
5
- end
6
-
7
- CREATEABLE_ATTRIBUTES = %w(file)
8
- ACCESSABLE_ATTRIBUTES = %w(title tags)
9
- READABLE_ATTRIBUTES = %w(id created_at updated_at account_id transcodings)
10
-
11
- (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES + READABLE_ATTRIBUTES - CASTED_ATTRIBUTES).each do |attribute|
12
- define_method(attribute) do
13
- @attributes[attribute]
14
- end
15
- end
16
-
17
- (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES).each do |attribute|
18
- define_method("#{attribute}=") do |value|
19
- @attributes[attribute] = value
20
- end
21
- end
3
+ resource_name "images"
4
+ creatable_attributes %w(file)
5
+ accessable_attributes %w(title tags)
6
+ readable_attributes %w(id created_at updated_at account_id transcodings)
22
7
  end
23
8
  end
@@ -22,7 +22,74 @@ module Streamio
22
22
  parse_response(resource.get(:params => parameters))
23
23
  end
24
24
 
25
+ # Deletes model with the given id. Raises exception if failing to do so.
26
+ #
27
+ # @param [String] id The id of the model to delete.
28
+ #
29
+ # @return [Boolean] True if the delete request is successful.
30
+ def destroy(id)
31
+ resource[id].delete
32
+ true
33
+ end
34
+
35
+ # Initializes a new model instance with the given attributes, saves it
36
+ # and returns it.
37
+ #
38
+ # @param [Hash] attributes The attributes to initialize the model with.
39
+ #
40
+ # @return [Model] The model after save has been called on it.
41
+ def create(attributes = {})
42
+ model = new(attributes)
43
+ model.save
44
+ model
45
+ end
46
+
47
+ def resource_name(name)
48
+ define_singleton_method(:resource) do
49
+ RestClient::Resource.new("#{Streamio.authenticated_api_base}/#{name}", :headers => {:accept => :json})
50
+ end
51
+ end
52
+
53
+ def creatable_attributes(attributes = nil)
54
+ return @creatable_attributes ||= [] if attributes.nil?
55
+
56
+ @creatable_attributes = attributes
57
+ create_getters(attributes)
58
+ create_setters(attributes)
59
+ end
60
+
61
+ def accessable_attributes(attributes = nil)
62
+ return @accessable_attributes ||= [] if attributes.nil?
63
+
64
+ @accessable_attributes = attributes
65
+ create_getters(attributes)
66
+ create_setters(attributes)
67
+ end
68
+
69
+ def readable_attributes(attributes = nil)
70
+ return @readable_attributes ||= [] if attributes.nil?
71
+
72
+ @readable_attributes = attributes
73
+ create_getters(attributes)
74
+ end
75
+
25
76
  protected
77
+ def create_getters(attributes)
78
+ (attributes - CASTED_ATTRIBUTES).each do |attribute|
79
+ define_method(attribute) do
80
+ @attributes[attribute]
81
+ end
82
+ end
83
+ end
84
+
85
+ def create_setters(attributes)
86
+ attributes.each do |attribute|
87
+ define_method("#{attribute}=") do |value|
88
+ @attributes[attribute] = value
89
+ end
90
+ end
91
+ end
92
+
26
93
  def parse_response(response)
27
94
  response = JSON.parse(response.body)
28
95
  if response.instance_of?(Array)
@@ -88,6 +155,15 @@ module Streamio
88
155
  true
89
156
  end
90
157
 
158
+ # Update the model instance with the current state on the remote.
159
+ #
160
+ # @return [Model] Returns itself.
161
+ def reload
162
+ remote = self.class.find(id)
163
+ @attributes = remote.attributes
164
+ self
165
+ end
166
+
91
167
  # @return [Boolean] True if the record is persisted.
92
168
  def persisted?
93
169
  !destroyed? && !id.nil?
@@ -119,7 +195,7 @@ module Streamio
119
195
  private
120
196
  def update
121
197
  parameters = {}
122
- (self.class::ACCESSABLE_ATTRIBUTES).each do |key|
198
+ (self.class.accessable_attributes).each do |key|
123
199
  parameters[key] = @attributes[key] if @attributes.has_key?(key)
124
200
  end
125
201
 
@@ -129,13 +205,13 @@ module Streamio
129
205
 
130
206
  def persist
131
207
  parameters = {}
132
- (self.class::CREATEABLE_ATTRIBUTES + self.class::ACCESSABLE_ATTRIBUTES).each do |key|
208
+ (self.class.creatable_attributes + self.class.accessable_attributes).each do |key|
133
209
  parameters[key] = @attributes[key] if @attributes.has_key?(key)
134
210
  end
135
211
 
136
212
  new_attributes = JSON.parse(self.class.resource.post(attributes).body)
137
213
 
138
- (self.class::ACCESSABLE_ATTRIBUTES + self.class::READABLE_ATTRIBUTES).each do |attribute|
214
+ (self.class.accessable_attributes + self.class.readable_attributes).each do |attribute|
139
215
  @attributes[attribute] = new_attributes[attribute]
140
216
  end
141
217
  true
@@ -1,3 +1,3 @@
1
1
  module Streamio
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -1,26 +1,12 @@
1
1
  module Streamio
2
2
  class Video < Model
3
- def self.resource
4
- RestClient::Resource.new("#{Streamio.authenticated_api_base}/videos", :headers => {:accept => :json})
5
- end
6
-
7
- CREATEABLE_ATTRIBUTES = %w(file encoding_profile_ids encoding_profile_tags skip_default_encoding_profiles use_original_as_transcoding)
8
- ACCESSABLE_ATTRIBUTES = %w(title description tags image_id)
9
- READABLE_ATTRIBUTES = %w(id state progress aspect_ratio_multiplier plays duration created_at updated_at account_id transcodings)
10
-
11
- (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES + READABLE_ATTRIBUTES - CASTED_ATTRIBUTES).each do |attribute|
12
- define_method(attribute) do
13
- @attributes[attribute]
14
- end
15
- end
16
-
17
- (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES).each do |attribute|
18
- define_method("#{attribute}=") do |value|
19
- @attributes[attribute] = value
20
- end
21
- end
3
+ resource_name "videos"
4
+ creatable_attributes %w(file encoding_profile_ids encoding_profile_tags skip_default_encoding_profiles use_original_as_transcoding)
5
+ accessable_attributes %w(title description tags image_id)
6
+ readable_attributes %w(id state progress aspect_ratio_multiplier plays duration created_at updated_at account_id transcodings)
22
7
 
23
- # Adds a transcoding to the video instance.
8
+ # Adds a transcoding to the video instance and reloads itself to
9
+ # reflect the changed transcodings array.
24
10
  #
25
11
  # @param [Hash] parameters The parameters to pass in when creating the transcoding.
26
12
  #
@@ -29,18 +15,21 @@ module Streamio
29
15
  # @return [Boolean] Indicating wether the transcoding was successfully created.
30
16
  def add_transcoding(parameters)
31
17
  self.class.resource["#{id}/transcodings"].post(parameters)
18
+ reload
32
19
  true
33
20
  rescue RestClient::Exception
34
21
  false
35
22
  end
36
23
 
37
- # Deletes a transcoding from the video.
24
+ # Deletes a transcoding from the video and reloads itself to
25
+ # reflect the changed transcodings array.
38
26
  #
39
27
  # @param [String] transcoding_id The id of the transcoding to be deleted.
40
28
  #
41
29
  # @return [Boolean] Indicating wether the transcoding was successfully deleted.
42
30
  def delete_transcoding(transcoding_id)
43
31
  self.class.resource["#{id}/transcodings/#{transcoding_id}"].delete
32
+ reload
44
33
  true
45
34
  rescue RestClient::Exception
46
35
  false
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: streamio
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.1
5
+ version: 0.6.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - David Backeus
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-16 00:00:00 Z
13
+ date: 2011-06-17 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client