Class: Api::PresetsController

Inherits:
ApiController show all
Defined in:
app/controllers/api/presets_controller.rb

Instance Method Summary collapse

Methods inherited from ApiController

#probe

Instance Method Details

#createObject

Creates a preset

Creates a preset using the specified parameters, which are all required. If the request was valid, the created preset is returned. If the request could not be completed, a list of errors will be returned.

Parameters

Required:

name

Name of the preset

params

Parameters to use

Optional:

thumbnail_options

Thumbnail options to use

Response codes

success

201 created

failed

406 Unprocessable Entity

Example

$ curl -d 'name=webm&parameters=params' http://localhost:3000/api/presets

{"preset":{
  "created_at":"2011-05-10T14:44:07Z",
  "id":3,
  "name":"webm",
  "parameters":"params",
  "thumbnail_options":null,
  "updated_at":"2011-05-10T14:44:07Z"}
}

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/api/presets_controller.rb', line 52

def create
  preset = Preset.from_api(params)

  if preset.valid?
    respond_with preset, location: api_preset_url(preset) do |format|
      format.html { redirect_to presets_path }
    end
  else
    respond_with preset do |format|
      format.html do
        @preset = preset
        render '/presets/new'
      end
    end
  end
end

#destroyObject

Deletes a preset

Parameters

id

Id of the preset to delete

Example

$ curl -XDELETE http://localhost:3000/api/presets/1
{} # HTTP Status: 200 OK

133
134
135
136
137
138
139
# File 'app/controllers/api/presets_controller.rb', line 133

def destroy
  preset = Preset.find(params[:id])
  preset.destroy
  respond_with preset do |format|
    format.html { redirect_to presets_path }
  end
end

#indexObject

Returns a list of presets

Example:

$ curl http://localhost:3000/api/presets

[
  {"preset":{
    "created_at":"2011-05-09T11:59:53Z",
    "id":1,
    "name":"h264",
    "parameters":"-acodec libfaac -ab 96k -ar 44100 -vcodec libx264 -vb 416k -vpre slow -vpre baseline -s 320x180 -y",
    "thumbnail_options":null,
    "updated_at":"2011-05-09T11:59:53Z"}
  }
]

21
22
23
# File 'app/controllers/api/presets_controller.rb', line 21

def index
  respond_with Preset.all
end

#showObject

Displays a preset

Parameters

id

Id of the preset to display

Example

$ curl http://localhost:3000/api/presets/1

{"preset":{
  "created_at":"2011-05-10T14:44:07Z",
  "id":3,
  "name":"webm",
  "parameters":"params",
  "thumbnail_options":null,
  "updated_at":"2011-05-10T14:44:07Z"}
}

85
86
87
# File 'app/controllers/api/presets_controller.rb', line 85

def show
  respond_with Preset.find(params[:id])
end

#updateObject

Updates a preset

Parameters

id

Id of the preset to update

name

Name of the preset

parameters

Parameters of the preset

thumbnail_options

Thumbnail options

Example

$ curl -XPUT -d 'name=h264&parameters=params' http://localhost:3000/api/presets/1
{} # HTTP Status: 200 OK

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/api/presets_controller.rb', line 101

def update
  if params[:preset]
    params[:name] = params[:preset][:name]
    params[:parameters] = params[:preset][:parameters]
    params[:thumbnail_options] = params[:preset][:thumbnail_options]
  end

  preset = Preset.find(params[:id])

  if preset.update_attributes(name: params[:name], parameters: params[:parameters], thumbnail_options: params[:thumbnail_options])
    respond_with preset, location: api_preset_url(preset) do |format|
      format.html { redirect_to presets_path }
    end
  else
    respond_with preset do |format|
      format.html do
        @preset = preset
        render '/presets/edit'
      end
    end
  end
end