bbrowning-deltacloud-core 0.0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +502 -0
- data/Rakefile +85 -0
- data/bin/deltacloudd +88 -0
- data/config.ru +5 -0
- data/deltacloud.rb +14 -0
- data/lib/deltacloud/base_driver.rb +19 -0
- data/lib/deltacloud/base_driver/base_driver.rb +189 -0
- data/lib/deltacloud/base_driver/features.rb +159 -0
- data/lib/deltacloud/base_driver/mock_driver.rb +37 -0
- data/lib/deltacloud/drivers/ec2/ec2_driver.rb +340 -0
- data/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb +170 -0
- data/lib/deltacloud/drivers/gogrid/gogrid_client.rb +45 -0
- data/lib/deltacloud/drivers/gogrid/gogrid_driver.rb +322 -0
- data/lib/deltacloud/drivers/mock/mock_driver.rb +275 -0
- data/lib/deltacloud/drivers/opennebula/cloud_client.rb +116 -0
- data/lib/deltacloud/drivers/opennebula/occi_client.rb +204 -0
- data/lib/deltacloud/drivers/opennebula/opennebula_driver.rb +241 -0
- data/lib/deltacloud/drivers/rackspace/rackspace_client.rb +129 -0
- data/lib/deltacloud/drivers/rackspace/rackspace_driver.rb +150 -0
- data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +254 -0
- data/lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb +84 -0
- data/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb +144 -0
- data/lib/deltacloud/drivers/terremark/terremark_driver.rb +261 -0
- data/lib/deltacloud/hardware_profile.rb +153 -0
- data/lib/deltacloud/helpers.rb +5 -0
- data/lib/deltacloud/helpers/application_helper.rb +56 -0
- data/lib/deltacloud/helpers/conversion_helper.rb +38 -0
- data/lib/deltacloud/helpers/hardware_profiles_helper.rb +35 -0
- data/lib/deltacloud/method_serializer.rb +84 -0
- data/lib/deltacloud/models/base_model.rb +58 -0
- data/lib/deltacloud/models/image.rb +26 -0
- data/lib/deltacloud/models/instance.rb +37 -0
- data/lib/deltacloud/models/instance_profile.rb +47 -0
- data/lib/deltacloud/models/realm.rb +25 -0
- data/lib/deltacloud/models/storage_snapshot.rb +26 -0
- data/lib/deltacloud/models/storage_volume.rb +27 -0
- data/lib/deltacloud/state_machine.rb +84 -0
- data/lib/deltacloud/validation.rb +70 -0
- data/lib/drivers.rb +38 -0
- data/lib/sinatra/accept_media_types.rb +128 -0
- data/lib/sinatra/lazy_auth.rb +56 -0
- data/lib/sinatra/rabbit.rb +272 -0
- data/lib/sinatra/respond_to.rb +269 -0
- data/lib/sinatra/static_assets.rb +83 -0
- data/lib/sinatra/url_for.rb +51 -0
- data/public/favicon.ico +0 -0
- data/public/images/grid.png +0 -0
- data/public/images/logo-wide.png +0 -0
- data/public/images/rails.png +0 -0
- data/public/images/topbar-bg.png +0 -0
- data/public/javascripts/application.js +32 -0
- data/public/javascripts/jquery-1.4.2.min.js +154 -0
- data/public/stylesheets/compiled/application.css +613 -0
- data/public/stylesheets/compiled/ie.css +31 -0
- data/public/stylesheets/compiled/print.css +27 -0
- data/public/stylesheets/compiled/screen.css +456 -0
- data/server.rb +342 -0
- data/support/fedora/deltacloudd +68 -0
- data/support/fedora/rubygem-deltacloud-core.spec +91 -0
- data/tests/deltacloud_test.rb +60 -0
- data/tests/images_test.rb +94 -0
- data/tests/instances_test.rb +136 -0
- data/tests/realms_test.rb +56 -0
- data/tests/storage_snapshots_test.rb +48 -0
- data/tests/storage_volumes_test.rb +48 -0
- data/torquebox-ec2-config.ru +8 -0
- data/views/accounts/index.html.haml +11 -0
- data/views/accounts/show.html.haml +30 -0
- data/views/api/show.html.haml +15 -0
- data/views/api/show.xml.haml +5 -0
- data/views/docs/collection.html.haml +37 -0
- data/views/docs/collection.xml.haml +14 -0
- data/views/docs/index.html.haml +15 -0
- data/views/docs/index.xml.haml +5 -0
- data/views/docs/operation.html.haml +31 -0
- data/views/docs/operation.xml.haml +10 -0
- data/views/errors/auth_exception.html.haml +8 -0
- data/views/errors/auth_exception.xml.haml +2 -0
- data/views/errors/backend_error.html.haml +17 -0
- data/views/errors/backend_error.xml.haml +8 -0
- data/views/errors/validation_failure.html.haml +11 -0
- data/views/errors/validation_failure.xml.haml +7 -0
- data/views/hardware_profiles/index.html.haml +25 -0
- data/views/hardware_profiles/index.xml.haml +4 -0
- data/views/hardware_profiles/show.html.haml +19 -0
- data/views/hardware_profiles/show.xml.haml +17 -0
- data/views/images/index.html.haml +30 -0
- data/views/images/index.xml.haml +7 -0
- data/views/images/show.html.haml +21 -0
- data/views/images/show.xml.haml +5 -0
- data/views/instance_states/show.gv.erb +45 -0
- data/views/instance_states/show.html.haml +31 -0
- data/views/instance_states/show.xml.haml +8 -0
- data/views/instances/index.html.haml +30 -0
- data/views/instances/index.xml.haml +23 -0
- data/views/instances/new.html.haml +55 -0
- data/views/instances/show.html.haml +43 -0
- data/views/instances/show.xml.haml +41 -0
- data/views/layout.html.haml +26 -0
- data/views/realms/index.html.haml +29 -0
- data/views/realms/index.xml.haml +12 -0
- data/views/realms/show.html.haml +15 -0
- data/views/realms/show.xml.haml +10 -0
- data/views/root/index.html.haml +4 -0
- data/views/storage_snapshots/index.html.haml +20 -0
- data/views/storage_snapshots/index.xml.haml +11 -0
- data/views/storage_snapshots/show.html.haml +14 -0
- data/views/storage_snapshots/show.xml.haml +9 -0
- data/views/storage_volumes/index.html.haml +21 -0
- data/views/storage_volumes/index.xml.haml +13 -0
- data/views/storage_volumes/show.html.haml +20 -0
- data/views/storage_volumes/show.xml.haml +13 -0
- metadata +361 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2009 Red Hat, Inc.
|
3
|
+
#
|
4
|
+
# This library is free software; you can redistribute it and/or
|
5
|
+
# modify it under the terms of the GNU Lesser General Public
|
6
|
+
# License as published by the Free Software Foundation; either
|
7
|
+
# version 2.1 of the License, or (at your option) any later version.
|
8
|
+
#
|
9
|
+
# This library is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
12
|
+
# Lesser General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU Lesser General Public
|
15
|
+
# License along with this library; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
17
|
+
|
18
|
+
|
19
|
+
class Instance < BaseModel
|
20
|
+
|
21
|
+
attr_accessor :owner_id
|
22
|
+
attr_accessor :image_id
|
23
|
+
attr_accessor :name
|
24
|
+
attr_accessor :realm_id
|
25
|
+
attr_accessor :state
|
26
|
+
attr_accessor :actions
|
27
|
+
attr_accessor :public_addresses
|
28
|
+
attr_accessor :private_addresses
|
29
|
+
attr_accessor :instance_profile
|
30
|
+
|
31
|
+
def initialize(init=nil)
|
32
|
+
super(init)
|
33
|
+
self.actions = [] if self.actions.nil?
|
34
|
+
self.public_addresses = [] if self.public_addresses.nil?
|
35
|
+
self.private_addresses = [] if self.private_addresses.nil?
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2009 Red Hat, Inc.
|
3
|
+
#
|
4
|
+
# This library is free software; you can redistribute it and/or
|
5
|
+
# modify it under the terms of the GNU Lesser General Public
|
6
|
+
# License as published by the Free Software Foundation; either
|
7
|
+
# version 2.1 of the License, or (at your option) any later version.
|
8
|
+
#
|
9
|
+
# This library is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
12
|
+
# Lesser General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU Lesser General Public
|
15
|
+
# License along with this library; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
17
|
+
|
18
|
+
# Model to store the hardware profile applied to an instance together with
|
19
|
+
# any instance-specific overrides
|
20
|
+
class InstanceProfile < BaseModel
|
21
|
+
attr_accessor :memory
|
22
|
+
attr_accessor :storage
|
23
|
+
attr_accessor :architecture
|
24
|
+
attr_accessor :cpu
|
25
|
+
|
26
|
+
def initialize(hwp_name, args = {})
|
27
|
+
opts = args.inject({ :id => hwp_name.to_s }) do |m, e|
|
28
|
+
k, v = e
|
29
|
+
m[$1] = v if k.to_s =~ /^hwp_(.*)$/
|
30
|
+
m
|
31
|
+
end
|
32
|
+
super(opts)
|
33
|
+
end
|
34
|
+
|
35
|
+
def name
|
36
|
+
id
|
37
|
+
end
|
38
|
+
|
39
|
+
def overrides
|
40
|
+
[:memory, :storage, :architecture, :cpu].inject({}) do |h, p|
|
41
|
+
if v = instance_variable_get("@#{p}")
|
42
|
+
h[p] = v
|
43
|
+
end
|
44
|
+
h
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2009 Red Hat, Inc.
|
3
|
+
#
|
4
|
+
# This library is free software; you can redistribute it and/or
|
5
|
+
# modify it under the terms of the GNU Lesser General Public
|
6
|
+
# License as published by the Free Software Foundation; either
|
7
|
+
# version 2.1 of the License, or (at your option) any later version.
|
8
|
+
#
|
9
|
+
# This library is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
12
|
+
# Lesser General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU Lesser General Public
|
15
|
+
# License along with this library; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
17
|
+
|
18
|
+
|
19
|
+
class Realm < BaseModel
|
20
|
+
|
21
|
+
attr_accessor :name
|
22
|
+
attr_accessor :limit
|
23
|
+
attr_accessor :state
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2009 Red Hat, Inc.
|
3
|
+
#
|
4
|
+
# This library is free software; you can redistribute it and/or
|
5
|
+
# modify it under the terms of the GNU Lesser General Public
|
6
|
+
# License as published by the Free Software Foundation; either
|
7
|
+
# version 2.1 of the License, or (at your option) any later version.
|
8
|
+
#
|
9
|
+
# This library is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
12
|
+
# Lesser General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU Lesser General Public
|
15
|
+
# License along with this library; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
class StorageSnapshot < BaseModel
|
21
|
+
|
22
|
+
attr_accessor :state
|
23
|
+
attr_accessor :storage_volume_id
|
24
|
+
attr_accessor :created
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2009 Red Hat, Inc.
|
3
|
+
#
|
4
|
+
# This library is free software; you can redistribute it and/or
|
5
|
+
# modify it under the terms of the GNU Lesser General Public
|
6
|
+
# License as published by the Free Software Foundation; either
|
7
|
+
# version 2.1 of the License, or (at your option) any later version.
|
8
|
+
#
|
9
|
+
# This library is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
12
|
+
# Lesser General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU Lesser General Public
|
15
|
+
# License along with this library; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
17
|
+
|
18
|
+
|
19
|
+
class StorageVolume < BaseModel
|
20
|
+
|
21
|
+
attr_accessor :created
|
22
|
+
attr_accessor :state
|
23
|
+
attr_accessor :capacity
|
24
|
+
attr_accessor :instance_id
|
25
|
+
attr_accessor :device
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
|
2
|
+
module Deltacloud
|
3
|
+
class StateMachine
|
4
|
+
|
5
|
+
attr_reader :states
|
6
|
+
def initialize(&block)
|
7
|
+
@states = []
|
8
|
+
instance_eval &block if block
|
9
|
+
end
|
10
|
+
|
11
|
+
def start()
|
12
|
+
state(:start)
|
13
|
+
end
|
14
|
+
|
15
|
+
def finish()
|
16
|
+
state(:finish)
|
17
|
+
end
|
18
|
+
|
19
|
+
def state(name)
|
20
|
+
state = @states.find{|e| e.name == name.to_sym}
|
21
|
+
if ( state.nil? )
|
22
|
+
state = State.new( self, name.to_sym )
|
23
|
+
@states << state
|
24
|
+
end
|
25
|
+
state
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing(sym,*args)
|
29
|
+
return state( sym ) if ( args.empty? )
|
30
|
+
super( sym, *args )
|
31
|
+
end
|
32
|
+
|
33
|
+
class State
|
34
|
+
|
35
|
+
attr_reader :name
|
36
|
+
attr_reader :transitions
|
37
|
+
|
38
|
+
def initialize(machine, name)
|
39
|
+
@machine = machine
|
40
|
+
@name = name
|
41
|
+
@transitions = []
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_s
|
45
|
+
self.name.to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
def to(destination_name)
|
49
|
+
destination = @machine.state(destination_name)
|
50
|
+
transition = Transition.new( @machine, destination )
|
51
|
+
@transitions << transition
|
52
|
+
transition
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
class Transition
|
58
|
+
|
59
|
+
attr_reader :destination
|
60
|
+
attr_reader :action
|
61
|
+
|
62
|
+
def initialize(machine, destination)
|
63
|
+
@machine = machine
|
64
|
+
@destination = destination
|
65
|
+
@auto = false
|
66
|
+
@action = nil
|
67
|
+
end
|
68
|
+
|
69
|
+
def automatically
|
70
|
+
@auto = true
|
71
|
+
end
|
72
|
+
|
73
|
+
def automatically?
|
74
|
+
@auto
|
75
|
+
end
|
76
|
+
|
77
|
+
def on(action)
|
78
|
+
@action = action
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Deltacloud::Validation
|
2
|
+
|
3
|
+
class Failure < StandardError
|
4
|
+
attr_reader :param
|
5
|
+
def initialize(param, msg='')
|
6
|
+
super(msg)
|
7
|
+
@param = param
|
8
|
+
end
|
9
|
+
|
10
|
+
def name
|
11
|
+
param.name
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Param
|
16
|
+
attr_reader :name, :klass, :type, :options, :description
|
17
|
+
|
18
|
+
def initialize(args)
|
19
|
+
@name = args[0]
|
20
|
+
@klass = args[1] || :string
|
21
|
+
@type = args[2] || :optional
|
22
|
+
@options = args[3] || []
|
23
|
+
@description = args[4] || ''
|
24
|
+
end
|
25
|
+
|
26
|
+
def required?
|
27
|
+
type.eql?(:required)
|
28
|
+
end
|
29
|
+
|
30
|
+
def optional?
|
31
|
+
type.eql?(:optional)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def param(*args)
|
36
|
+
raise DuplicateParamException if params[args[0]]
|
37
|
+
p = Param.new(args)
|
38
|
+
params[p.name] = p
|
39
|
+
end
|
40
|
+
|
41
|
+
def params
|
42
|
+
@params ||= {}
|
43
|
+
@params
|
44
|
+
end
|
45
|
+
|
46
|
+
# Add the parameters in hash +new+ to already existing parameters. If
|
47
|
+
# +new+ contains a parameter with an already existing name, the old
|
48
|
+
# definition is clobbered.
|
49
|
+
def add_params(new)
|
50
|
+
# We do not check for duplication on purpose: multiple calls
|
51
|
+
# to add_params should be cumulative
|
52
|
+
new.each { |p| @params[p.name] = p }
|
53
|
+
end
|
54
|
+
|
55
|
+
def each_param(&block)
|
56
|
+
params.each_value { |p| yield p }
|
57
|
+
end
|
58
|
+
|
59
|
+
def validate(values)
|
60
|
+
each_param do |p|
|
61
|
+
if p.required? and not values[p.name]
|
62
|
+
raise Failure.new(p, "Required parameter #{p.name} not found")
|
63
|
+
end
|
64
|
+
if values[p.name] and not p.options.empty? and
|
65
|
+
not p.options.include?(values[p.name])
|
66
|
+
raise Failure.new(p, "Parameter #{p.name} has value #{values[p.name]} which is not in #{p.options.join(", ")}")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/drivers.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
DRIVERS = {
|
2
|
+
:ec2 => { :name => "EC2" },
|
3
|
+
:rackspace => { :name => "Rackspace" },
|
4
|
+
:gogrid => { :name => "Gogrid" },
|
5
|
+
:rhevm => { :name => "RHEVM" },
|
6
|
+
:rimuhosting => { :name => "RimuHosting"},
|
7
|
+
:opennebula => { :name => "Opennebula", :class => "OpennebulaDriver" },
|
8
|
+
:terremark => { :name => "Terremark"},
|
9
|
+
:mock => { :name => "Mock" }
|
10
|
+
}
|
11
|
+
|
12
|
+
def driver_name
|
13
|
+
DRIVERS[DRIVER][:name]
|
14
|
+
end
|
15
|
+
|
16
|
+
def driver_class_name
|
17
|
+
basename = DRIVERS[DRIVER][:class] || "#{driver_name}Driver"
|
18
|
+
"Deltacloud::Drivers::#{driver_name}::#{basename}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def driver_source_name
|
22
|
+
File.join("deltacloud", "drivers", "#{DRIVER}", "#{DRIVER}_driver.rb")
|
23
|
+
end
|
24
|
+
|
25
|
+
def driver_mock_source_name
|
26
|
+
return File.join('deltacloud', 'drivers', DRIVER.to_s, "#{DRIVER}_driver.rb") if driver_name.eql? 'Mock'
|
27
|
+
end
|
28
|
+
|
29
|
+
def driver
|
30
|
+
require driver_source_name
|
31
|
+
#require 'deltacloud/base_driver/mock_driver.rb'
|
32
|
+
|
33
|
+
if Sinatra::Application.environment.eql? :test
|
34
|
+
require driver_mock_source_name if driver_mock_source_name
|
35
|
+
end
|
36
|
+
|
37
|
+
@driver ||= eval( driver_class_name ).new
|
38
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
module Rack
|
2
|
+
class Request
|
3
|
+
# The media types of the HTTP_ACCEPT header ordered according to their
|
4
|
+
# "quality" (preference level), without any media type parameters.
|
5
|
+
#
|
6
|
+
# ===== Examples
|
7
|
+
#
|
8
|
+
# env['HTTP_ACCEPT'] #=> 'application/xml;q=0.8,text/html,text/plain;q=0.9'
|
9
|
+
#
|
10
|
+
# req = Rack::Request.new(env)
|
11
|
+
# req.accept_media_types #=> ['text/html', 'text/plain', 'application/xml']
|
12
|
+
# req.accept_media_types.prefered #=> 'text/html'
|
13
|
+
#
|
14
|
+
# For more information, see:
|
15
|
+
# * Acept header: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
16
|
+
# * Quality values: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.9
|
17
|
+
#
|
18
|
+
# ===== Returns
|
19
|
+
# AcceptMediaTypes:: ordered list of accept header's media types
|
20
|
+
#
|
21
|
+
def accept_media_types
|
22
|
+
@accept_media_types ||= Rack::AcceptMediaTypes.new(@env['HTTP_ACCEPT'])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# AcceptMediaTypes is intended for wrapping env['HTTP_ACCEPT'].
|
27
|
+
#
|
28
|
+
# It allows ordering of its values (accepted media types) according to their
|
29
|
+
# "quality" (preference level).
|
30
|
+
#
|
31
|
+
# This wrapper is typically used to determine the request's prefered media
|
32
|
+
# type (see example below).
|
33
|
+
#
|
34
|
+
# ===== Examples
|
35
|
+
#
|
36
|
+
# env['HTTP_ACCEPT'] #=> 'application/xml;q=0.8,text/html,text/plain;q=0.9'
|
37
|
+
#
|
38
|
+
# types = Rack::AcceptMediaTypes.new(env['HTTP_ACCEPT'])
|
39
|
+
# types #=> ['text/html', 'text/plain', 'application/xml']
|
40
|
+
# types.prefered #=> 'text/html'
|
41
|
+
#
|
42
|
+
# ===== Notes
|
43
|
+
#
|
44
|
+
# For simplicity, media type parameters are striped, as they are seldom used
|
45
|
+
# in practice. Users who need them are excepted to parse the Accept header
|
46
|
+
# manually.
|
47
|
+
#
|
48
|
+
# ===== References
|
49
|
+
#
|
50
|
+
# HTTP 1.1 Specs:
|
51
|
+
# * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
52
|
+
# * http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.9
|
53
|
+
#
|
54
|
+
class AcceptMediaTypes < Array
|
55
|
+
|
56
|
+
#--
|
57
|
+
# NOTE
|
58
|
+
# Reason for special handling of nil accept header:
|
59
|
+
#
|
60
|
+
# "If no Accept header field is present, then it is assumed that the client
|
61
|
+
# accepts all media types."
|
62
|
+
#
|
63
|
+
def initialize(header)
|
64
|
+
if header.nil?
|
65
|
+
replace(['*/*'])
|
66
|
+
else
|
67
|
+
replace(order(header.gsub(/ /, '').split(/,/)))
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# The client's prefered media type.
|
72
|
+
def prefered
|
73
|
+
first
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
# Order media types by quality values, remove invalid types, and return media ranges.
|
79
|
+
#
|
80
|
+
def order(types) #:nodoc:
|
81
|
+
types.map {|type| AcceptMediaType.new(type) }.reverse.sort.reverse.select {|type| type.valid? }.map {|type| type.range }
|
82
|
+
end
|
83
|
+
|
84
|
+
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
85
|
+
#
|
86
|
+
class AcceptMediaType #:nodoc:
|
87
|
+
include Comparable
|
88
|
+
|
89
|
+
# media-range = ( "*/*"
|
90
|
+
# | ( type "/" "*" )
|
91
|
+
# | ( type "/" subtype )
|
92
|
+
# ) *( ";" parameter )
|
93
|
+
attr_accessor :range
|
94
|
+
|
95
|
+
# qvalue = ( "0" [ "." 0*3DIGIT ] )
|
96
|
+
# | ( "1" [ "." 0*3("0") ] )
|
97
|
+
attr_accessor :quality
|
98
|
+
|
99
|
+
def initialize(type)
|
100
|
+
self.range, *params = type.split(';')
|
101
|
+
self.quality = extract_quality(params)
|
102
|
+
end
|
103
|
+
|
104
|
+
def <=>(type)
|
105
|
+
self.quality <=> type.quality
|
106
|
+
end
|
107
|
+
|
108
|
+
# "A weight is normalized to a real number in the range 0 through 1,
|
109
|
+
# where 0 is the minimum and 1 the maximum value. If a parameter has a
|
110
|
+
# quality value of 0, then content with this parameter is `not
|
111
|
+
# acceptable' for the client."
|
112
|
+
#
|
113
|
+
def valid?
|
114
|
+
self.quality.between?(0.1, 1)
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
# Extract value from 'q=FLOAT' parameter if present, otherwise assume 1
|
119
|
+
#
|
120
|
+
# "The default value is q=1."
|
121
|
+
#
|
122
|
+
def extract_quality(params)
|
123
|
+
q = params.detect {|p| p.match(/q=\d\.?\d{0,3}/) }
|
124
|
+
q ? q.split('=').last.to_f : 1.0
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|