restfulie 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. data/Gemfile +10 -5
  2. data/Rakefile +5 -2
  3. data/lib/restfulie.rb +10 -5
  4. data/lib/restfulie/client.rb +13 -9
  5. data/lib/restfulie/client/base.rb +24 -65
  6. data/lib/restfulie/client/configuration.rb +62 -64
  7. data/lib/restfulie/client/entry_point.rb +36 -0
  8. data/lib/restfulie/client/ext/atom_ext.rb +12 -0
  9. data/lib/restfulie/client/ext/http_ext.rb +22 -0
  10. data/lib/restfulie/client/ext/json_ext.rb +12 -0
  11. data/lib/restfulie/client/ext/xml_ext.rb +4 -0
  12. data/lib/restfulie/client/http.rb +25 -13
  13. data/lib/restfulie/client/http/cache.rb +22 -22
  14. data/lib/restfulie/client/http/error.rb +70 -70
  15. data/lib/restfulie/client/http/link_request_builder.rb +15 -0
  16. data/lib/restfulie/client/http/request_adapter.rb +209 -0
  17. data/lib/restfulie/client/http/request_builder.rb +107 -0
  18. data/lib/restfulie/client/http/request_builder_executor.rb +24 -0
  19. data/lib/restfulie/client/http/request_executor.rb +17 -0
  20. data/lib/restfulie/client/http/request_follow.rb +42 -0
  21. data/lib/restfulie/client/http/request_follow_executor.rb +10 -0
  22. data/lib/restfulie/client/http/request_history.rb +69 -0
  23. data/lib/restfulie/client/http/request_history_executor.rb +10 -0
  24. data/lib/restfulie/client/http/request_marshaller.rb +127 -0
  25. data/lib/restfulie/client/http/request_marshaller_executor.rb +10 -0
  26. data/lib/restfulie/client/http/response.rb +23 -0
  27. data/lib/restfulie/client/http/response_handler.rb +67 -0
  28. data/lib/restfulie/client/http/response_holder.rb +9 -0
  29. data/lib/restfulie/client/mikyung.rb +17 -14
  30. data/lib/restfulie/client/mikyung/concatenator.rb +15 -12
  31. data/lib/restfulie/client/mikyung/core.rb +65 -39
  32. data/lib/restfulie/client/mikyung/languages.rb +8 -26
  33. data/lib/restfulie/client/mikyung/languages/german.rb +24 -0
  34. data/lib/restfulie/client/mikyung/languages/portuguese.rb +23 -0
  35. data/lib/restfulie/client/mikyung/rest_process_model.rb +184 -107
  36. data/lib/restfulie/client/mikyung/steady_state_walker.rb +34 -28
  37. data/lib/restfulie/client/mikyung/then_condition.rb +33 -27
  38. data/lib/restfulie/client/mikyung/when_condition.rb +53 -49
  39. data/lib/restfulie/common.rb +7 -12
  40. data/lib/restfulie/common/converter.rb +20 -9
  41. data/lib/restfulie/common/converter/atom.rb +8 -83
  42. data/lib/restfulie/common/converter/atom/base.rb +89 -0
  43. data/lib/restfulie/common/converter/atom/builder.rb +101 -99
  44. data/lib/restfulie/common/converter/atom/helpers.rb +16 -8
  45. data/lib/restfulie/common/converter/json.rb +12 -0
  46. data/lib/restfulie/common/converter/json/base.rb +84 -0
  47. data/lib/restfulie/common/converter/json/builder.rb +102 -0
  48. data/lib/restfulie/common/converter/json/helpers.rb +17 -0
  49. data/lib/restfulie/common/converter/values.rb +30 -26
  50. data/lib/restfulie/common/converter/xml.rb +14 -0
  51. data/lib/restfulie/common/converter/xml/base.rb +61 -0
  52. data/lib/restfulie/common/converter/xml/builder.rb +112 -0
  53. data/lib/restfulie/common/converter/xml/helpers.rb +17 -0
  54. data/lib/restfulie/common/converter/xml/link.rb +25 -0
  55. data/lib/restfulie/common/converter/xml/links.rb +25 -0
  56. data/lib/restfulie/common/core_ext.rb +1 -5
  57. data/lib/restfulie/common/core_ext/hash.rb +12 -0
  58. data/lib/restfulie/common/error.rb +19 -0
  59. data/lib/restfulie/common/logger.rb +17 -9
  60. data/lib/restfulie/common/representation.rb +9 -10
  61. data/lib/restfulie/common/representation/atom.rb +15 -47
  62. data/lib/restfulie/common/representation/atom/base.rb +122 -365
  63. data/lib/restfulie/common/representation/atom/category.rb +41 -0
  64. data/lib/restfulie/common/representation/atom/entry.rb +52 -100
  65. data/lib/restfulie/common/representation/atom/factory.rb +43 -0
  66. data/lib/restfulie/common/representation/atom/feed.rb +103 -99
  67. data/lib/restfulie/common/representation/atom/link.rb +68 -0
  68. data/lib/restfulie/common/representation/atom/person.rb +48 -0
  69. data/lib/restfulie/common/representation/atom/source.rb +59 -0
  70. data/lib/restfulie/common/representation/atom/tag_collection.rb +38 -0
  71. data/lib/restfulie/common/representation/atom/xml.rb +95 -0
  72. data/lib/restfulie/common/representation/generic.rb +30 -29
  73. data/lib/restfulie/common/representation/json.rb +10 -22
  74. data/lib/restfulie/common/representation/json/base.rb +27 -0
  75. data/lib/restfulie/common/representation/json/keys_as_methods.rb +72 -0
  76. data/lib/restfulie/common/representation/json/link.rb +29 -0
  77. data/lib/restfulie/common/representation/json/link_collection.rb +23 -0
  78. data/lib/restfulie/common/representation/xml.rb +18 -227
  79. data/lib/restfulie/server.rb +9 -10
  80. data/lib/restfulie/server/action_controller.rb +10 -12
  81. data/lib/restfulie/server/action_controller/base.rb +18 -15
  82. data/lib/restfulie/server/action_controller/{routing/patch.rb → patch.rb} +0 -0
  83. data/lib/restfulie/server/action_controller/restful_responder.rb +43 -35
  84. data/lib/restfulie/server/action_view.rb +8 -6
  85. data/lib/restfulie/server/action_view/helpers.rb +47 -41
  86. data/lib/restfulie/server/action_view/template_handlers.rb +24 -12
  87. data/lib/restfulie/server/action_view/template_handlers/tokamak.rb +17 -12
  88. data/lib/restfulie/server/configuration.rb +22 -19
  89. data/lib/restfulie/server/{restfulie_controller.rb → controller.rb} +1 -10
  90. data/lib/restfulie/server/core_ext.rb +1 -1
  91. data/lib/restfulie/version.rb +14 -0
  92. metadata +52 -16
  93. data/lib/restfulie/client/http/adapter.rb +0 -502
  94. data/lib/restfulie/client/http/atom_ext.rb +0 -4
  95. data/lib/restfulie/client/http/core_ext.rb +0 -6
  96. data/lib/restfulie/client/http/core_ext/http.rb +0 -19
  97. data/lib/restfulie/client/http/marshal.rb +0 -145
  98. data/lib/restfulie/client/http/xml_ext.rb +0 -7
  99. data/lib/restfulie/common/core_ext/proc.rb +0 -48
  100. data/lib/restfulie/common/errors.rb +0 -15
  101. data/lib/restfulie/server/action_controller/routing.rb +0 -12
  102. data/lib/restfulie/server/action_controller/routing/restful_route.rb +0 -14
