restfulie 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/README.textile +83 -7
  2. data/Rakefile +98 -13
  3. data/lib/restfulie/client/base.rb +48 -53
  4. data/lib/restfulie/client/configuration.rb +69 -0
  5. data/lib/restfulie/client/http/adapter.rb +487 -0
  6. data/lib/restfulie/client/http/atom_ext.rb +87 -0
  7. data/lib/restfulie/client/http/cache.rb +28 -0
  8. data/lib/restfulie/client/http/error.rb +80 -0
  9. data/lib/restfulie/client/http/marshal.rb +147 -0
  10. data/lib/restfulie/client/http.rb +13 -0
  11. data/lib/restfulie/client.rb +8 -56
  12. data/lib/restfulie/common/builder/builder_base.rb +58 -0
  13. data/lib/restfulie/common/builder/helpers.rb +22 -0
  14. data/lib/restfulie/common/builder/marshalling/atom.rb +197 -0
  15. data/lib/restfulie/common/builder/marshalling/base.rb +12 -0
  16. data/lib/restfulie/common/builder/marshalling/json.rb +2 -0
  17. data/lib/restfulie/common/builder/marshalling.rb +16 -0
  18. data/lib/restfulie/common/builder/rules/collection_rule.rb +10 -0
  19. data/lib/restfulie/common/builder/rules/link.rb +20 -0
  20. data/lib/restfulie/common/builder/rules/links.rb +9 -0
  21. data/lib/restfulie/common/builder/rules/member_rule.rb +8 -0
  22. data/lib/restfulie/common/builder/rules/namespace.rb +25 -0
  23. data/lib/restfulie/common/builder/rules/rules_base.rb +76 -0
  24. data/lib/restfulie/common/builder.rb +16 -0
  25. data/lib/restfulie/common/errors.rb +9 -0
  26. data/lib/restfulie/{logger.rb → common/logger.rb} +3 -5
  27. data/lib/restfulie/common/representation/atom.rb +48 -0
  28. data/lib/restfulie/common/representation/generic.rb +33 -0
  29. data/lib/restfulie/common/representation/xml.rb +24 -0
  30. data/lib/restfulie/common/representation.rb +10 -0
  31. data/lib/restfulie/common.rb +23 -0
  32. data/lib/restfulie/server/action_controller/base.rb +31 -0
  33. data/lib/restfulie/server/action_controller/params_parser.rb +62 -0
  34. data/lib/restfulie/server/action_controller/restful_responder.rb +39 -0
  35. data/lib/restfulie/server/action_controller/routing/restful_route.rb +14 -0
  36. data/lib/restfulie/server/action_controller/routing.rb +12 -0
  37. data/lib/restfulie/server/action_controller.rb +15 -0
  38. data/lib/restfulie/server/action_view/helpers.rb +45 -0
  39. data/lib/restfulie/server/action_view/template_handlers/tokamak.rb +15 -0
  40. data/lib/restfulie/server/action_view/template_handlers.rb +13 -0
  41. data/lib/restfulie/server/action_view.rb +8 -0
  42. data/lib/restfulie/server/configuration.rb +21 -0
  43. data/lib/restfulie/server/core_ext/array.rb +45 -0
  44. data/lib/restfulie/server/core_ext.rb +1 -0
  45. data/lib/restfulie/server/restfulie_controller.rb +1 -17
  46. data/lib/restfulie/server.rb +15 -0
  47. data/lib/restfulie.rb +4 -72
  48. data/lib/vendor/atom/configuration.rb +24 -0
  49. data/lib/vendor/atom/pub.rb +250 -0
  50. data/lib/vendor/atom/xml/parser.rb +373 -0
  51. data/lib/vendor/atom.rb +771 -0
  52. metadata +94 -33
  53. data/lib/restfulie/client/atom_media_type.rb +0 -75
  54. data/lib/restfulie/client/cache.rb +0 -103
  55. data/lib/restfulie/client/entry_point.rb +0 -94
  56. data/lib/restfulie/client/extensions/http.rb +0 -116
  57. data/lib/restfulie/client/helper.rb +0 -28
  58. data/lib/restfulie/client/instance.rb +0 -158
  59. data/lib/restfulie/client/request_execution.rb +0 -321
  60. data/lib/restfulie/client/state.rb +0 -36
  61. data/lib/restfulie/media_type.rb +0 -143
  62. data/lib/restfulie/media_type_control.rb +0 -115
  63. data/lib/restfulie/media_type_defaults.rb +0 -51
  64. data/lib/restfulie/server/atom_media_type.rb +0 -115
  65. data/lib/restfulie/server/base.rb +0 -91
  66. data/lib/restfulie/server/controller.rb +0 -122
  67. data/lib/restfulie/server/instance.rb +0 -102
  68. data/lib/restfulie/server/marshalling.rb +0 -47
  69. data/lib/restfulie/server/opensearch/description.rb +0 -54
  70. data/lib/restfulie/server/opensearch.rb +0 -18
  71. data/lib/restfulie/server/transition.rb +0 -93
  72. data/lib/restfulie/unmarshalling.rb +0 -131
  73. data/lib/vendor/jeokkarak/hashi.rb +0 -65
  74. data/lib/vendor/jeokkarak/jeokkarak.rb +0 -81
