restfulie 0.1.0.beta1 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/README.textile +87 -99
  2. data/Rakefile +14 -109
  3. data/lib/restfulie.rb +183 -23
  4. metadata +12 -197
  5. data/Gemfile +0 -28
  6. data/Gemfile.lock +0 -128
  7. data/LICENSE +0 -17
  8. data/lib/restfulie/client.rb +0 -26
  9. data/lib/restfulie/client/base.rb +0 -36
  10. data/lib/restfulie/client/cache.rb +0 -11
  11. data/lib/restfulie/client/cache/basic.rb +0 -76
  12. data/lib/restfulie/client/cache/fake.rb +0 -15
  13. data/lib/restfulie/client/cache/http_ext.rb +0 -123
  14. data/lib/restfulie/client/cache/restrictions.rb +0 -13
  15. data/lib/restfulie/client/configuration.rb +0 -67
  16. data/lib/restfulie/client/dsl.rb +0 -66
  17. data/lib/restfulie/client/entry_point.rb +0 -61
  18. data/lib/restfulie/client/ext/atom_ext.rb +0 -14
  19. data/lib/restfulie/client/ext/http_ext.rb +0 -22
  20. data/lib/restfulie/client/ext/json_ext.rb +0 -16
  21. data/lib/restfulie/client/feature.rb +0 -5
  22. data/lib/restfulie/client/feature/base.rb +0 -75
  23. data/lib/restfulie/client/feature/base_request.rb +0 -35
  24. data/lib/restfulie/client/feature/cache.rb +0 -16
  25. data/lib/restfulie/client/feature/enhance_response.rb +0 -12
  26. data/lib/restfulie/client/feature/follow_request.rb +0 -41
  27. data/lib/restfulie/client/feature/history.rb +0 -26
  28. data/lib/restfulie/client/feature/history_request.rb +0 -19
  29. data/lib/restfulie/client/feature/open_search.rb +0 -21
  30. data/lib/restfulie/client/feature/open_search/pattern_matcher.rb +0 -25
  31. data/lib/restfulie/client/feature/serialize_body.rb +0 -32
  32. data/lib/restfulie/client/feature/setup_header.rb +0 -22
  33. data/lib/restfulie/client/feature/throw_error.rb +0 -41
  34. data/lib/restfulie/client/feature/verb.rb +0 -119
  35. data/lib/restfulie/client/http.rb +0 -7
  36. data/lib/restfulie/client/http/cache.rb +0 -28
  37. data/lib/restfulie/client/http/error.rb +0 -77
  38. data/lib/restfulie/client/http/response_holder.rb +0 -29
  39. data/lib/restfulie/client/master_delegator.rb +0 -31
  40. data/lib/restfulie/client/mikyung.rb +0 -15
  41. data/lib/restfulie/client/mikyung/concatenator.rb +0 -18
  42. data/lib/restfulie/client/mikyung/core.rb +0 -70
  43. data/lib/restfulie/client/mikyung/languages.rb +0 -11
  44. data/lib/restfulie/client/mikyung/languages/german.rb +0 -24
  45. data/lib/restfulie/client/mikyung/languages/portuguese.rb +0 -23
  46. data/lib/restfulie/client/mikyung/rest_process_model.rb +0 -191
  47. data/lib/restfulie/client/mikyung/steady_state_walker.rb +0 -38
  48. data/lib/restfulie/client/mikyung/then_condition.rb +0 -39
  49. data/lib/restfulie/client/mikyung/when_condition.rb +0 -57
  50. data/lib/restfulie/common.rb +0 -18
  51. data/lib/restfulie/common/converter.rb +0 -43
  52. data/lib/restfulie/common/converter/atom.rb +0 -12
  53. data/lib/restfulie/common/converter/atom/base.rb +0 -91
  54. data/lib/restfulie/common/converter/atom/builder.rb +0 -111
  55. data/lib/restfulie/common/converter/atom/helpers.rb +0 -17
  56. data/lib/restfulie/common/converter/json.rb +0 -12
  57. data/lib/restfulie/common/converter/json/base.rb +0 -87
  58. data/lib/restfulie/common/converter/json/builder.rb +0 -102
  59. data/lib/restfulie/common/converter/json/helpers.rb +0 -17
  60. data/lib/restfulie/common/converter/open_search.rb +0 -16
  61. data/lib/restfulie/common/converter/open_search/descriptor.rb +0 -32
  62. data/lib/restfulie/common/converter/values.rb +0 -33
  63. data/lib/restfulie/common/converter/xml.rb +0 -14
  64. data/lib/restfulie/common/converter/xml/base.rb +0 -63
  65. data/lib/restfulie/common/converter/xml/builder.rb +0 -113
  66. data/lib/restfulie/common/converter/xml/helpers.rb +0 -17
  67. data/lib/restfulie/common/converter/xml/link.rb +0 -30
  68. data/lib/restfulie/common/converter/xml/links.rb +0 -21
  69. data/lib/restfulie/common/core_ext.rb +0 -1
  70. data/lib/restfulie/common/core_ext/hash.rb +0 -18
  71. data/lib/restfulie/common/error.rb +0 -19
  72. data/lib/restfulie/common/links.rb +0 -9
  73. data/lib/restfulie/common/logger.rb +0 -19
  74. data/lib/restfulie/common/representation.rb +0 -3
  75. data/lib/restfulie/common/representation/atom.rb +0 -20
  76. data/lib/restfulie/common/representation/atom/atom.rng +0 -597
  77. data/lib/restfulie/common/representation/atom/base.rb +0 -142
  78. data/lib/restfulie/common/representation/atom/category.rb +0 -41
  79. data/lib/restfulie/common/representation/atom/entry.rb +0 -59
  80. data/lib/restfulie/common/representation/atom/factory.rb +0 -43
  81. data/lib/restfulie/common/representation/atom/feed.rb +0 -110
  82. data/lib/restfulie/common/representation/atom/link.rb +0 -68
  83. data/lib/restfulie/common/representation/atom/person.rb +0 -48
  84. data/lib/restfulie/common/representation/atom/source.rb +0 -59
  85. data/lib/restfulie/common/representation/atom/tag_collection.rb +0 -38
  86. data/lib/restfulie/common/representation/atom/xml.rb +0 -90
  87. data/lib/restfulie/common/representation/generic.rb +0 -22
  88. data/lib/restfulie/common/representation/json.rb +0 -13
  89. data/lib/restfulie/common/representation/json/base.rb +0 -27
  90. data/lib/restfulie/common/representation/json/keys_as_methods.rb +0 -74
  91. data/lib/restfulie/common/representation/json/link.rb +0 -29
  92. data/lib/restfulie/common/representation/json/link_collection.rb +0 -23
  93. data/lib/restfulie/common/representation/links.rb +0 -11
  94. data/lib/restfulie/server.rb +0 -25
  95. data/lib/restfulie/server/action_controller.rb +0 -11
  96. data/lib/restfulie/server/action_controller/base.rb +0 -48
  97. data/lib/restfulie/server/action_controller/params_parser.rb +0 -100
  98. data/lib/restfulie/server/action_controller/patch.rb +0 -6
  99. data/lib/restfulie/server/action_controller/restful_responder.rb +0 -12
  100. data/lib/restfulie/server/action_controller/trait.rb +0 -9
  101. data/lib/restfulie/server/action_controller/trait/cacheable.rb +0 -81
  102. data/lib/restfulie/server/action_controller/trait/created.rb +0 -17
  103. data/lib/restfulie/server/action_view.rb +0 -10
  104. data/lib/restfulie/server/action_view/helpers.rb +0 -50
  105. data/lib/restfulie/server/action_view/template_handlers.rb +0 -30
  106. data/lib/restfulie/server/action_view/template_handlers/tokamak.rb +0 -21
  107. data/lib/restfulie/server/configuration.rb +0 -24
  108. data/lib/restfulie/server/controller.rb +0 -74
  109. data/lib/restfulie/server/core_ext.rb +0 -1
  110. data/lib/restfulie/server/core_ext/array.rb +0 -61
  111. data/lib/restfulie/version.rb +0 -14
