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,29 +1,33 @@
1
- module Restfulie::Common::Converter::Components
2
- # This is a Blank Slate class to support the renderization of the values block for Resource Representation Interfaces
3
- # Every Media type should implement a Builder with a insert_value method that renders the values block to a specific format
4
- class Values
5
- attr_accessor :builder
6
-
7
- # BlankSlate
8
- instance_methods.each do |m|
9
- undef_method m unless m.to_s =~ /\[\]|method_missing|respond_to\?|^__/
10
- end
11
-
12
- def initialize(builder)
13
- @builder = builder
14
- @current_prefix = nil
15
- end
16
-
17
- def [](prefix)
18
- @current_prefix = prefix
19
- self
20
- end
21
-
22
- def method_missing(symbol, *args, &block)
23
- name = symbol.to_s
24
- prefix = @current_prefix
25
- @current_prefix = nil
26
- @builder.insert_value(name, prefix, *args, &block)
1
+ module Restfulie
2
+ module Common
3
+ module Converter
4
+ # This is a Blank Slate class to support the renderization of the values block for Resource Representation Interfaces
5
+ # Every Media type should implement a Builder with a insert_value method that renders the values block to a specific format
6
+ class Values
7
+ attr_accessor :builder
8
+
9
+ # BlankSlate
10
+ instance_methods.each do |m|
11
+ undef_method m unless m.to_s =~ /\[\]|method_missing|respond_to\?|^__/
12
+ end
13
+
14
+ def initialize(builder)
15
+ @builder = builder
16
+ @current_prefix = nil
17
+ end
18
+
19
+ def [](prefix)
20
+ @current_prefix = prefix
21
+ self
22
+ end
23
+
24
+ def method_missing(symbol, *args, &block)
25
+ name = symbol.to_s
26
+ prefix = @current_prefix
27
+ @current_prefix = nil
28
+ @builder.insert_value(name, prefix, *args, &block)
29
+ end
30
+ end
27
31
  end
28
32
  end
29
33
  end
