Class: Streamio::Model

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

Direct Known Subclasses

Audio, EncodingProfile, Image, Player, Playlist, Upload, Video

Constant Summary collapse

CASTED_ATTRIBUTES =
%w(tags created_at updated_at)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes you wish to apply to the new Model instance.



136
137
138
139
140
141
142
# File 'lib/streamio/model.rb', line 136

def initialize(attributes = {})
  @errors = {}
  @attributes = attributes.inject(Hash.new) do |options, (key, value)|
    options[key.to_s] = value
    options
  end
end

Instance Attribute Details

#attributesObject (readonly)

A Hash resulting from parsing the JSON returned from Streamios API.



130
131
132
# File 'lib/streamio/model.rb', line 130

def attributes
  @attributes
end

#errorsObject (readonly)

A Hash containing validation errors after a failed save.



133
134
135
# File 'lib/streamio/model.rb', line 133

def errors
  @errors
end

Class Method Details

.accessable_attributes(attributes = nil) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/streamio/model.rb', line 77

def accessable_attributes(attributes = nil)
  return @accessable_attributes ||= [] if attributes.nil?
  
  @accessable_attributes = attributes
  create_getters(attributes)
  create_setters(attributes)
end

.all(parameters = {}) ⇒ Array

Querys for a list of models.

for the query. Refer to Streamio API reference for a list of valid parameters for each of the different models availible.

Parameters:

  • parameters (Hash) (defaults to: {})

    The parameters will determine the conditions

Returns:

  • (Array)

    Array of found models.



20
21
22
23
24
# File 'lib/streamio/model.rb', line 20

def all(parameters = {})
  sanitize_parameters(parameters)
  response = resource.get(nil, parameters)
  parse_response(response)
end

.count(parameters = {}) ⇒ Integer

Returns a count of number of models on your account filtered on the parameters your specify.

for the count. Refer to Streamio API reference for a list of valid parameters for each of the different models availible.

Parameters:

  • parameters (Hash) (defaults to: {})

    The parameters will determine the conditions

Returns:

  • (Integer)

    The number of models found.



55
56
57
58
59
# File 'lib/streamio/model.rb', line 55

def count(parameters = {})
  sanitize_parameters(parameters)
  response = resource.get("count", parameters)
  MultiJson.decode(response.body)["count"]
end

.creatable_attributes(attributes = nil) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/streamio/model.rb', line 69

def creatable_attributes(attributes = nil)
  return @creatable_attributes ||= [] if attributes.nil?
  
  @creatable_attributes = attributes
  create_getters(attributes)
  create_setters(attributes)
end

.create(attributes = {}) ⇒ Model

Initializes a new model instance with the given attributes, saves it and returns it.

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to initialize the model with.

Returns:

  • (Model)

    The model after save has been called on it.



42
43
44
45
46
# File 'lib/streamio/model.rb', line 42

def create(attributes = {})
  model = new(attributes)
  model.save
  model
end

.destroy(id) ⇒ Boolean

Deletes model with the given id. Raises exception if failing to do so.

Parameters:

  • id (String)

    The id of the model to delete.

Returns:

  • (Boolean)

    True if the delete request is successful.



31
32
33
34
# File 'lib/streamio/model.rb', line 31

def destroy(id)
  resource.delete(id)
  true
end

.find(id) ⇒ Model

Gets a model by its id.

Parameters:

  • id (String)

    The models id.

Returns:

  • (Model)

    The found model.



9
10
11
# File 'lib/streamio/model.rb', line 9

def find(id)
  parse_response(resource.get(id))
end

.readable_attributes(attributes = nil) ⇒ Object



85
86
87
88
89
90
# File 'lib/streamio/model.rb', line 85

def readable_attributes(attributes = nil)
  return @readable_attributes ||= [] if attributes.nil?
  
  @readable_attributes = attributes
  create_getters(attributes)
end

.resourceObject



65
66
67
# File 'lib/streamio/model.rb', line 65

def resource
  Resource.new(@resource_name)
end

.resource_name(name) ⇒ Object



61
62
63
# File 'lib/streamio/model.rb', line 61

def resource_name(name)
  @resource_name = name
end

Instance Method Details

#created_atTime

Returns When the record was created.

Returns:

  • (Time)

    When the record was created.



197
198
199
200
# File 'lib/streamio/model.rb', line 197

def created_at
  return nil unless @attributes["created_at"]
  Time.parse(@attributes["created_at"])
end

#destroyBoolean

Deletes the record and freezes this instance to reflect that no changes should be made (since they can’t be persisted).

Returns:

  • (Boolean)

    True if the record was deleted.



165
166
167
168
169
# File 'lib/streamio/model.rb', line 165

def destroy
  self.class.resource.delete(id)
  @attributes.freeze
  true
end

#destroyed?Boolean

Returns True if you destroyed this record.

Returns:

  • (Boolean)

    True if you destroyed this record.



186
187
188
# File 'lib/streamio/model.rb', line 186

def destroyed?
  @attributes.frozen?
end

#persisted?Boolean

Returns True if the record is persisted.

Returns:

  • (Boolean)

    True if the record is persisted.



181
182
183
# File 'lib/streamio/model.rb', line 181

def persisted?
  !destroyed? && !id.nil?
end

#reloadModel

Update the model instance with the current state on the remote.

Returns:

  • (Model)

    Returns itself.



174
175
176
177
178
# File 'lib/streamio/model.rb', line 174

def reload
  remote = self.class.find(id)
  @attributes = remote.attributes
  self
end

#saveBoolean

Saves the model.

If the model is new a record gets created, otherwise the existing record gets updated.

If save fails it might be due to validation errors so you might want to check the models errors.

Returns:

  • (Boolean)

    Indicating if the save / update was successful.



153
154
155
156
157
158
159
# File 'lib/streamio/model.rb', line 153

def save
  if persisted?
    update
  else  
    persist
  end
end

#tagsArray

Returns Array of tags applied to the record.

Returns:

  • (Array)

    Array of tags applied to the record.



191
192
193
194
# File 'lib/streamio/model.rb', line 191

def tags
  @attributes["tags"] = [] if @attributes["tags"].nil?
  @attributes["tags"]
end

#updated_atTime

Returns When the record was last updated.

Returns:

  • (Time)

    When the record was last updated.



203
204
205
206
# File 'lib/streamio/model.rb', line 203

def updated_at
  return nil unless @attributes["updated_at"]
  Time.parse(@attributes["updated_at"])
end