restfulie 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +83 -7
- data/Rakefile +98 -13
- data/lib/restfulie/client/base.rb +48 -53
- data/lib/restfulie/client/configuration.rb +69 -0
- data/lib/restfulie/client/http/adapter.rb +487 -0
- data/lib/restfulie/client/http/atom_ext.rb +87 -0
- data/lib/restfulie/client/http/cache.rb +28 -0
- data/lib/restfulie/client/http/error.rb +80 -0
- data/lib/restfulie/client/http/marshal.rb +147 -0
- data/lib/restfulie/client/http.rb +13 -0
- data/lib/restfulie/client.rb +8 -56
- data/lib/restfulie/common/builder/builder_base.rb +58 -0
- data/lib/restfulie/common/builder/helpers.rb +22 -0
- data/lib/restfulie/common/builder/marshalling/atom.rb +197 -0
- data/lib/restfulie/common/builder/marshalling/base.rb +12 -0
- data/lib/restfulie/common/builder/marshalling/json.rb +2 -0
- data/lib/restfulie/common/builder/marshalling.rb +16 -0
- data/lib/restfulie/common/builder/rules/collection_rule.rb +10 -0
- data/lib/restfulie/common/builder/rules/link.rb +20 -0
- data/lib/restfulie/common/builder/rules/links.rb +9 -0
- data/lib/restfulie/common/builder/rules/member_rule.rb +8 -0
- data/lib/restfulie/common/builder/rules/namespace.rb +25 -0
- data/lib/restfulie/common/builder/rules/rules_base.rb +76 -0
- data/lib/restfulie/common/builder.rb +16 -0
- data/lib/restfulie/common/errors.rb +9 -0
- data/lib/restfulie/{logger.rb → common/logger.rb} +3 -5
- data/lib/restfulie/common/representation/atom.rb +48 -0
- data/lib/restfulie/common/representation/generic.rb +33 -0
- data/lib/restfulie/common/representation/xml.rb +24 -0
- data/lib/restfulie/common/representation.rb +10 -0
- data/lib/restfulie/common.rb +23 -0
- data/lib/restfulie/server/action_controller/base.rb +31 -0
- data/lib/restfulie/server/action_controller/params_parser.rb +62 -0
- data/lib/restfulie/server/action_controller/restful_responder.rb +39 -0
- data/lib/restfulie/server/action_controller/routing/restful_route.rb +14 -0
- data/lib/restfulie/server/action_controller/routing.rb +12 -0
- data/lib/restfulie/server/action_controller.rb +15 -0
- data/lib/restfulie/server/action_view/helpers.rb +45 -0
- data/lib/restfulie/server/action_view/template_handlers/tokamak.rb +15 -0
- data/lib/restfulie/server/action_view/template_handlers.rb +13 -0
- data/lib/restfulie/server/action_view.rb +8 -0
- data/lib/restfulie/server/configuration.rb +21 -0
- data/lib/restfulie/server/core_ext/array.rb +45 -0
- data/lib/restfulie/server/core_ext.rb +1 -0
- data/lib/restfulie/server/restfulie_controller.rb +1 -17
- data/lib/restfulie/server.rb +15 -0
- data/lib/restfulie.rb +4 -72
- data/lib/vendor/atom/configuration.rb +24 -0
- data/lib/vendor/atom/pub.rb +250 -0
- data/lib/vendor/atom/xml/parser.rb +373 -0
- data/lib/vendor/atom.rb +771 -0
- metadata +94 -33
- data/lib/restfulie/client/atom_media_type.rb +0 -75
- data/lib/restfulie/client/cache.rb +0 -103
- data/lib/restfulie/client/entry_point.rb +0 -94
- data/lib/restfulie/client/extensions/http.rb +0 -116
- data/lib/restfulie/client/helper.rb +0 -28
- data/lib/restfulie/client/instance.rb +0 -158
- data/lib/restfulie/client/request_execution.rb +0 -321
- data/lib/restfulie/client/state.rb +0 -36
- data/lib/restfulie/media_type.rb +0 -143
- data/lib/restfulie/media_type_control.rb +0 -115
- data/lib/restfulie/media_type_defaults.rb +0 -51
- data/lib/restfulie/server/atom_media_type.rb +0 -115
- data/lib/restfulie/server/base.rb +0 -91
- data/lib/restfulie/server/controller.rb +0 -122
- data/lib/restfulie/server/instance.rb +0 -102
- data/lib/restfulie/server/marshalling.rb +0 -47
- data/lib/restfulie/server/opensearch/description.rb +0 -54
- data/lib/restfulie/server/opensearch.rb +0 -18
- data/lib/restfulie/server/transition.rb +0 -93
- data/lib/restfulie/unmarshalling.rb +0 -131
- data/lib/vendor/jeokkarak/hashi.rb +0 -65
- data/lib/vendor/jeokkarak/jeokkarak.rb +0 -81
@@ -1,115 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
|
17
|
-
# extends Rails mime type
|
18
|
-
|
19
|
-
module Mime
|
20
|
-
class << self
|
21
|
-
# alias_method :old_const_set, :const_set
|
22
|
-
|
23
|
-
# ignores setting the contest again
|
24
|
-
# def const_set(a,b)
|
25
|
-
# super(a,b) unless Mime.const_defined?(a)
|
26
|
-
# end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
module Restfulie
|
31
|
-
|
32
|
-
module MediaTypeControl
|
33
|
-
|
34
|
-
# defines a custom media_type for this type
|
35
|
-
def media_type(*args)
|
36
|
-
args.each do |name|
|
37
|
-
custom_representations << name
|
38
|
-
type = Restfulie::MediaType.rendering_type(name, self)
|
39
|
-
Restfulie::MediaType.register(type)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# returns the list of media types available for this resource to be deserialized from
|
44
|
-
def media_types
|
45
|
-
Restfulie::MediaType.default_types + MediaType.medias_for(self)
|
46
|
-
end
|
47
|
-
|
48
|
-
# returns a list of media types that this resource can be serialized to
|
49
|
-
def media_type_representations
|
50
|
-
custom_representations + Restfulie::MediaType.default_representations.dup
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
# this model's custom representations. those representations were added through media_type definitions
|
56
|
-
def custom_representations
|
57
|
-
@custom_representations ||= []
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
|
63
|
-
def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
|
64
|
-
SET << Mime.const_get(symbol.to_s.upcase)
|
65
|
-
|
66
|
-
([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } unless skip_lookup
|
67
|
-
([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last }
|
68
|
-
end
|
69
|
-
|
70
|
-
module MediaType
|
71
|
-
|
72
|
-
class << self
|
73
|
-
|
74
|
-
def register(type)
|
75
|
-
silence_warnings do
|
76
|
-
Mime::Type.register(type.name, type.short_name.to_sym)
|
77
|
-
end
|
78
|
-
|
79
|
-
media_types[type.name] = type
|
80
|
-
end
|
81
|
-
|
82
|
-
# TODO rename to type for mt
|
83
|
-
def media_type(name)
|
84
|
-
name = normalize(name)
|
85
|
-
raise Restfulie::UnsupportedContentType.new("unsupported content type '#{name}'") if media_types[name].nil?
|
86
|
-
media_types[name].type
|
87
|
-
end
|
88
|
-
|
89
|
-
def supports?(name)
|
90
|
-
name = normalize(name)
|
91
|
-
!media_types[name].nil?
|
92
|
-
end
|
93
|
-
|
94
|
-
def normalize(name)
|
95
|
-
name[/[^;]*/]
|
96
|
-
end
|
97
|
-
|
98
|
-
def media_types
|
99
|
-
@media_types ||= {}
|
100
|
-
end
|
101
|
-
|
102
|
-
# TODO move to MediaTypeControl.custom_media_types
|
103
|
-
def medias_for(type)
|
104
|
-
found = {}
|
105
|
-
type.media_type_representations.each do |key|
|
106
|
-
found[key] = media_types[key]
|
107
|
-
end
|
108
|
-
found.values
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
114
|
-
|
115
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
module Restfulie
|
19
|
-
|
20
|
-
module MediaType
|
21
|
-
def self.HtmlType
|
22
|
-
custom_type('html', DefaultMediaTypeDecoder, lambda {})
|
23
|
-
end
|
24
|
-
def self.TextHtmlType
|
25
|
-
custom_type('text/html', DefaultMediaTypeDecoder, lambda {})
|
26
|
-
end
|
27
|
-
|
28
|
-
# TODO rename it and move it
|
29
|
-
def self.default_types
|
30
|
-
[Restfulie::MediaType.HtmlType,
|
31
|
-
Restfulie::MediaType.TextHtmlType,
|
32
|
-
rendering_type('application/xml', self),
|
33
|
-
rendering_type('application/json', self),
|
34
|
-
rendering_type('xml', self),
|
35
|
-
rendering_type('json', self)]
|
36
|
-
end
|
37
|
-
|
38
|
-
# Default representations: every object can be serialized to those types
|
39
|
-
def self.default_representations
|
40
|
-
['html','text/html','application/xml','application/json','xml','json']
|
41
|
-
end
|
42
|
-
|
43
|
-
# TODO should allow aliases...
|
44
|
-
register(Restfulie::MediaType.HtmlType)
|
45
|
-
register(Restfulie::MediaType.TextHtmlType)
|
46
|
-
register(rendering_type('application/xml', DefaultMediaTypeDecoder))
|
47
|
-
register(rendering_type('application/json', DefaultMediaTypeDecoder))
|
48
|
-
register(rendering_type('xml', DefaultMediaTypeDecoder))
|
49
|
-
register(rendering_type('json', DefaultMediaTypeDecoder))
|
50
|
-
end
|
51
|
-
end
|
@@ -1,115 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
module Restfulie
|
19
|
-
|
20
|
-
module Server
|
21
|
-
|
22
|
-
class AtomMediaType < Restfulie::MediaType::Type
|
23
|
-
def initialize(name, type)
|
24
|
-
super(name, type)
|
25
|
-
end
|
26
|
-
def execute_for(controller, resource, options, render_options)
|
27
|
-
response = ["atom"].include?(format_name) ? resource.send(:"to_#{format_name}", options) : resource
|
28
|
-
render(controller, response, render_options)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
Restfulie::MediaType.register(Restfulie::MediaType.rendering_type('application/atom+xml', Server::AtomMediaType))
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
class Array
|
39
|
-
extend Restfulie::MediaTypeControl
|
40
|
-
media_type "application/atom+xml"
|
41
|
-
|
42
|
-
def to_atom(options = {}, &block)
|
43
|
-
AtomFeed.new(self).title(options[:title]).to_atom(options[:controller], block)
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
class AtomFeed
|
49
|
-
def initialize(feed)
|
50
|
-
@feed = feed
|
51
|
-
@title = "feed"
|
52
|
-
end
|
53
|
-
|
54
|
-
def title(title)
|
55
|
-
@title = title
|
56
|
-
self
|
57
|
-
end
|
58
|
-
|
59
|
-
def to_atom(controller, block = nil)
|
60
|
-
last_modified = updated_at
|
61
|
-
id = id_for(controller)
|
62
|
-
xml = items_to_atom_xml(controller, block)
|
63
|
-
"""<?xml version=\"1.0\"?>
|
64
|
-
<feed xmlns=\"http://www.w3.org/2005/Atom\">
|
65
|
-
<id>#{id}</id>
|
66
|
-
<title type=\"text\">#{@title}</title>
|
67
|
-
<updated>""" + last_modified.strftime("%Y-%m-%dT%H:%M:%S-08:00") + """</updated>
|
68
|
-
<author><name>#{@title}</name></author>
|
69
|
-
#{self_link(controller)}
|
70
|
-
#{xml}
|
71
|
-
</feed>"""
|
72
|
-
end
|
73
|
-
|
74
|
-
def id_for(controller)
|
75
|
-
@id || controller.url_for({})
|
76
|
-
end
|
77
|
-
|
78
|
-
def id(*id)
|
79
|
-
@id = id
|
80
|
-
self
|
81
|
-
end
|
82
|
-
|
83
|
-
def self_link(controller, what = {})
|
84
|
-
uri = controller.url_for(what)
|
85
|
-
"<link rel=\"self\" href=\"#{uri}\"/>"
|
86
|
-
end
|
87
|
-
|
88
|
-
def updated_at
|
89
|
-
@feed.updated_at
|
90
|
-
end
|
91
|
-
|
92
|
-
def items_to_atom_xml(controller, serializer = nil)
|
93
|
-
xml = ""
|
94
|
-
@feed.each do |item|
|
95
|
-
uri = controller.url_for(item)
|
96
|
-
media_type = item.class.respond_to?(:media_type_representations) ? item.class.media_type_representations[0] : 'application/xml'
|
97
|
-
item_xml = serializer.nil? ? item.to_xml(:controller => controller, :skip_instruct => true) : serializer.call(item)
|
98
|
-
xml += """ <entry>
|
99
|
-
<id>#{uri}</id>
|
100
|
-
<title type=\"text\">#{item.class.name}</title>
|
101
|
-
<updated>#{modification_for(item).strftime("%Y-%m-%dT%H:%M:%S-08:00")}</updated>
|
102
|
-
#{self_link(controller, item)}
|
103
|
-
<content type=\"#{media_type}\">
|
104
|
-
#{item_xml}
|
105
|
-
</content>
|
106
|
-
</entry>\n"""
|
107
|
-
end
|
108
|
-
xml
|
109
|
-
end
|
110
|
-
|
111
|
-
def modification_for(item)
|
112
|
-
item.respond_to?(:updated_at) ? item.updated_at : Time.now
|
113
|
-
end
|
114
|
-
|
115
|
-
end
|
@@ -1,91 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
module Restfulie
|
19
|
-
module Server
|
20
|
-
module Base
|
21
|
-
|
22
|
-
# returns the definition for the transaction
|
23
|
-
def existing_transition(name)
|
24
|
-
transitions[name]
|
25
|
-
end
|
26
|
-
|
27
|
-
# returns a hash of all possible transitions: Restfulie::Server::Transition
|
28
|
-
def transitions
|
29
|
-
@transitions ||= {}
|
30
|
-
end
|
31
|
-
|
32
|
-
# returns a hash of all possible states
|
33
|
-
def states
|
34
|
-
@states ||= {}
|
35
|
-
end
|
36
|
-
|
37
|
-
# adds a new state to the list of possible states
|
38
|
-
def state(name, options = {})
|
39
|
-
options[:allow] = [options[:allow]] unless options[:allow].kind_of? Array
|
40
|
-
states[name] = options
|
41
|
-
end
|
42
|
-
|
43
|
-
# defines a new transition. the transition options works in the same way
|
44
|
-
# that following_transition definition does.
|
45
|
-
def transition(name = nil, options = {}, result = nil, &body)
|
46
|
-
return TransitionBuilder.new(self) if name.nil?
|
47
|
-
|
48
|
-
transition = Restfulie::Server::Transition.new(name, options, result, body)
|
49
|
-
transitions[name] = transition
|
50
|
-
|
51
|
-
define_execution_method(self, name, result)
|
52
|
-
define_can_method(self, name)
|
53
|
-
end
|
54
|
-
|
55
|
-
class TransitionBuilder
|
56
|
-
def initialize(type)
|
57
|
-
@type = type
|
58
|
-
end
|
59
|
-
def method_missing(name, *args)
|
60
|
-
@transition = Restfulie::Server::Transition.new(name)
|
61
|
-
@type.transitions[name] = @transition
|
62
|
-
@type.define_can_method(@type, name)
|
63
|
-
self
|
64
|
-
end
|
65
|
-
|
66
|
-
def at(options)
|
67
|
-
@transition.options = options
|
68
|
-
end
|
69
|
-
|
70
|
-
def results_in(result)
|
71
|
-
@transition.result = result
|
72
|
-
end
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
def define_execution_method(type, name, result)
|
77
|
-
type.send(:define_method, name) do |*args|
|
78
|
-
self.status = result.to_s unless result.nil?
|
79
|
-
end unless type.respond_to?(name)
|
80
|
-
end
|
81
|
-
|
82
|
-
def define_can_method(type, name)
|
83
|
-
type.send(:define_method, "can_#{name}?") do
|
84
|
-
transitions = self.available_transitions[:allow]
|
85
|
-
transitions.include? name
|
86
|
-
end unless type.respond_to?("can_#{name}?")
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
@@ -1,122 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
module Restfulie::Server::Cache
|
18
|
-
|
19
|
-
class Config
|
20
|
-
|
21
|
-
def allow(seconds)
|
22
|
-
@max_age = seconds
|
23
|
-
self
|
24
|
-
end
|
25
|
-
|
26
|
-
def max_age
|
27
|
-
@max_age ||= 0
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
module ActionController
|
33
|
-
class Base
|
34
|
-
|
35
|
-
def self.cache
|
36
|
-
@cache_config ||= Restfulie::Server::Cache::Config.new
|
37
|
-
end
|
38
|
-
|
39
|
-
# renders an specific resource to xml
|
40
|
-
# using any extra options to render it (invoke to_xml).
|
41
|
-
def render_resource(resource, options = {}, render_options = {})
|
42
|
-
|
43
|
-
response.headers['Cache-control'] = "max-age=#{self.class.cache.max_age}"
|
44
|
-
return nil unless stale? resource.cache_info
|
45
|
-
|
46
|
-
return render(render_options) if render_options[:text]
|
47
|
-
|
48
|
-
options[:controller] = self
|
49
|
-
respond_to do |format|
|
50
|
-
add_media_responses(format, resource, options, render_options)
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
# renders a resource collection, making full use of atom support
|
56
|
-
def render_collection(collection, &block)
|
57
|
-
if block
|
58
|
-
content = collection.to_atom(:title =>collection_name, :controller => self) do |item|
|
59
|
-
block.call item
|
60
|
-
end
|
61
|
-
else
|
62
|
-
content = collection.to_atom(:title => collection_name, :controller => self)
|
63
|
-
end
|
64
|
-
render_resource collection, nil, {:content_type => 'application/atom+xml', :text => content}
|
65
|
-
end
|
66
|
-
|
67
|
-
# returns the name of this controllers collection
|
68
|
-
def collection_name
|
69
|
-
self.class.name[/(.*)Controller/,1]
|
70
|
-
end
|
71
|
-
|
72
|
-
def add_media_responses(format, resource, options, render_options)
|
73
|
-
types = Restfulie::MediaType.default_types
|
74
|
-
types = resource.class.media_types if resource.class.respond_to? :media_types
|
75
|
-
types.each do |media_type|
|
76
|
-
add_media_response(format, resource, media_type, options, render_options)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def add_media_response(format, resource, media_type, options, render_options)
|
81
|
-
controller = self
|
82
|
-
format.send media_type.short_name.to_sym do
|
83
|
-
media_type.execute_for(controller, resource, options, render_options)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
# adds support to rendering resources, i.e.:
|
88
|
-
# render :resource => @order, :with => { :except => [:paid_at] }
|
89
|
-
alias_method :old_render, :render
|
90
|
-
def render(options = nil, extra_options = {}, &block)
|
91
|
-
resource = options[:resource] unless options.nil?
|
92
|
-
unless resource.nil?
|
93
|
-
render_resource(resource, options[:with])
|
94
|
-
else
|
95
|
-
old_render(options, extra_options)
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
# renders a created resource including its required headers:
|
100
|
-
# Location and 201
|
101
|
-
def render_created(resource, options = {})
|
102
|
-
location= url_for resource
|
103
|
-
render_resource resource, options, {:status => :created, :location => location}
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
module MimeResponds
|
108
|
-
class Responder
|
109
|
-
attr_reader :mime_type_priority
|
110
|
-
alias_method :old_respond, :respond unless method_defined?(:old_respond)
|
111
|
-
def respond
|
112
|
-
RestfulieResponder.new.respond(self)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
class RestfulieResponder
|
117
|
-
def respond(instance)
|
118
|
-
instance.old_respond unless instance.mime_type_priority.include? "html"
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
@@ -1,102 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
module Restfulie::Server::Cache
|
19
|
-
|
20
|
-
# returns specific cache information for this resource
|
21
|
-
# you may customize it and return a custom etag and last_modified fields
|
22
|
-
def cache_info
|
23
|
-
info = {:etag => self}
|
24
|
-
info[:etag] = self.etag if self.respond_to? :etag
|
25
|
-
info[:last_modified] = self.updated_at.utc if self.respond_to? :updated_at
|
26
|
-
info
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
# acts_as_restfulie instances implement this module
|
32
|
-
module Restfulie::Server::Instance
|
33
|
-
|
34
|
-
include Restfulie::Server::Cache
|
35
|
-
|
36
|
-
# checks whether this resource can execute a transition or has such a relation
|
37
|
-
def can?(what)
|
38
|
-
what = what.to_sym if what.kind_of? String
|
39
|
-
all_following_transitions.each do |t|
|
40
|
-
return true if t.name == what
|
41
|
-
end
|
42
|
-
false
|
43
|
-
end
|
44
|
-
|
45
|
-
# returns a list of available transitions for this objects state
|
46
|
-
# TODO rename because it should never be used by the client...
|
47
|
-
def available_transitions
|
48
|
-
status_available = respond_to?(:status) && status!=nil
|
49
|
-
return {:allow => []} unless status_available
|
50
|
-
self.class.states[self.status.to_sym] || {:allow => []}
|
51
|
-
end
|
52
|
-
|
53
|
-
# returns a list containing all available transitions for this object's state
|
54
|
-
def all_following_transitions
|
55
|
-
all = [] + available_transitions[:allow]
|
56
|
-
following_transitions.each do |t|
|
57
|
-
t = Restfulie::Server::Transition.new(t) if t.kind_of? Array
|
58
|
-
all << t
|
59
|
-
end
|
60
|
-
all
|
61
|
-
end
|
62
|
-
|
63
|
-
# checks if its possible to execute such transition and, if it is, executes it
|
64
|
-
def move_to(name)
|
65
|
-
raise "Current state #{status} is invalid in order to execute #{name}. It must be one of #{transitions}" unless available_transitions[:allow].include? name
|
66
|
-
self.class.transitions[name].execute_at self
|
67
|
-
end
|
68
|
-
|
69
|
-
# gets all the links for each transition
|
70
|
-
def links(controller)
|
71
|
-
links = []
|
72
|
-
unless controller.nil?
|
73
|
-
all_following_transitions.each do |transition|
|
74
|
-
rel, uri = link_for(transition, controller)
|
75
|
-
links << {:rel => rel, :uri => uri}
|
76
|
-
end
|
77
|
-
end
|
78
|
-
links
|
79
|
-
end
|
80
|
-
|
81
|
-
private
|
82
|
-
# gets a link for this transition
|
83
|
-
def link_for(transition, controller)
|
84
|
-
transition = self.class.existing_transition(transition.to_sym) unless transition.kind_of? Restfulie::Server::Transition
|
85
|
-
transition.link_for(self, controller)
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
|
90
|
-
class Array
|
91
|
-
|
92
|
-
include Restfulie::Server::Cache
|
93
|
-
|
94
|
-
def updated_at
|
95
|
-
last = nil
|
96
|
-
each do |item|
|
97
|
-
last = item.updated_at if item.respond_to?(:updated_at) && (last.nil? || item.updated_at > last)
|
98
|
-
end
|
99
|
-
last || Time.now
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
module Restfulie
|
19
|
-
|
20
|
-
module Server
|
21
|
-
|
22
|
-
module Marshalling
|
23
|
-
|
24
|
-
# marshalls your object to json.
|
25
|
-
# adds all links if there are any available.
|
26
|
-
def to_json(options = {})
|
27
|
-
Hash.from_xml(to_xml(options)).to_json
|
28
|
-
end
|
29
|
-
|
30
|
-
# marshalls your object to xml.
|
31
|
-
# adds all links if there are any available.
|
32
|
-
def to_xml(options = {})
|
33
|
-
options[:skip_types] = true
|
34
|
-
super options do |xml|
|
35
|
-
links(options[:controller]).each do |link|
|
36
|
-
if options[:use_name_based_link]
|
37
|
-
xml.tag!(link[:rel], link[:uri])
|
38
|
-
else
|
39
|
-
xml.tag!('atom:link', 'xmlns:atom' => 'http://www.w3.org/2005/Atom', :rel => link[:rel], :href => link[:uri])
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
module Restfulie
|
19
|
-
module OpenSearch
|
20
|
-
|
21
|
-
class Description
|
22
|
-
|
23
|
-
def self.define(attribute, xml_attribute)
|
24
|
-
|
25
|
-
define_method("#{attribute.to_s}=") do |new_value|
|
26
|
-
self.instance_variable_set(xml_attribute, new_value)
|
27
|
-
end
|
28
|
-
|
29
|
-
define_method(attribute.to_s) do |new_value|
|
30
|
-
self.instance_variable_get xml_attribute
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
define(:short_name, :@ShortName)
|
36
|
-
define(:description, :@Description)
|
37
|
-
define(:tags, :@Tags)
|
38
|
-
define(:contact, :@Contact)
|
39
|
-
|
40
|
-
def initialize(name)
|
41
|
-
short_name = name
|
42
|
-
description = name
|
43
|
-
@types = []
|
44
|
-
end
|
45
|
-
|
46
|
-
def accepts(content_type)
|
47
|
-
@types << content_type
|
48
|
-
self
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
end
|