Class: Api::HostsController

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

Instance Method Summary collapse

Methods inherited from ApiController

#probe

Instance Method Details

#createObject

Creates a host

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

Parameters

name

The name of the host

url

The url of the host

Response codes

success

201 created

failed

406 Unprocessable Entity

Example

$ curl -d 'name=transcoder&url=http://transcoder.com' http://localhost:3000/api/hosts

{
  "host": {
    "available":true,
    "available_slots":10,
    "created_at":"2011-05-09T15:32:53Z",
    "id":6,
     "name":"transcoder",
     "total_slots":10,
     "updated_at":"2011-05-09T15:32:53Z",
     "url":"http://transcoder.com"
  }
}

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/api/hosts_controller.rb', line 58

def create
  host = Host.from_api(params)

  if host.valid?
    respond_with host, location: api_host_url(host) do |format|
      format.html { redirect_to hosts_path }
    end
  else
    respond_with host do |format|
      format.html do
        @host = host
        render '/hosts/new'
      end
    end
  end
end

#destroyObject

Deletes a host

Parameters

id

Id of the host to delete

Example

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

145
146
147
148
149
150
151
# File 'app/controllers/api/hosts_controller.rb', line 145

def destroy
  host = Host.find(params[:id])
  host.destroy
  respond_with host do |format|
    format.html { redirect_to hosts_path }
  end
end

#indexObject

Displays a list of hosts

All returned hosts will ahve their statuses updated to provide an up-to-date view of their slots.

Example

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

[
   {"host":{
     "available":true,
     "available_slots":1,
     "created_at":"2011-05-09T11:59:52Z",
     "id":1,
     "name":"Localhost",
     "total_slots":1,
     "updated_at":"2011-05-09T13:53:31Z",
     "url":"http://127.0.0.1:8080"}
   }
 ]

25
26
27
28
29
# File 'app/controllers/api/hosts_controller.rb', line 25

def index
  hosts = Host.all
  hosts.map(&:update_status)
  respond_with hosts
end

#showObject

Shows a specific host

The host will have its status updated to provide an up-to-date view of the slots.

Parameters

id

The id of the host to show

Example

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

{
   "host":{
     "available":true,
     "available_slots":1,
     "created_at":"2011-05-09T11:59:52Z",
     "id":1,
     "name":"Localhost",
     "total_slots":1,
     "updated_at":"2011-05-09T13:53:31Z",
     "url":"http://127.0.0.1:8080"}
   }

96
97
98
99
100
# File 'app/controllers/api/hosts_controller.rb', line 96

def show
  host = Host.find(params[:id])
  host.update_status
  respond_with host
end

#updateObject

Updates a host

Paramdeters

id

Id of the host to update

name

Name of the host

url

Url of the host

Example

$ curl -XPUT -d 'name=h&url=foo.com' http://localhost:3000/api/hosts/1
{} # HTTP Status: 200 OK

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/controllers/api/hosts_controller.rb', line 113

def update
  if params[:host]
    params[:name] = params[:host][:name]
    params[:url]  = params[:host][:url]
  end

  host = Host.find(params[:id])

  if host.update_attributes(name: params[:name], url: params[:url])
    host.update_status
    respond_with host, location: api_host_url(host) do |format|
      format.html { redirect_to hosts_path }
    end
  else
    respond_with host do |format|
      format.html do
        @host = host
        render '/hosts/edit'
      end
    end
  end
end