@@ -0,0 +1,14 @@
1
+ module Restfulie
2
+ module Common
3
+ module Converter
4
+ module Xml
5
+ autoload :Base, 'restfulie/common/converter/xml/base'
6
+ autoload :Builder, 'restfulie/common/converter/xml/builder'
7
+ autoload :Helpers, 'restfulie/common/converter/xml/helpers'
8
+ autoload :Links, 'restfulie/common/converter/xml/links'
9
+ autoload :Link, 'restfulie/common/converter/xml/link'
10
+ extend Base::ClassMethods
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,61 @@
1
+ module Restfulie
2
+ module Common
3
+ module Converter
4
+ module Xml
5
+ module Base
6
+ module ClassMethods
7
+ mattr_reader :media_type_name
8
+ @@media_type_name = 'application/xml'
9
+
10
+ mattr_reader :headers
11
+ @@headers = {
12
+ :post => { 'Content-Type' => media_type_name }
13
+ }
14
+
15
+ def marshal(entity, options = {})
16
+ to_xml(entity, options)
17
+ end
18
+
19
+ def unmarshal(string)
20
+ Hash.from_xml string
21
+ end
22
+
23
+ mattr_reader :recipes
24
+ @@recipes = {}
25
+
26
+ def describe_recipe(recipe_name, options={}, &block)
27
+ raise 'Undefined recipe' unless block_given?
28
+ raise 'Undefined recipe_name' unless recipe_name
29
+ @@recipes[recipe_name] = block
30
+ end
31
+
32
+ def to_xml(obj, options = {}, &block)
33
+ return obj if obj.kind_of?(String)
34
+
35
+ if block_given?
36
+ recipe = block
37
+ elsif options[:recipe]
38
+ recipe = @@recipes[options[:recipe]]
39
+ elsif obj.kind_of?(Hash) && obj.size==1
40
+ return obj.values.first.to_xml(:root => obj.keys.first)
41
+ else
42
+ return obj.to_xml
43
+ end
44
+
45
+ # Create representation and proxy
46
+ builder = Builder.new(obj)
47
+
48
+ # Check recipe arity size before calling it
49
+ recipe.call(*[builder, obj, options][0,recipe.arity])
50
+ builder.doc.to_xml
51
+ end
52
+
53
+ def helper
54
+ Helpers
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,112 @@
1
+ module Restfulie
2
+ module Common
3
+ module Converter
4
+ module Xml
5
+ # Implements the interface for marshal Xml media type requests (application/xml)
6
+ class Builder
7
+ attr_reader :doc
8
+ def initialize(obj, options = {})
9
+ @doc = Nokogiri::XML::Document.new
10
+ @obj = obj
11
+ root = options[:root] || Restfulie::Common::Converter.root_element_for(obj)
12
+ @parent = @doc.create_element(root)
13
+ @parent.parent = @doc
14
+ end
15
+
16
+ def values(options = {}, &block)
17
+ options.each do |key,value|
18
+ attr = key.to_s
19
+ if attr =~ /^xmlns(:\w+)?$/
20
+ ns = attr.split(":", 2)[1]
21
+ @parent.add_namespace_definition(ns, value)
22
+ end
23
+ end
24
+ yield Values.new(self)
25
+ end
26
+
27
+ def insert_value(name, prefix, *args, &block)
28
+ node = create_element(name.to_s, prefix, *args)
29
+ node.parent = @parent
30
+
31
+ if block_given?
32
+ @parent = node
33
+ block.call
34
+ @parent = node.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/xml"
42
+ insert_value("link", nil, options)
43
+ end
44
+
45
+ def members(a_collection = nil, &block)
46
+ collection = a_collection || @obj
47
+ raise Error::BuilderError("Members method require a collection to execute") unless collection.respond_to?(:each)
48
+ collection.each do |member|
49
+ entry = @doc.create_element(Restfulie::Common::Converter.root_element_for(member))
50
+ entry.parent = @parent
51
+ @parent = entry
52
+ block.call(self, member)
53
+ @parent = entry.parent
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def create_element(node, prefix, *args)
60
+ node = @doc.create_element(node) do |n|
61
+ if prefix
62
+ if namespace = prefix_valid?(prefix)
63
+ # Adding namespace prefix
64
+ n.namespace = namespace
65
+ namespace = nil
66
+ end
67
+ end
68
+
69
+ args.each do |arg|
70
+ case arg
71
+ # Adding XML attributes
72
+ when Hash
73
+ arg.each { |k,v|
74
+ key = k.to_s
75
+ if key =~ /^xmlns(:\w+)?$/
76
+ ns_name = key.split(":", 2)[1]
77
+ n.add_namespace_definition(ns_name, v)
78
+ next
79
+ end
80
+ n[k.to_s] = v.to_s
81
+ }
82
+ # Adding XML node content
83
+ else
84
+ content = if arg.kind_of?(Time) || arg.kind_of?(DateTime)
85
+ arg.xmlschema
86
+ else
87
+ arg
88
+ end
89
+ n.content = content
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ def prefix_valid?(prefix)
96
+ ns = @parent.namespace_definitions.find { |x| x.prefix == prefix.to_s }
97
+
98
+ unless ns
99
+ @parent.ancestors.each do |a|
100
+ next if a == @doc
101
+ ns = a.namespace_definitions.find { |x| x.prefix == prefix.to_s }
102
+ break if ns
103
+ end
104
+ end
105
+
106
+ return ns
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,17 @@
1
+ module Restfulie
2
+ module Common
3
+ module Converter
4
+ module Xml
5
+ module Helpers
6
+ def collection(obj, *args, &block)
7
+ Xml.to_xml(obj, {}, &block)
8
+ end
9
+
10
+ def member(obj, *args, &block)
11
+ Xml.to_xml(obj, {}, &block)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ module Restfulie
2
+ module Common
3
+ module Converter
4
+ module Xml
5
+ class Link
6
+ def initialize(options = {})
7
+ @options = options
8
+ end
9
+ def href
10
+ @options["href"]
11
+ end
12
+ def rel
13
+ @options["rel"]
14
+ end
15
+ def content_type
16
+ @options["type"]
17
+ end
18
+ def type
19
+ content_type
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module Restfulie
2
+ module Common
3
+ module Converter
4
+ module Xml
5
+ class Links
6
+ def initialize(links)
7
+ links = [links] unless links.kind_of? Array
8
+ links = [] unless links
9
+ @links = links.map do |l|
10
+ link = Restfulie::Common::Converter::Xml::Link.new(l)
11
+ link.instance_eval { self.class.send :include, ::Restfulie::Client::HTTP::LinkRequestBuilder }
12
+ link
13
+ end
14
+ end
15
+
16
+ def method_missing(sym, *args)
17
+ @links.find do |link|
18
+ link.rel == sym.to_s
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,5 +1 @@
1
- %w(
2
- proc
3
- ).each do |file|
4
- require File.join(File.dirname(__FILE__), 'core_ext', file)
5
- end
1
+ require 'restfulie/common/core_ext/hash'
@@ -0,0 +1,12 @@
1
+ class Hash
2
+ def links(*args)
3
+ links = fetch("link", [])
4
+ Restfulie::Common::Converter::Xml::Links.new(links)
5
+ end
6
+ def method_missing(sym, *args)
7
+ self[sym.to_s].nil? ? super(sym, args) : self[sym.to_s]
8
+ end
9
+ def respond_to?(sym)
10
+ include?(sym.to_s) || super(sym)
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module Restfulie
2
+ module Common
3
+ module Error
4
+ class RestfulieError < StandardError; end
5
+ class MarshallingError < RestfulieError; end
6
+ class UndefinedMarshallingError < MarshallingError; end
7
+
8
+ # Atom marshallinh error
9
+ class AtomMarshallingError < MarshallingError; end
10
+ class NameSpaceError < AtomMarshallingError; end
11
+
12
+ # Converter
13
+ class ConverterError < RestfulieError; end
14
+
15
+ # builder
16
+ class BuilderError < RestfulieError; end
17
+ end
18
+ end
19
+ end
@@ -1,11 +1,19 @@
1
- module Restfulie::Common::Logger
2
- # Configure the logger used by Restfulie
3
- #
4
- # The logger defaults to ActiveSupport::BufferedLogger.new(STDOUT)
5
- class << self
6
- attr_accessor :logger
1
+ module Restfulie
2
+ module Common
3
+ class Logger
4
+ # Configure the logger used by Restfulie
5
+ #
6
+ # The logger defaults to ActiveSupport::BufferedLogger.new(STDOUT)
7
+ def self.logger
8
+ @@logger
9
+ end
10
+
11
+ def self.logger=(value)
12
+ @@logger = value
13
+ end
14
+
15
+ @@logger = ActiveSupport::BufferedLogger.new(STDOUT)
16
+ @@logger.level = ::Logger::DEBUG
17
+ end
7
18
  end
