restfulie 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +10 -5
- data/Rakefile +5 -2
- data/lib/restfulie.rb +10 -5
- data/lib/restfulie/client.rb +13 -9
- data/lib/restfulie/client/base.rb +24 -65
- data/lib/restfulie/client/configuration.rb +62 -64
- data/lib/restfulie/client/entry_point.rb +36 -0
- data/lib/restfulie/client/ext/atom_ext.rb +12 -0
- data/lib/restfulie/client/ext/http_ext.rb +22 -0
- data/lib/restfulie/client/ext/json_ext.rb +12 -0
- data/lib/restfulie/client/ext/xml_ext.rb +4 -0
- data/lib/restfulie/client/http.rb +25 -13
- data/lib/restfulie/client/http/cache.rb +22 -22
- data/lib/restfulie/client/http/error.rb +70 -70
- data/lib/restfulie/client/http/link_request_builder.rb +15 -0
- data/lib/restfulie/client/http/request_adapter.rb +209 -0
- data/lib/restfulie/client/http/request_builder.rb +107 -0
- data/lib/restfulie/client/http/request_builder_executor.rb +24 -0
- data/lib/restfulie/client/http/request_executor.rb +17 -0
- data/lib/restfulie/client/http/request_follow.rb +42 -0
- data/lib/restfulie/client/http/request_follow_executor.rb +10 -0
- data/lib/restfulie/client/http/request_history.rb +69 -0
- data/lib/restfulie/client/http/request_history_executor.rb +10 -0
- data/lib/restfulie/client/http/request_marshaller.rb +127 -0
- data/lib/restfulie/client/http/request_marshaller_executor.rb +10 -0
- data/lib/restfulie/client/http/response.rb +23 -0
- data/lib/restfulie/client/http/response_handler.rb +67 -0
- data/lib/restfulie/client/http/response_holder.rb +9 -0
- data/lib/restfulie/client/mikyung.rb +17 -14
- data/lib/restfulie/client/mikyung/concatenator.rb +15 -12
- data/lib/restfulie/client/mikyung/core.rb +65 -39
- data/lib/restfulie/client/mikyung/languages.rb +8 -26
- data/lib/restfulie/client/mikyung/languages/german.rb +24 -0
- data/lib/restfulie/client/mikyung/languages/portuguese.rb +23 -0
- data/lib/restfulie/client/mikyung/rest_process_model.rb +184 -107
- data/lib/restfulie/client/mikyung/steady_state_walker.rb +34 -28
- data/lib/restfulie/client/mikyung/then_condition.rb +33 -27
- data/lib/restfulie/client/mikyung/when_condition.rb +53 -49
- data/lib/restfulie/common.rb +7 -12
- data/lib/restfulie/common/converter.rb +20 -9
- data/lib/restfulie/common/converter/atom.rb +8 -83
- data/lib/restfulie/common/converter/atom/base.rb +89 -0
- data/lib/restfulie/common/converter/atom/builder.rb +101 -99
- data/lib/restfulie/common/converter/atom/helpers.rb +16 -8
- data/lib/restfulie/common/converter/json.rb +12 -0
- data/lib/restfulie/common/converter/json/base.rb +84 -0
- data/lib/restfulie/common/converter/json/builder.rb +102 -0
- data/lib/restfulie/common/converter/json/helpers.rb +17 -0
- data/lib/restfulie/common/converter/values.rb +30 -26
- data/lib/restfulie/common/converter/xml.rb +14 -0
- data/lib/restfulie/common/converter/xml/base.rb +61 -0
- data/lib/restfulie/common/converter/xml/builder.rb +112 -0
- data/lib/restfulie/common/converter/xml/helpers.rb +17 -0
- data/lib/restfulie/common/converter/xml/link.rb +25 -0
- data/lib/restfulie/common/converter/xml/links.rb +25 -0
- data/lib/restfulie/common/core_ext.rb +1 -5
- data/lib/restfulie/common/core_ext/hash.rb +12 -0
- data/lib/restfulie/common/error.rb +19 -0
- data/lib/restfulie/common/logger.rb +17 -9
- data/lib/restfulie/common/representation.rb +9 -10
- data/lib/restfulie/common/representation/atom.rb +15 -47
- data/lib/restfulie/common/representation/atom/base.rb +122 -365
- data/lib/restfulie/common/representation/atom/category.rb +41 -0
- data/lib/restfulie/common/representation/atom/entry.rb +52 -100
- data/lib/restfulie/common/representation/atom/factory.rb +43 -0
- data/lib/restfulie/common/representation/atom/feed.rb +103 -99
- data/lib/restfulie/common/representation/atom/link.rb +68 -0
- data/lib/restfulie/common/representation/atom/person.rb +48 -0
- data/lib/restfulie/common/representation/atom/source.rb +59 -0
- data/lib/restfulie/common/representation/atom/tag_collection.rb +38 -0
- data/lib/restfulie/common/representation/atom/xml.rb +95 -0
- data/lib/restfulie/common/representation/generic.rb +30 -29
- data/lib/restfulie/common/representation/json.rb +10 -22
- data/lib/restfulie/common/representation/json/base.rb +27 -0
- data/lib/restfulie/common/representation/json/keys_as_methods.rb +72 -0
- data/lib/restfulie/common/representation/json/link.rb +29 -0
- data/lib/restfulie/common/representation/json/link_collection.rb +23 -0
- data/lib/restfulie/common/representation/xml.rb +18 -227
- data/lib/restfulie/server.rb +9 -10
- data/lib/restfulie/server/action_controller.rb +10 -12
- data/lib/restfulie/server/action_controller/base.rb +18 -15
- data/lib/restfulie/server/action_controller/{routing/patch.rb → patch.rb} +0 -0
- data/lib/restfulie/server/action_controller/restful_responder.rb +43 -35
- data/lib/restfulie/server/action_view.rb +8 -6
- data/lib/restfulie/server/action_view/helpers.rb +47 -41
- data/lib/restfulie/server/action_view/template_handlers.rb +24 -12
- data/lib/restfulie/server/action_view/template_handlers/tokamak.rb +17 -12
- data/lib/restfulie/server/configuration.rb +22 -19
- data/lib/restfulie/server/{restfulie_controller.rb → controller.rb} +1 -10
- data/lib/restfulie/server/core_ext.rb +1 -1
- data/lib/restfulie/version.rb +14 -0
- metadata +52 -16
- data/lib/restfulie/client/http/adapter.rb +0 -502
- data/lib/restfulie/client/http/atom_ext.rb +0 -4
- data/lib/restfulie/client/http/core_ext.rb +0 -6
- data/lib/restfulie/client/http/core_ext/http.rb +0 -19
- data/lib/restfulie/client/http/marshal.rb +0 -145
- data/lib/restfulie/client/http/xml_ext.rb +0 -7
- data/lib/restfulie/common/core_ext/proc.rb +0 -48
- data/lib/restfulie/common/errors.rb +0 -15
- data/lib/restfulie/server/action_controller/routing.rb +0 -12
- data/lib/restfulie/server/action_controller/routing/restful_route.rb +0 -14
@@ -1,109 +1,111 @@
|
|
1
|
-
module Restfulie
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
options.each do |key,value|
|
15
|
-
attr = key.to_s
|
16
|
-
if attr =~ /^xmlns(:\w+)?$/
|
17
|
-
ns = attr.split(":", 2)[1]
|
18
|
-
@parent.add_namespace_definition(ns, value)
|
19
|
-
end
|
20
|
-
end if options
|
1
|
+
module Restfulie
|
2
|
+
module Common
|
3
|
+
module Converter
|
4
|
+
module Atom
|
5
|
+
class Builder
|
6
|
+
attr_accessor :atom_type
|
7
|
+
def initialize(atom_type, obj)
|
8
|
+
@doc = Nokogiri::XML::Document.new
|
9
|
+
@obj = obj
|
10
|
+
@parent = @doc.create_element(atom_type.to_s)
|
11
|
+
@parent.add_namespace_definition(nil, "http://www.w3.org/2005/Atom")
|
12
|
+
@parent.parent = @doc
|
13
|
+
end
|
21
14
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
15
|
+
def values(options = {}, &block)
|
16
|
+
options.each do |key,value|
|
17
|
+
attr = key.to_s
|
18
|
+
if attr =~ /^xmlns(:\w+)?$/
|
19
|
+
ns = attr.split(":", 2)[1]
|
20
|
+
@parent.add_namespace_definition(ns, value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
yield Values.new(self)
|
24
|
+
end
|
25
|
+
|
26
|
+
def members(options = {}, &block)
|
27
|
+
collection = options[:collection] || @obj
|
28
|
+
raise Error::BuilderError.new("Members method require a collection to execute") unless collection.respond_to?(:each)
|
29
|
+
collection.each do |member|
|
30
|
+
entry = @doc.create_element("entry")
|
31
|
+
entry.parent = @parent
|
32
|
+
@parent = entry
|
33
|
+
block.call(self, member)
|
34
|
+
@parent = entry.parent
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def link(relationship, uri, options = {})
|
39
|
+
options["rel"] = relationship.to_s
|
40
|
+
options["href"] = uri
|
41
|
+
options["type"] ||= "application/atom+xml"
|
42
|
+
insert_value("link", nil, options)
|
43
|
+
end
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
Restfulie::Common::Representation::Atom::Factory.create(@doc)
|
56
|
-
end
|
57
|
-
|
58
|
-
private
|
59
|
-
|
60
|
-
def create_element(node, prefix, *args)
|
61
|
-
node = @doc.create_element(node) do |n|
|
62
|
-
if prefix
|
63
|
-
if namespace = prefix_valid?(prefix)
|
64
|
-
# Adding namespace prefix
|
65
|
-
n.namespace = namespace
|
66
|
-
namespace = nil
|
45
|
+
def insert_value(name, prefix, *args, &block)
|
46
|
+
node = create_element(name.to_s, prefix, *args)
|
47
|
+
node.parent = @parent
|
48
|
+
if block_given?
|
49
|
+
@parent = node
|
50
|
+
block.call
|
51
|
+
@parent = node.parent
|
52
|
+
end
|
67
53
|
end
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
54
|
+
|
55
|
+
def representation
|
56
|
+
Representation::Atom::Factory.create(@doc)
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def create_element(node, prefix, *args)
|
62
|
+
node = @doc.create_element(node) do |n|
|
63
|
+
if prefix
|
64
|
+
if namespace = prefix_valid?(prefix)
|
65
|
+
# Adding namespace prefix
|
66
|
+
n.namespace = namespace
|
67
|
+
namespace = nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
args.each do |arg|
|
72
|
+
case arg
|
73
|
+
# Adding XML attributes
|
74
|
+
when Hash
|
75
|
+
arg.each { |k,v|
|
76
|
+
key = k.to_s
|
77
|
+
if key =~ /^xmlns(:\w+)?$/
|
78
|
+
ns_name = key.split(":", 2)[1]
|
79
|
+
n.add_namespace_definition(ns_name, v)
|
80
|
+
next
|
81
|
+
end
|
82
|
+
n[k.to_s] = v.to_s
|
83
|
+
}
|
84
|
+
# Adding XML node content
|
85
|
+
else
|
86
|
+
arg.kind_of?(Time) || arg.kind_of?(DateTime) ? content = arg.xmlschema : content = arg
|
87
|
+
n.content = content
|
88
|
+
end
|
80
89
|
end
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def prefix_valid?(prefix)
|
94
|
+
ns = @parent.namespace_definitions.find { |x| x.prefix == prefix.to_s }
|
95
|
+
|
96
|
+
unless ns
|
97
|
+
@parent.ancestors.each do |a|
|
98
|
+
next if a == @doc
|
99
|
+
ns = a.namespace_definitions.find { |x| x.prefix == prefix.to_s }
|
100
|
+
break if ns
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
return ns
|
105
|
+
#TODO: raise ArgumentError, "Namespace #{prefix} has not been defined" if wanted
|
87
106
|
end
|
88
107
|
end
|
89
108
|
end
|
90
109
|
end
|
91
|
-
|
92
|
-
def prefix_valid?(prefix)
|
93
|
-
ns = @parent.namespace_definitions.find { |x| x.prefix == prefix.to_s }
|
94
|
-
|
95
|
-
unless ns
|
96
|
-
@parent.ancestors.each do |a|
|
97
|
-
next if a == @doc
|
98
|
-
ns = a.namespace_definitions.find { |x| x.prefix == prefix.to_s }
|
99
|
-
break if ns
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
return ns
|
104
|
-
#TODO: raise ArgumentError, "Namespace #{prefix} has not been defined" if wanted
|
105
|
-
end
|
106
|
-
|
107
110
|
end
|
108
|
-
|
109
111
|
end
|
@@ -1,9 +1,17 @@
|
|
1
|
-
module Restfulie
|
2
|
-
|
3
|
-
|
1
|
+
module Restfulie
|
2
|
+
module Common
|
3
|
+
module Converter
|
4
|
+
module Atom
|
5
|
+
module Helpers
|
6
|
+
def collection(obj, *args, &block)
|
7
|
+
Restfulie::Common::Converter::Atom.to_atom(obj, :atom_type => :feed, &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def member(obj, *args, &block)
|
11
|
+
Restfulie::Common::Converter::Atom.to_atom(obj, :atom_type => :entry, &block)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
4
16
|
end
|
5
|
-
|
6
|
-
def member(obj, *args, &block)
|
7
|
-
Restfulie::Common::Converter::Atom.to_atom(obj, :atom_type => :entry, &block)
|
8
|
-
end
|
9
|
-
end
|
17
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Restfulie
|
2
|
+
module Common
|
3
|
+
module Converter
|
4
|
+
module Json
|
5
|
+
autoload :Base, 'restfulie/common/converter/json/base'
|
6
|
+
autoload :Builder, 'restfulie/common/converter/json/builder'
|
7
|
+
autoload :Helpers, 'restfulie/common/converter/json/helpers'
|
8
|
+
extend Base::ClassMethods
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Restfulie
|
2
|
+
module Common
|
3
|
+
module Converter
|
4
|
+
module Json
|
5
|
+
module Base
|
6
|
+
module ClassMethods
|
7
|
+
mattr_reader :media_type_name
|
8
|
+
@@media_type_name = 'application/json'
|
9
|
+
|
10
|
+
mattr_reader :headers
|
11
|
+
@@headers = {
|
12
|
+
:get => { 'Accept' => media_type_name },
|
13
|
+
:post => { 'Content-Type' => media_type_name }
|
14
|
+
}
|
15
|
+
|
16
|
+
mattr_reader :recipes
|
17
|
+
@@recipes = {}
|
18
|
+
|
19
|
+
def describe_recipe(recipe_name, options={}, &block)
|
20
|
+
raise 'Undefined recipe' unless block_given?
|
21
|
+
raise 'Undefined recipe_name' unless recipe_name
|
22
|
+
@@recipes[recipe_name] = block
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_json(obj = nil, options = {:root => {}}, &block)
|
26
|
+
# just instantiate the string with the atom factory
|
27
|
+
return Restfulie::Common::Representation::Json.create(obj) if obj.kind_of?(String)
|
28
|
+
|
29
|
+
if block_given?
|
30
|
+
recipe = block
|
31
|
+
else
|
32
|
+
recipe = options[:recipe]
|
33
|
+
end
|
34
|
+
|
35
|
+
# Check if the object is already an json
|
36
|
+
unless recipe
|
37
|
+
return Restfulie::Common::Representation::Json.create(obj) if obj.kind_of?(Hash) || obj.kind_of?(Array)
|
38
|
+
raise Restfulie::Common::Error::ConverterError.new("Recipe required")
|
39
|
+
end
|
40
|
+
|
41
|
+
# Get recipe already described
|
42
|
+
recipe = @@recipes[recipe] unless recipe.respond_to?(:call)
|
43
|
+
|
44
|
+
# Create representation and proxy
|
45
|
+
builder = Restfulie::Common::Converter::Json::Builder.new(obj, (options[:root] || {}))
|
46
|
+
|
47
|
+
# Check recipe arity size before calling it
|
48
|
+
recipe.call(*[builder, obj, options][0,recipe.arity])
|
49
|
+
|
50
|
+
builder.representation
|
51
|
+
end
|
52
|
+
|
53
|
+
alias_method :unmarshal, :to_json
|
54
|
+
|
55
|
+
def to_hash(obj)
|
56
|
+
return obj if obj.kind_of?(Hash) || obj.kind_of?(Array)
|
57
|
+
|
58
|
+
if obj.kind_of?(::String)
|
59
|
+
Restfulie::Common::Representation::Json.create(obj)
|
60
|
+
else
|
61
|
+
raise Restfulie::Common::Error::ConverterError.new("It was not possible to convert this object to a Hash")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_s(obj)
|
66
|
+
return obj if obj.kind_of?(String)
|
67
|
+
Restfulie::Common::Representation::Json.create(obj).to_s
|
68
|
+
end
|
69
|
+
|
70
|
+
def marshal(obj, options = nil)
|
71
|
+
return obj if obj.kind_of? String
|
72
|
+
to_json(obj, options).to_json
|
73
|
+
end
|
74
|
+
|
75
|
+
# returns the current helper
|
76
|
+
def helper
|
77
|
+
Helpers
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Restfulie
|
2
|
+
module Common
|
3
|
+
module Converter
|
4
|
+
module Json
|
5
|
+
class Builder
|
6
|
+
def initialize(obj, initial_obj = {})
|
7
|
+
@doc = initial_obj
|
8
|
+
@obj = obj
|
9
|
+
@current = @doc
|
10
|
+
end
|
11
|
+
|
12
|
+
def values(options = nil, &block)
|
13
|
+
yield Values.new(self)
|
14
|
+
end
|
15
|
+
|
16
|
+
def members(options = {}, &block)
|
17
|
+
collection = options[:collection] || @obj
|
18
|
+
raise Error::BuilderError.new("Members method require a collection to execute") unless collection.respond_to?(:each)
|
19
|
+
root = options[:root] || Restfulie::Common::Converter.root_element_for(collection)
|
20
|
+
|
21
|
+
collection.each do |member|
|
22
|
+
node = {}
|
23
|
+
|
24
|
+
parent = @current
|
25
|
+
@current = node
|
26
|
+
block.call(self, member)
|
27
|
+
@current = parent
|
28
|
+
|
29
|
+
add_to_current(root, node)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def link(relationship, uri, options = {})
|
34
|
+
options["rel"] = relationship.to_s
|
35
|
+
options["href"] = uri
|
36
|
+
options["type"] ||= "application/json"
|
37
|
+
insert_value("link", nil, options)
|
38
|
+
end
|
39
|
+
|
40
|
+
def insert_value(name, prefix, *args, &block)
|
41
|
+
node = create_element(block_given?, *args)
|
42
|
+
|
43
|
+
if block_given?
|
44
|
+
parent = @current
|
45
|
+
@current = node
|
46
|
+
block.call
|
47
|
+
@current = parent
|
48
|
+
end
|
49
|
+
|
50
|
+
add_to_current(name, node)
|
51
|
+
end
|
52
|
+
|
53
|
+
def representation
|
54
|
+
Representation::Json.create(@doc).to_json
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def create_element(has_block, *args)
|
60
|
+
vals = []
|
61
|
+
hashes = []
|
62
|
+
|
63
|
+
args.each do |arg|
|
64
|
+
arg.kind_of?(Hash) ? hashes << arg : vals << arg
|
65
|
+
end
|
66
|
+
|
67
|
+
if hashes.empty?
|
68
|
+
# only simple values
|
69
|
+
unless vals.empty?
|
70
|
+
vals = vals.first if vals.size == 1
|
71
|
+
node = has_block ? {} : vals
|
72
|
+
else
|
73
|
+
node = has_block ? {} : nil
|
74
|
+
end
|
75
|
+
else
|
76
|
+
# yes we have hashes
|
77
|
+
node = {}
|
78
|
+
hashes.each { |hash| node.merge!(hash) }
|
79
|
+
unless vals.empty?
|
80
|
+
vals = vals.first if vals.size == 1
|
81
|
+
node = has_block ? {} : [node, vals]
|
82
|
+
end
|
83
|
+
node
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def add_to_current(name, value)
|
88
|
+
if @current[name]
|
89
|
+
if @current[name].kind_of?(Array)
|
90
|
+
@current[name] << value
|
91
|
+
else
|
92
|
+
@current[name] = [@current[name], value]
|
93
|
+
end
|
94
|
+
else
|
95
|
+
@current[name] = value
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Restfulie
|
2
|
+
module Common
|
3
|
+
module Converter
|
4
|
+
module Json
|
5
|
+
module Helpers
|
6
|
+
def collection(obj, *args, &block)
|
7
|
+
Restfulie::Common::Converter::Json.to_json(obj, &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def member(obj, *args, &block)
|
11
|
+
Restfulie::Common::Converter::Json.to_json(obj, &block)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|