@@ -1,109 +1,111 @@
1
- module Restfulie::Common::Converter::Atom
2
- class Builder
3
- attr_accessor :atom_type
4
-
5
- def initialize(atom_type, obj)
6
- @doc = Nokogiri::XML::Document.new
7
- @obj = obj
8
- @parent = @doc.create_element(atom_type.to_s)
9
- @parent.add_namespace_definition(nil, "http://www.w3.org/2005/Atom")
10
- @parent.parent = @doc
11
- end
12
-
13
- def values(options = nil, &block)
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
- yield Restfulie::Common::Converter::Components::Values.new(self)
23
- end
24
-
25
- def members(a_collection = nil, &block)
26
- collection = a_collection || @obj
27
- raise Restfulie::Common::Error::BuilderError("Members method require a collection to execute") unless collection.respond_to?(:each)
28
- collection.each do |member|
29
- entry = @doc.create_element("entry")
30
- entry.parent = @parent
31
- @parent = entry
32
- block.call(self, member)
33
- @parent = entry.parent
34
- end
35
- end
36
-
37
- def link(relationship, uri, options = {})
38
- options["rel"] = relationship.to_s
39
- options["href"] = uri
40
- insert_value("link", nil, options)
41
- end
42
-
43
- def insert_value(name, prefix, *args, &block)
44
- node = create_element(name.to_s, prefix, *args)
45
- node.parent = @parent
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
- if block_given?
48
- @parent = node
49
- block.call
50
- @parent = node.parent
51
- end
52
- end
53
-
54
- def representation
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
- end
69
-
70
- args.each do |arg|
71
- case arg
72
- # Adding XML attributes
73
- when Hash
74
- arg.each { |k,v|
75
- key = k.to_s
76
- if key =~ /^xmlns(:\w+)?$/
77
- ns_name = key.split(":", 2)[1]
78
- n.add_namespace_definition(ns_name, v)
79
- next
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
- n[k.to_s] = v.to_s
82
- }
83
- # Adding XML node content
84
- else
85
- arg.kind_of?(Time) || arg.kind_of?(DateTime) ? content = arg.xmlschema : content = arg
86
- n.content = content
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::Common::Converter::Atom::Helpers
2
- def collection(obj, *args, &block)
3
- Restfulie::Common::Converter::Atom.to_atom(obj, :atom_type => :feed, &block)
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