8
19
  end
9
-
10
- Restfulie::Common::Logger.logger = ActiveSupport::BufferedLogger.new(STDOUT)
11
- Restfulie::Common::Logger.logger.level = Logger::DEBUG
@@ -1,11 +1,10 @@
1
- #initialize namespace
2
- module Restfulie::Common::Representation; end
3
-
4
- %w(
5
- atom
6
- generic
7
- json
8
- xml
9
- ).each do |file|
10
- require File.join(File.dirname(__FILE__), 'representation', file)
1
+ module Restfulie
2
+ module Common
3
+ module Representation
4
+ autoload :Atom, 'restfulie/common/representation/atom'
5
+ autoload :Generic, 'restfulie/common/representation/generic'
6
+ autoload :Json, 'restfulie/common/representation/json'
7
+ autoload :XmlD, 'restfulie/common/representation/xml'
8
+ end
9
+ end
11
10
  end
@@ -1,52 +1,20 @@
1
1
  require 'nokogiri'
2
2
 
3
- #initialize namespace
4
- module Restfulie::Common::Representation::Atom
5
-
6
- # Create a new Representation::Atom object using a +string_or_io+
7
- # object.
8
- #
9
- # Examples
10
- # xml = IO.read("spec/units/lib/atoms/full_atom.xml")
11
- # atom = Restfulie::Common::Representation::Atom.new(xml)
12
- class Factory
13
- # RelaxNG file to validate atom
14
- RELAXNG_ATOM = File.join(File.dirname(__FILE__), 'atom', 'atom.rng')
15
-
16
- if Nokogiri::VERSION_INFO["libxml"]["loaded"] < "2.7.7"
17
- puts "WARNING! In order to use schema validation on atom representations you need libxml version 2.7.7 or superior loaded in your system."
18
- SCHEMA = nil
19
- else
20
- SCHEMA = ::Nokogiri::XML::RelaxNG(File.open(RELAXNG_ATOM))
3
+ module Restfulie
4
+ module Common
5
+ module Representation
6
+ module Atom
7
+ autoload :Factory, 'restfulie/common/representation/atom/factory'
8
+ autoload :XML, 'restfulie/common/representation/atom/xml'
9
+ autoload :Base, 'restfulie/common/representation/atom/base'
10
+ autoload :TagCollection, 'restfulie/common/representation/atom/tag_collection'
11
+ autoload :Link, 'restfulie/common/representation/atom/link'
12
+ autoload :Person, 'restfulie/common/representation/atom/person'
13
+ autoload :Category, 'restfulie/common/representation/atom/category'
14
+ autoload :Feed, 'restfulie/common/representation/atom/feed'
15
+ autoload :Entry, 'restfulie/common/representation/atom/entry'
16
+ autoload :Source, 'restfulie/common/representation/atom/source'
17
+ end
21
18
  end
22
-
23
- class << self
24
- def create(string_or_io)
25
- doc = string_or_io.kind_of?(Nokogiri::XML::Document) ? string_or_io : Nokogiri::XML(string_or_io)
26
-
27
- if SCHEMA && !(errors = SCHEMA.validate(doc)).empty?
28
- raise Restfulie::Common::Representation::Atom::AtomInvalid.new("Invalid Atom: "+ errors.join(", "))
29
- end
30
-
31
- if doc.root.name == "feed"
32
- Restfulie::Common::Representation::Atom::Feed.new(doc)
33
- elsif doc.root.name == "entry"
34
- Restfulie::Common::Representation::Atom::Entry.new(doc)
35
- end
36
- end
37
- end
38
-
39
19
  end
40
-
41
- class AtomInvalid < StandardError; end
42
-
43
20
  end
44
-
45
- %w(
46
- base
47
- feed
48
- entry
49
- ).each do |file|
50
- require File.join(File.dirname(__FILE__), 'atom', file)
51
- end
52
-