@@ -1,23 +0,0 @@
1
- module Restfulie
2
- module Client
3
- module Mikyung
4
- module Languages
5
- module Portuguese
6
- def Quando(concat, &block)
7
- When(concat, &block)
8
- end
9
- def E(concat, &block)
10
- And(concat, &block)
11
- end
12
- def Mas(concat, &block)
13
- But(concat, &block)
14
- end
15
- def Entao(concat, &block)
16
- Then(concat, &block)
17
- end
18
- end
19
- end
20
- end
21
- end
22
- end
23
-
@@ -1,191 +0,0 @@
1
- # a configuration error
2
- module Restfulie
3
- module Client
4
- module Mikyung
5
- class ConfigurationError < Restfulie::Common::Error::RestfulieError
6
- end
7
-
8
- class CompletionCriteriaMissingError < Restfulie::Common::Error::RestfulieError
9
- end
10
-
11
- class StepsFileNotFoundError < Restfulie::Common::Error::RestfulieError
12
- end
13
-
14
- class ScenarioFileNotFoundError < StepsFileNotFoundError
15
- end
16
-
17
- # Provides a DSL to build your process in a human readable way.
18
- #
19
- # Example:
20
- # When there is a machine
21
- # And already installed
22
- # Then reboot
23
- #
24
- # Before creating your DSL you should provide your method content:
25
- #
26
- # When /there (are|is an|is a|is) (.*)/ do |resource, regex|
27
- # resource.keys.first==regex[2]
28
- # end
29
- #
30
- # When "already installed" do |resource|
31
- # @installed
32
- # end
33
- #
34
- # Then "reboot" do |resource|
35
- # resource.machine.boot.post! :boot => {:reason => "Installed #{@software[:name]}"}
36
- # end
37
- class RestProcessModel
38
- @@at = ""
39
- @@follow = false
40
- @@accept = "application/atom+xml"
41
- @@current_dir = ""
42
-
43
- def self.at(uri)
44
- @@at = uri
45
- end
46
-
47
- def self.follow(bool)
48
- @@follow = bool
49
- end
50
-
51
- def self.accept(type)
52
- @@accept = type
53
- end
54
-
55
- def self.current_dir(dir)
56
- @@current_dir = dir
57
- end
58
-
59
- def self.get_restfulie
60
- Restfulie.at(@@at).tap do |client|
61
- client.follow if @@follow
62
- client.accepts(@@accept) if @@accept
63
- end
64
- end
65
-
66
- # concatenates anything to a current expression
67
- def method_missing(sym, *args)
68
- Restfulie::Client::Mikyung::Concatenator.new(sym.to_s, *args)
69
- end
70
-
71
- # the list of results
72
- def then_rules
73
- @then_rules ||= []
74
- end
75
-
76
- # the list of conditions
77
- def conditions
78
- @conditions ||= []
79
- end
80
-
81
- # the list of conditional rules
82
- def when_rules
83
- @when_rules ||= []
84
- end
85
-
86
- # creates a When rule or block
87
- #
88
- # When blocks should return true or false whether the current resource matches what you expect:
89
- # When /there (are|is an|is a|is) (.*)/ do |resource, regex|
90
- # resource.keys.first==regex[2]
91
- # end
92
- #
93
- # When rules will group conditions and rules together:
94
- # When there is a machine
95
- # And already installed
96
- # Then reboot
97
- def When(concat, &block)
98
- if concat.respond_to? :content
99
- @condition = when_factory(concat)
100
- conditions << @condition
101
- else
102
- when_rules << [concat, block]
103
- end
104
- end
105
-
106
- # Adds a constraint to the current scenario
107
- def And(concat)
108
- @condition.and when_factory(concat)
109
- end
110
-
111
- # Adds a negative constraint to the current scenario
112
- def But(concat)
113
- @condition.but when_factory(concat)
114
- end
115
-
116
- # Creates a result rule
117
- #
118
- # example:
119
- # Then "reboot" do |resource|
120
- # resource.machine.boot.post! :boot => {:reason => "Installed #{@software[:name]}"}
121
- # end
122
- def Then(concat, &block)
123
- if concat.respond_to? :content
124
- @condition.results_on Restfulie::Client::Mikyung::ThenCondition.new(concat.content)
125
- else
126
- then_rules << [concat, block]
127
- end
128
- end
129
-
130
- # Goes through every scenario and finds which one fits the current server steady state.
131
- # Picks this step and executes the business rule attached to it.
132
- def next_step(resource, mikyung)
133
- conditions.each do |c|
134
- if c.should_run_for(resource, self)
135
- return c.execute(resource, self, mikyung)
136
- end
137
- end
138
- nil
139
- end
140
-
141
- # load step definitions from the 'steps/[class_name].rb'
142
- # otherwise you can simply override this method with a module
143
- def steps
144
- unless @steps_loaded
145
- step_file = File.expand_path("./steps/#{self.class.name.underscore}.rb", @@current_dir)
146
- if File.exists?(step_file)
147
- self.instance_eval File.read(step_file), __FILE__, __LINE__ + 1
148
- else
149
- raise StepsFileNotFoundError.new("File #{step_file} not found")
150
- end
151
- @steps_loaded = true
152
- end
153
- end
154
-
155
- # load scenario definition from 'scenarios/[class_name].scenario'
156
- # otherwise you can simply override this method with a module
157
- def scenario
158
- unless @scenarios_loaded
159
- scenario_file = File.expand_path("./scenarios/#{self.class.name.underscore}.scenario", @@current_dir)
160
- if File.exists?(scenario_file)
161
- self.instance_eval File.read(scenario_file), __FILE__, __LINE__ + 1
162
- else
163
- raise ScenarioFileNotFoundError.new("File #{scenario_file} not found")
164
- end
165
- @scenarios_loaded = true
166
- end
167
- end
168
-
169
- # you need to override this method to provide a completion
170
- # criteria. Will raise an error otherwise
171
- def completed?(resource)
172
- raise CompletionCriteriaMissingError.new
173
- end
174
-
175
- private
176
-
177
- def when_factory(concat)
178
- rule = when_rules.find do |rule|
179
- concat.content.match(rule[0])
180
- end
181
- if rule.nil?
182
- raise Restfulie::Client::Mikyung::ConfigurationError, "You forgot to create '#{concat.content}' prior to its usage."
183
- end
184
- Restfulie::Client::Mikyung::WhenCondition.new(concat.content, rule, concat.content.match(rule[0]))
185
- end
186
- end
187
- end
188
- end
189
- end
190
-
191
-
@@ -1,38 +0,0 @@
1
- # A steady walker that tries 3 times each step
2
- module Restfulie
3
- module Client
4
- module Mikyung
5
- class SteadyStateWalker
6
- def move(goal, current, mikyung)
7
- step = goal.next_step(current, mikyung)
8
- raise UnableToAchieveGoalError, "No step was found for #{current}" unless step
9
- Common::Logger.logger.debug "Mikyung > next step will be #{step}"
10
- step = step.new if step.kind_of? Class
11
- try_to_execute(step, current, 3, mikyung)
12
- end
13
-
14
- private
15
-
16
- def try_to_execute(step, current, max_attempts, mikyung)
17
- raise "Unable to proceed when trying to #{step}" if max_attempts == 0
18
-
19
- resource = step
20
- raise "Step returned 'give up'" if resource.nil?
21
-
22
- if step.respond_to?(:execute)
23
- resource = step.execute(current, mikyung)
24
- end
25
-
26
- # TODO: should it really retry if it is not 200? Or only if it is on the 50x family?
27
- # for instance the result could be a 302 redirection
28
- unless resource.response.code == 200
29
- try_to_execute(step, current, max_attempts-1, mikyung)
30
- else
31
- Common::Logger.logger.debug resource.response.body
32
- resource
33
- end
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,39 +0,0 @@
1
- # A conclusion to a step.
2
- #
3
- # Whenever a step rule matches, there are a series of conditions to be executed.
4
- module Restfulie
5
- module Client
6
- module Mikyung
7
- class ThenCondition
8
- attr_reader :description
9
-
10
- # creates a new result, based on this description
11
- def initialize(description)
12
- @description = description
13
- end
14
-
15
- # finds the rule for this result and executes it
16
- def execute(resource, goal, mikyung)
17
- goal.then_rules.each do |rule|
18
- if (matches = Regexp.new(rule[0]).match(@description))
19
- return invoke_rule(rule[1], resource, matches, mikyung)
20
- end
21
- end
22
- nil
23
- end
24
-
25
- private
26
- def invoke_rule(rule, resource, matches, mikyung)
27
- case rule.arity
28
- when 1
29
- rule.call(resource)
30
- when 2
31
- rule.call(resource, matches)
32
- else
33
- rule.call(resource, matches, mikyung)
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,57 +0,0 @@
1
- # Checks whether one or more rule holds and is capable of executing results.
2
- module Restfulie
3
- module Client
4
- module Mikyung
5
- # Creates a conditional execution based on a description.
6
- # Its rule is an array where its first element represents a rule name and second a lambda that returns true or false
7
- # Its params are extra params that might be passed to the rule
8
- # Example
9
- # WhenCondition.new("when running", ["", lambda { |resource| resource.human.state=='running' }], "")
10
- class WhenCondition
11
- def initialize(description, rule, params)
12
- @description = description
13
- @results = []
14
- @extra = []
15
- @rule = rule
16
- @params = params
17
- end
18
-
19
- # will execute the first attached result
20
- def execute(resource, goal, mikyung)
21
- @results.each do |result|
22
- Restfulie::Common::Logger.logger.info("will '#{result.description}'")
23
- return result.execute(resource, goal, mikyung)
24
- end
25
- end
26
-
27
- # checks whether this step should execute for a specific resource
28
- def should_run_for(resource, goal)
29
- if @rule[1].arity == 2
30
- rule_accepts = @rule[1].call(resource, @params)
31
- else
32
- rule_accepts = @rule[1].call(resource)
33
- end
34
- return false unless rule_accepts
35
- !@extra.find do |condition|
36
- !condition.should_run_for(resource, goal)
37
- end
38
- end
39
-
40
- # adds an extra condition to this step
41
- def and(condition)
42
- @extra << condition
43
- end
44
-
45
- # adds an extra condition to this step
46
- def but(condition)
47
- @extra << condition
48
- end
49
-
50
- # adds an extra result to this step
51
- def results_on(result)
52
- @results << result
53
- end
54
- end
55
- end
56
- end
57
- end
@@ -1,18 +0,0 @@
1
- require 'net/http'
2
- require 'uri'
3
-
4
- require 'rubygems'
5
- require 'active_support'
6
- require 'action_controller'
7
-
8
- module Restfulie
9
- module Common
10
- autoload :Error, 'restfulie/common/error'
11
- autoload :Links, 'restfulie/common/links'
12
- autoload :Logger, 'restfulie/common/logger'
13
- autoload :Representation, 'restfulie/common/representation'
14
- autoload :Converter, 'restfulie/common/converter'
15
- end
16
- end
17
-
18
- require 'restfulie/common/core_ext'
@@ -1,43 +0,0 @@
1
- module Restfulie
2
- module Common
3
- module Converter
4
- Dir["#{File.dirname(__FILE__)}/converter/*.rb"].each {|f| autoload File.basename(f)[0..-4].camelize.to_sym, f }
5
-
6
- # Returns the default root element name for an item or collection
7
- def self.root_element_for(obj)
8
- if obj.kind_of?(Hash) && obj.size==1
9
- obj.keys.first.to_s
10
- elsif obj.kind_of?(Array) && !obj.empty?
11
- root_element_for(obj.first).to_s.underscore.pluralize
12
- else
13
- obj.class.to_s.underscore
14
- end
15
- end
16
-
17
- def self.register(media_type,representation)
18
- representations[media_type] = representation
19
- end
20
-
21
- def self.content_type_for(media_type)
22
- return nil unless media_type
23
- content_type = media_type.split(';')[0] # [/(.*?);/, 1]
24
- representations[content_type]
25
- end
26
-
27
- private
28
-
29
- def self.representations
30
- @representations ||= {}
31
- end
32
-
33
- register 'application/atom+xml' , ::Restfulie::Common::Converter::Atom
34
- register 'application/xml' , ::Restfulie::Common::Converter::Xml
35
- register 'text/xml' , ::Restfulie::Common::Converter::Xml
36
- register 'application/json' , ::Restfulie::Common::Converter::Json
37
- register 'application/opensearchdescription+xml' , ::Restfulie::Common::Converter::OpenSearch
38
-
39
-
40
- end
41
- end
42
- end
43
-
@@ -1,12 +0,0 @@
1
- module Restfulie
2
- module Common
3
- module Converter
4
- module Atom
5
- autoload :Base, 'restfulie/common/converter/atom/base'
6
- autoload :Builder, 'restfulie/common/converter/atom/builder'
7
- autoload :Helpers, 'restfulie/common/converter/atom/helpers'
8
- extend Base::ClassMethods
9
- end
10
- end
11
- end
12
- end
@@ -1,91 +0,0 @@
1
- require 'active_support/core_ext/hash/conversions'
2
-
3
- module Restfulie
4
- module Common
5
- module Converter
6
- module Atom
7
- module Base
8
- module ClassMethods
9
- mattr_reader :media_type_name
10
- @@media_type_name = 'application/atom+xml'
11
-
12
- mattr_reader :headers
13
- @@headers = {
14
- :get => { 'Accept' => media_type_name },
15
- :post => { 'Content-Type' => media_type_name }
16
- }
17
-
18
- mattr_reader :recipes
19
- @@recipes = {}
20
-
21
- def helper
22
- Restfulie::Common::Converter::Atom::Helpers
23
- end
24
-
25
- def describe_recipe(recipe_name, options={}, &block)
26
- raise 'Undefined recipe' unless block_given?
27
- raise 'Undefined recipe_name' unless recipe_name
28
- @@recipes[recipe_name] = block
29
- end
30
-
31
- def to_atom(obj = nil, options = {}, &block)
32
- # just instantiate the string with the atom factory
33
- return Restfulie::Common::Representation::Atom::Factory.create(obj) if obj.kind_of?(String)
34
-
35
- if block_given?
36
- recipe = block
37
- elsif options[:recipe]
38
- recipe = @@recipes[options[:recipe]]
39
- else
40
- return obj if obj.respond_to?(:atom_type) && (obj.atom_type == "feed" || obj.atom_type == "entry")
41
- raise Restfulie::Common::Error::ConverterError.new("Recipe required")
42
- end
43
-
44
- # execute with the builder if a recipe is set (even if the obj is an atom)
45
- options[:atom_type] ||= obj.respond_to?(:each) ? :feed : :entry
46
- raise Restfulie::Common::Error::ConverterError.new("Undefined atom type #{options[:atom_type]}") unless [:entry,:feed].include?(options[:atom_type])
47
-
48
- # Create representation and proxy
49
- builder = Builder.new(options[:atom_type], obj)
50
-
51
- # Check recipe arity size before calling it
52
- recipe.call(*[builder, obj, options][0,recipe.arity])
53
-
54
- builder.representation
55
- end
56
-
57
- alias_method :unmarshal, :to_atom
58
-
59
- def to_hash(obj)
60
- return obj if obj.kind_of?(Hash)
61
-
62
- xml = nil
63
-
64
- if obj.kind_of?(::String)
65
- xml = obj
66
- elsif obj.respond_to?(:to_xml)
67
- xml = obj.to_xml
68
- end
69
-
70
- Hash.from_xml(xml).with_indifferent_access unless xml.nil?
71
- end
72
-
73
-
74
- def to_s(obj)
75
- return obj if obj.kind_of?(String)
76
- if obj.respond_to?(:to_xml)
77
- obj.to_xml.to_s
78
- else
79
- obj.to_s
80
- end
81
- end
82
-
83
- def marshal(obj, options = nil)
84
- to_atom(obj, options).to_xml
85
- end
86
- end
87
- end
88
- end
89
- end
90
- end
91
- end