@@ -1,18 +0,0 @@
1
- #
2
- # Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
3
- # All rights reserved.
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
-
18
- require 'restfulie/server/opensearch/description'
@@ -1,93 +0,0 @@
1
- #
2
- # Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
3
- # All rights reserved.
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
-
18
- module Restfulie
19
-
20
- module Server
21
-
22
- # represents a transition on the server side
23
- class Transition
24
- attr_reader :body, :name
25
- attr_writer :options
26
- attr_accessor :result
27
-
28
- def initialize(*args)
29
- args[0].kind_of?(Array) ? from_array(*args) : from(*args)
30
- end
31
-
32
- private
33
- def from_array(array)
34
- @name = array[0]
35
- @options = array.length>0 ? array[1] : nil
36
- @result = array.length>1 ? array[2] : nil
37
- @body = nil
38
- end
39
-
40
- def from(name, options = {}, result = nil, body = nil)
41
- @name = name
42
- @options = options
43
- @result = result
44
- @body = body
45
- end
46
-
47
- public
48
-
49
- def action
50
- @options || {}
51
- end
52
-
53
- # executes this transition in a resource
54
- def execute_at(target_object)
55
- target_object.status = @result.to_s unless @result.nil?
56
- end
57
-
58
- # return the link to this transitions's uri
59
- def link_for(model, controller)
60
- specific_action = @body ? @body.call(model) : action.dup
61
-
62
- specific_action = parse_specific_action(specific_action, model)
63
-
64
- rel = specific_action[:rel] || @name
65
- specific_action[:rel] = nil
66
-
67
- specific_action[:action] ||= @name
68
- uri = controller.url_for(specific_action)
69
-
70
- return rel, uri
71
- end
72
-
73
- private
74
-
75
- # if you use the class level DSL, you will need to add a lambda for instance level accessors:
76
- # transition :show, {:action => :show, :foo_id => lambda { |model| model.id }}
77
- # but you can replace it for a symbol and defer the model call
78
- # transition :show, {:action => :show, :foo_id => :id}
79
- def parse_specific_action(action, model)
80
- action.inject({}) do |actions, pair|
81
- if pair.last.is_a?( Symbol ) && model.attributes.include?(pair.last)
82
- actions.merge!( pair.first => model.send(pair.last) )
83
- else
84
- actions.merge!( pair.first => pair.last )
85
- end
86
- end
87
- action
88
- end
89
-
90
- end
91
- end
92
-
93
- end
@@ -1,131 +0,0 @@
1
- #
2
- # Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
3
- # All rights reserved.
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
-
18
- module Hashi
19
- class CustomHash
20
- def initialize(hash = {})
21
- @internal_hash = hash
22
- link = hash['link'] if hash.kind_of? Hash
23
- add_transitions([link]) if link.kind_of? Hash
24
- add_transitions(link) if link.kind_of? Array
25
- end
26
- end
27
- end
28
-
29
- # module Jeokkarak
30
- # module Base
31
- # alias_method :old_from_hash_parse, :from_hash_parse
32
- # def from_hash_parse(result,h,key,value)
33
- # return old_from_hash_parse(result, h, key, value) if key!='link'
34
- # link = h[key]
35
- # result.add_transitions([link]) if link.kind_of? Hash
36
- # result.add_transitions(link) if link.kind_of? Array
37
- # end
38
- # end
39
- # end
40
-
41
- module Restfulie
42
- module Unmarshalling
43
- # basic code from Matt Pulver
44
- # found at http://www.xcombinator.com/2008/08/11/activerecord-from_xml-and-from_json-part-2/
45
- # addapted to support links
46
-
47
- def from_hash( hash )
48
- h = hash ? hash.dup : {}
49
- links = nil
50
- h.each do |key, value|
51
- if key == "link"
52
- links = value.instance_of?(Array) ? h[key] : [h[key]]
53
- elsif [Array, Hash].include? value.class
54
- klazz = get_the_class(key)
55
- if value.instance_of?(Array)
56
- h[key].map! { |e| (klazz || Hashi::CustomHash).send(:from_hash, e) }
57
- elsif value.instance_of?(Hash)
58
- h[key] = klazz.array_from_hash(value)
59
- else
60
- h[key] = klazz.from_hash value
61
- end
62
- end
63
- h.delete key if ["xmlns", "link"].include?(key)
64
- end
65
-
66
- result = make_new_object h
67
- result.add_transitions(links) if !(links.nil?) && self.include?(Restfulie::Client::Instance)
68
- result
69
- end
70
-
71
- # creates an array of this type based on a hash created using rails Hash.from_xml method
72
- # because Hash.from_xml returns some no-cute hash structure when dealing with aligned elements
73
- # we had to support it in a variety of ways.
74
- # usage example:
75
- #
76
- # hash = {"player" => {"name" => "guilherme silveira"}}
77
- # player = Player.array_from_hash(hash)
78
- # puts player[0].name
79
- #
80
- # hash = {"player" => [{"name" => "guilherme silveira"}, {"name" => "andre de santi"}]}
81
- # player = Player.array_from_hash(hash)
82
- # puts player[0].name
83
- # puts player[1].name
84
- def array_from_hash(hash)
85
-
86
- return self.from_hash(hash) unless (hash.size == 1 && hash.keys.first == self.to_s.underscore)
87
-
88
- children = hash.values.first
89
-
90
- return [self.from_hash(children)] unless children.instance_of?(Array)
91
-
92
- children.map { |child| self.from_hash(child) }
93
-
94
- end
95
-
96
- def from_json(json)
97
- from_hash(safe_json_decode(json).values.first)
98
- end
99
-
100
- # The xml has a surrounding class tag (e.g. ship-to),
101
- # but the hash has no counterpart (e.g. 'ship_to' => {} )
102
- def from_xml(xml)
103
- hash = Hash.from_xml xml
104
- head = hash[self.to_s.underscore]
105
- result = self.from_hash head
106
- return nil if result.nil?
107
- result._came_from = :xml if self.include?(Restfulie::Client::Instance)
108
- result
109
- end
110
-
111
- private
112
- def make_new_object(hash = {})
113
- obj = self.new
114
- hash.keys.each { |key| obj.send("#{key}=", hash[key]) }
115
- obj
116
- end
117
-
118
- def get_the_class(name)
119
- respond_to?(:reflect_on_association) ? reflect_on_association(name.to_sym).klass.to_s.constantize : nil
120
- end
121
- end
122
- end
123
-
124
- private
125
- def safe_json_decode(json)
126
- return {} if !json
127
- begin
128
- ActiveSupport::JSON.decode json
129
- rescue ; {} end
130
- end
131
- # end of code based on Matt Pulver's
@@ -1,65 +0,0 @@
1
- module Hashi
2
- class UndefinedMethod < Exception
3
- attr_reader :msg
4
- def initialize(msg)
5
- @msg = msg
6
- end
7
- def to_s
8
- @msg
9
- end
10
- end
11
-
12
- class CustomHash
13
-
14
- attr_reader :internal_hash
15
-
16
- def initialize(hash = {})
17
- @internal_hash = hash
18
- end
19
-
20
- def method_missing(name, *args, &block)
21
- name = name.to_s if name.kind_of? Symbol
22
- if name[-1,1] == "?"
23
- parse(name, @internal_hash[name.chop])
24
- elsif name[-1,1] == "="
25
- @internal_hash[name.chop] = args[0]
26
- elsif @internal_hash.kind_of?(Array) && name == "each"
27
- @internal_hash.each do |k| block.call(transform(k)) end
28
- elsif name.respond_to? name
29
- @internal_hash.send(name, *args, &block)
30
- else
31
- return nil if @internal_hash.has_key?(name) && @internal_hash[name].nil?
32
- parse(name, transform(@internal_hash[name]))
33
- end
34
- end
35
-
36
- def respond_to?(symbol)
37
- super(symbol) || @internal_hash.key?(symbol.to_s)
38
- end
39
-
40
- def [](x)
41
- transform(@internal_hash[x])
42
- end
43
-
44
- private
45
- def transform(value)
46
- return CustomHash.new(value) if value.kind_of?(Hash) || value.kind_of?(Array)
47
- value
48
- end
49
-
50
- def parse(name, value)
51
- raise Hashi::UndefinedMethod.new("undefined method '#{name}'") if value.nil?
52
- value
53
- end
54
-
55
- end
56
-
57
- def self.from_hash(hash)
58
- CustomHash.new(hash)
59
- end
60
-
61
- def self.to_object(hash)
62
- CustomHash.new(hash)
63
- end
64
-
65
- end
@@ -1,81 +0,0 @@
1
- #
2
- # Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
3
- # All rights reserved.
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require 'vendor/jeokkarak/hashi'
18
-
19
- module Jeokkarak
20
- module Base
21
-
22
- # defines that this type has a child element
23
- def has_child(type, options={})
24
- resource_children[options[:as]] = type
25
- end
26
-
27
- # checks what is the type element for this type (supports rails ActiveRecord, has_child and Hashi)
28
- def child_type_for(name)
29
- return reflect_on_association(name.to_sym ).klass if respond_to? :reflect_on_association
30
- resource_children[name] || Hashi
31
- end
32
-
33
- # returns the registered children list for this resource
34
- def resource_children
35
- @children ||= {}
36
- @children
37
- end
38
-
39
- # creates an instance of this type based on this hash
40
- def from_hash(h)
41
- h = {} if h.nil? # nasty required check
42
- h = h.dup
43
- result = self.new
44
- result._internal_hash = h
45
- h.each do |key,value|
46
- from_hash_parse result, h, key, value
47
- end
48
- def result.method_missing(name, *args, &block)
49
- Hashi.to_object(@_internal_hash).send(name, args[0], block)
50
- end
51
- result
52
- end
53
-
54
- # extension point to parse a value
55
- def from_hash_parse(result,h,key,value)
56
- case value.class.to_s
57
- when 'Array'
58
- h[key].map! { |e| child_type_for(key).from_hash e }
59
- when /\AHash(WithIndifferentAccess)?\Z/
60
- h[key] = child_type_for(key ).from_hash value
61
- end
62
- name = "#{key}="
63
- result.send(name, value) if result.respond_to?(name)
64
- end
65
- end
66
- end
67
-
68
- module Jeokkarak
69
- module Config
70
-
71
- # entry point to define a jeokkarak type
72
- def acts_as_jeokkarak
73
- self.module_eval do
74
- attr_accessor :_internal_hash
75
- end
76
- self.extend Jeokkarak::Base
77
- end
78
- end
79
- end
80
-
81
- Object.extend Jeokkarak::Config