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,4 +0,0 @@
1
- module Restfulie::Client::HTTP#:nodoc:
2
- # inject new behavior in Atom instances to enable easily access to link relationships.
3
- ::Restfulie::Common::Representation::Atom::Link.instance_eval { include LinkRequestBuilder }
4
- end
@@ -1,6 +0,0 @@
1
- %w(
2
- http
3
- ).each do |file|
4
- require "restfulie/client/http/core_ext/#{file}"
5
- end
6
-
@@ -1,19 +0,0 @@
1
- class Net::HTTP::Patch < Net::HTTP::Get
2
- METHOD = "PATCH"
3
- end
4
-
5
- # Definition of a patch method in the same way that post works
6
- class Net::HTTP
7
- def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
8
- res = nil
9
- request(Patch.new(path, initheader), data) {|r|
10
- r.read_body dest, &block
11
- res = r
12
- }
13
- unless @newimpl
14
- res.value
15
- return res, res.body
16
- end
17
- res
18
- end
19
- end
@@ -1,145 +0,0 @@
1
- #Request builder for marshalled data that unmarshalls content after receiving it.
2
- module Restfulie::Client::HTTP
3
-
4
- module ResponseHolder
5
- attr_accessor :response
6
- end
7
-
8
- module RequestMarshaller
9
- include ::Restfulie::Client::HTTP::RequestHistory
10
-
11
- @@representations = {
12
- 'application/atom+xml' => ::Restfulie::Common::Converter::Atom
13
- }
14
- def self.register_representation(media_type,representation)
15
- @@representations[media_type] = representation
16
- end
17
-
18
- RequestMarshaller.register_representation('application/xml', ::Restfulie::Common::Converter::Xml)
19
- RequestMarshaller.register_representation('text/xml', ::Restfulie::Common::Converter::Xml)
20
- RequestMarshaller.register_representation('application/json', ::Restfulie::Common::Representation::Json)
21
-
22
- def self.content_type_for(media_type)
23
- return nil unless media_type
24
- content_type = media_type.split(';')[0] # [/(.*?);/, 1]
25
- @@representations[content_type]
26
- end
27
-
28
- def accepts(media_types)
29
- @default_representation = @@representations[media_types]
30
- super
31
- end
32
-
33
- def raw
34
- @raw = true
35
- self
36
- end
37
-
38
- def post(payload, options = { :recipe => nil })
39
- request(:post, path, payload, options.merge(headers))
40
- end
41
-
42
- def post!(payload, options = { :recipe => nil })
43
- request!(:post, path, payload, options.merge(headers))
44
- end
45
-
46
- #Executes super if its a raw request, returning the content itself.
47
- #otherwise tries to parse the content with a mediatype handler or returns the response itself.
48
- def request!(method, path, *args)
49
- if has_payload?(method, path, *args)
50
- recipe = get_recipe(*args)
51
-
52
- payload = get_payload(method, path, *args)
53
- rel = self.respond_to?(:rel) ? self.rel : ""
54
- type = headers['Content-Type']
55
- raise Restfulie::Common::Error::RestfulieError, "Missing content type related to the data to be submitted" unless type
56
- marshaller = RequestMarshaller.content_type_for(type)
57
- payload = marshaller.marshal(payload, { :rel => rel, :recipe => recipe })
58
- args = set_marshalled_payload(method, path, payload, *args)
59
- args = add_representation_headers(method, path, marshaller, *args)
60
- end
61
-
62
- if @acceptable_mediatypes
63
- unmarshaller = RequestMarshaller.content_type_for(@acceptable_mediatypes)
64
- args = add_representation_headers(method, path, unmarshaller, *args)
65
- end
66
-
67
- response = super(method, path, *args)
68
- parse_response(response)
69
- end
70
-
71
- private
72
-
73
- # parses the http response.
74
- # first checks if its a 201, redirecting to the resource location.
75
- # otherwise check if its a raw request, returning the content itself.
76
- # finally, tries to parse the content with a mediatype handler or returns the response itself.
77
- def parse_response(response)
78
- if response.code == 201
79
- request = Restfulie.at(response.headers['location'])
80
- request.accepts(@acceptable_mediatypes) if @acceptable_mediatypes
81
- request.get!
82
- elsif @raw
83
- response
84
- elsif !response.body.empty?
85
- representation = RequestMarshaller.content_type_for(response.headers['content-type']) || Restfulie::Common::Representation::Generic.new
86
- unmarshalled = representation.unmarshal(response.body)
87
- unmarshalled.extend(ResponseHolder)
88
- unmarshalled.response = response
89
- unmarshalled
90
- else
91
- response.extend(ResponseHolder)
92
- response.response = response
93
- response
94
- end
95
- end
96
-
97
- def get_recipe(*args)
98
- headers_and_recipe = args.extract_options!
99
- recipe = headers_and_recipe.delete(:recipe)
100
- args << headers_and_recipe
101
- recipe
102
- end
103
-
104
- def has_payload?(method, path, *args)
105
- [:put,:post,:patch].include?(method)
106
- end
107
-
108
- def get_payload(method, path, *args)
109
- args.extract_options! #remove header
110
- args.shift #payload
111
- end
112
-
113
- def set_marshalled_payload(method, path, payload, *args)
114
- headers = args.extract_options!
115
- args.shift #old payload
116
- args << payload << headers
117
- args
118
- end
119
-
120
- def add_representation_headers(method, path, representation, *args)
121
- headers = args.extract_options!
122
- headers = headers.merge(representation.headers[method] || {})
123
- args << headers
124
- args
125
- end
126
-
127
- end
128
-
129
- # NOTE: When including this module remember to override the type method to return
130
- # a valid content type as a string.
131
- module LinkRequestBuilder
132
- include RequestMarshaller
133
- def path#:nodoc:
134
- at(href)
135
- as(type) if type
136
- super
137
- end
138
- end
139
-
140
- #=This class includes RequestBuilder module.
141
- class RequestMarshallerExecutor < RequestHistoryExecutor
142
- include RequestMarshaller
143
- end
144
-
145
- end
@@ -1,7 +0,0 @@
1
- module Restfulie::Client
2
- end
3
- module Restfulie::Client::HTTP#:nodoc:
4
- ::Hash.instance_eval {
5
- include Restfulie::Client::HTTP::LinkRequestBuilder
6
- }
7
- end
@@ -1,48 +0,0 @@
1
- class Proc
2
- attr_accessor :helpers
3
- alias_method :old_call, :call
4
-
5
- def call(*args)
6
- @helpers.nil? ? old_call(*args): call_include_helpers(@helpers, *args)
7
- end
8
-
9
- def call_include_helpers(helpers, *args)
10
- helpers = [helpers] unless helpers.kind_of?(Array)
11
- helpers = helpers.map { |helper| Object.new.send(:extend, helper) }
12
- block_caller = eval("self", self.binding)
13
-
14
- m = extensible_module(block_caller)
15
- m.send(:define_method, :method_missing) do |symbol, *args, &block|
16
- mod = helpers.find { |h| h.respond_to?(symbol) }
17
- mod.nil? ? super : mod.send(symbol, *args, &block)
18
- end
19
-
20
- block_caller.extend(m)
21
- result = old_call(*args)
22
- m.send(:remove_method, :method_missing)
23
-
24
- result
25
- end
26
-
27
- private
28
-
29
- # Search for extending the module in ancestors
30
- def extensible_module(object)
31
- ancestors = object.instance_eval { (class << self; self; end) }.ancestors
32
-
33
- extend_mod = ancestors.find { |ancestor|
34
- !ancestor.instance_methods.include?("method_missing") && ancestor.instance_eval { (class << self; self; end) }.ancestors.include?(ProcIncludedHelpers)
35
- }
36
-
37
- if extend_mod.nil?
38
- extend_mod = Module.new
39
- extend_mod.extend(ProcIncludedHelpers)
40
- end
41
-
42
- extend_mod
43
- end
44
-
45
- module ProcIncludedHelpers; end
46
-
47
- end
48
-
@@ -1,15 +0,0 @@
1
- module Restfulie::Common::Error
2
- class RestfulieError < StandardError; end
3
- class MarshallingError < RestfulieError; end
4
- class UndefinedMarshallingError < MarshallingError; end
5
-
6
- # Atom marshallinh error
7
- class AtomMarshallingError < MarshallingError; end
8
- class NameSpaceError < AtomMarshallingError; end
9
-
10
- # Converter
11
- class ConverterError < RestfulieError; end
12
-
13
- # builder
14
- class BuilderError < RestfulieError; end
15
- end
@@ -1,12 +0,0 @@
1
- module Restfulie::Server::ActionController::Routing#:nodoc:
2
- end
3
-
4
- require 'restfulie/server/action_controller/routing/restful_route'
5
-
6
- ActionController::Routing::RouteSet.send :include,
7
- Restfulie::Server::ActionController::Routing::Route
8
-
9
- ActionController::Routing::Route.send :include,
10
- Restfulie::Server::ActionController::Routing::Route
11
-
12
-
@@ -1,14 +0,0 @@
1
- module Restfulie::Server::ActionController::Routing::Route
2
-
3
- def self.included(base)
4
- #base.alias_method_chain :extract_request_environment, :host
5
- end
6
-
7
- #def extract_request_environment_with_host(request)
8
- #env = extract_request_environment_without_host(request)
9
- #env.merge :host => request.host,
10
- #:domain => request.domain, :subdomain => request.subdomains.first
11
- #end
12
-
13
- end
14
-