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,14 +0,0 @@
1
- # Atom links now can be followed
2
- module Restfulie
3
- module Common
4
- module Representation
5
- module Atom
6
- class Link
7
- def follow
8
- Restfulie.at(href).as(type)
9
- end
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,22 +0,0 @@
1
- module Net
2
- class HTTP
3
- # Definition of a patch method in the same way that post works
4
- def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
5
- res = nil
6
- request(Patch.new(path, initheader), data) {|r|
7
- r.read_body dest, &block
8
- res = r
9
- }
10
- unless @newimpl
11
- res.value
12
- return res, res.body
13
- end
14
- res
15
- end
16
-
17
- class Patch < Get
18
- METHOD = "PATCH"
19
- end
20
- end
21
- end
22
-
@@ -1,16 +0,0 @@
1
- # inject new behavior in Atom instances to enable easily access to link relationships.
2
- module Restfulie
3
- module Common
4
- module Representation
5
- class Json
6
- class Link
7
- def follow
8
- r = Restfulie.at(href)
9
- r = r.as(type) if type
10
- r
11
- end
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,5 +0,0 @@
1
- module Restfulie::Client
2
- module Feature
3
- Dir["#{File.dirname(__FILE__)}/feature/*.rb"].each {|f| autoload File.basename(f)[0..-4].camelize.to_sym, f }
4
- end
5
- end
@@ -1,75 +0,0 @@
1
- module Restfulie::Client::Feature
2
- module Base
3
-
4
- attr_reader :default_headers, :cookies, :verb, :host
5
- attr_writer :headers
6
-
7
- #Set host
8
- def at(url)
9
- if self.host.nil?
10
- self.host= url
11
- else
12
- self.host= self.host + url
13
- end
14
- self
15
- end
16
-
17
- #Set Content-Type and Accept headers
18
- def as(content_type)
19
- headers['Content-Type'] = content_type
20
- accepts(content_type)
21
- end
22
-
23
- #Set Accept headers
24
- def accepts(content_type)
25
- headers['Accept'] = content_type
26
- self
27
- end
28
-
29
- # Merge internal header
30
- #
31
- # * <tt>headers (e.g. {'Cache-control' => 'no-cache'})</tt>
32
- #
33
- def with(headers)
34
- headers.merge!(headers)
35
- self
36
- end
37
-
38
- # Path (e.g. http://restfulie.com/posts => /posts)
39
- def path
40
- host.path + (host.query.nil? ? "" : "?#{host.query}")
41
- end
42
-
43
- def host=(host)
44
- if host.is_a?(::URI)
45
- @host = host
46
- else
47
- @host = ::URI.parse(host)
48
- end
49
- end
50
-
51
- def default_headers
52
- @default_headers ||= {}
53
- end
54
-
55
- def headers
56
- @headers ||= {}
57
- end
58
-
59
- def http_to_s(method, path, *args)
60
- result = ["#{method.to_s.upcase} #{path}"]
61
-
62
- arguments = args.dup
63
- headers = arguments.extract_options!
64
-
65
- if [:post, :put].include?(method)
66
- body = arguments.shift
67
- end
68
-
69
- result << headers.collect { |key, value| "#{key}: #{value}" }.join("\n")
70
-
71
- (result + [body ? (body.inspect + "\n") : nil]).compact.join("\n") << "\n"
72
- end
73
-
74
- end
75
- end
@@ -1,35 +0,0 @@
1
- class Restfulie::Client::Feature::BaseRequest
2
-
3
- def execute(flow, request, response, env)
4
- request!(request.verb, request.host, request.path, request, flow, env)
5
- end
6
-
7
- # Executes a request against your server and return a response instance.
8
- # * <tt>method: :get,:post,:delete,:head,:put</tt>
9
- # * <tt>path: '/posts'</tt>
10
- # * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
11
- def request!(method, host, path, request, flow, env)
12
-
13
- ::Restfulie::Common::Logger.logger.info(request.http_to_s(method, path, [request.headers])) if ::Restfulie::Common::Logger.logger
14
- begin
15
- http_request = get_connection_provider(host)
16
-
17
- if env[:body]
18
- response = http_request.send(method, path, env[:body], request.headers)
19
- else
20
- response = http_request.send(method, path, request.headers)
21
- end
22
-
23
- rescue Exception => e
24
- response = e
25
- end
26
-
27
- flow.continue(request, response, env)
28
-
29
- end
30
-
31
- def get_connection_provider(host)
32
- @connection ||= ::Net::HTTP.new(host.host, host.port)
33
- end
34
-
35
- end
@@ -1,16 +0,0 @@
1
- class Restfulie::Client::Feature::Cache
2
-
3
- def execute(flow, request, response, env)
4
- found = Restfulie::Client.cache_provider.get([request.host, request.path], request)
5
- return found if found
6
-
7
- resp = flow.continue(request, response, env)
8
- if resp.kind_of?(Exception)
9
- resp
10
- else
11
- Restfulie::Client.cache_provider.put([request.host, request.path], request, resp)
12
- resp
13
- end
14
- end
15
-
16
- end
@@ -1,12 +0,0 @@
1
- module Restfulie::Client::Feature
2
- class EnhanceResponse
3
- def execute(flow, request, response, env)
4
- resp = flow.continue(request, response, env)
5
- unless resp.kind_of? ::Restfulie::Client::HTTP::ResponseHolder
6
- resp.extend(::Restfulie::Client::HTTP::ResponseHolder)
7
- resp.results_from request, resp
8
- end
9
- resp
10
- end
11
- end
12
- end
@@ -1,41 +0,0 @@
1
- # ==== FollowLink follow new location of a document usually with response codes 201,301,302,303 and 307. You can also configure other codes.
2
- #
3
- # ==== Example:
4
- # @executor = ::Restfulie::Client::HTTP::FollowLinkExecutor.new("http://restfulie.com") #this class includes FollowLink module.
5
- # @executor.at('/custom/songs').accepts('application/atom+xml').follow(201).post!("custom").code
6
- class Restfulie::Client::Feature::FollowRequest
7
-
8
- def follow(code = nil)
9
- unless code.nil? or follow_codes.include?(code)
10
- follow_codes << code
11
- end
12
- self
13
- end
14
-
15
- def execute(flow, request, response, env)
16
- resp = flow.continue(request, response, env)
17
- if !resp.respond_to?(:code)
18
- return resp
19
- end
20
- code = resp.code.to_i
21
- if follow_codes.include?(code)
22
- if code==201 && !resp.body.empty?
23
- resp
24
- else
25
- location = resp.response.headers['location'] || resp.response.headers['Location']
26
- raise Error::AutoFollowWithoutLocationError.new(request, resp) unless location
27
- # use the first location available
28
- location = location[0]
29
- Restfulie.at(location).accepts(request.headers['Accept']).get
30
- end
31
- else
32
- resp
33
- end
34
- end
35
-
36
- protected
37
-
38
- def follow_codes
39
- @follow_codes ||= [201,301,302,303,307]
40
- end
41
- end
@@ -1,26 +0,0 @@
1
- module Restfulie::Client::Feature::History
2
-
3
- def snapshots
4
- @snapshots ||= []
5
- end
6
-
7
- def max_to_remind
8
- 10
9
- end
10
-
11
- def history(number)
12
- snapshots[snapshots.size + number] || raise("Undefined snapshot for #{number}, only containing #{@snapshots.size} snapshots.")
13
- end
14
-
15
- def make_snapshot(request)
16
- snapshots.shift if snapshot_full?
17
- snapshots << request
18
- end
19
-
20
- private
21
-
22
- def snapshot_full?
23
- snapshots.size >= max_to_remind
24
- end
25
-
26
- end
@@ -1,19 +0,0 @@
1
- # ==== RequestHistory
2
- # Uses RequestBuilder and remind previous requests
3
- #
4
- # ==== Example:
5
- #
6
- # @executor = ::Restfulie::Client::HTTP::RequestHistoryExecutor.new("http://restfulie.com") #this class includes RequestHistory module.
7
- # @executor.at('/posts').as('application/xml').accepts('application/atom+xml').with('Accept-Language' => 'en').get.code #=> 200 #first request
8
- # @executor.at('/blogs').as('application/xml').accepts('application/atom+xml').with('Accept-Language' => 'en').get.code #=> 200 #second request
9
- # @executor.request_history!(0) #doing first request
10
- #
11
- class Restfulie::Client::Feature::HistoryRequest
12
-
13
- def execute(flow, request, response, env)
14
- resp = flow.continue(request, response, env)
15
- request.make_snapshot(request)
16
- resp
17
- end
18
-
19
- end
@@ -1,21 +0,0 @@
1
- module Restfulie::Client::Feature
2
- module OpenSearch
3
- autoload :PatternMatcher, 'restfulie/client/feature/open_search/pattern_matcher'
4
- end
5
- end
6
-
7
- module Restfulie::Client::Feature::OpenSearch
8
-
9
- def search(params)
10
- at ("?" + PatternMatcher.new.match(params, params_pattern))
11
- get
12
- end
13
-
14
- attr_reader :params_pattern
15
-
16
- def with_pattern(params_pattern)
17
- @params_pattern = params_pattern
18
- self
19
- end
20
-
21
- end
@@ -1,25 +0,0 @@
1
- module Restfulie::Client::Feature::OpenSearch
2
-
3
- class PatternMatcher
4
-
5
- def match(params, pattern)
6
- params = params.collect do |key, value|
7
- [key, value]
8
- end
9
- pattern = params.inject(pattern) do |pattern, p|
10
- what = "{#{p[0]}}"
11
- if pattern[what]
12
- pattern[what]= "#{p[1]}"
13
- end
14
- what = "{#{p[0]}?}"
15
- if pattern[what]
16
- pattern[what]= "#{p[1]}"
17
- end
18
- pattern
19
- end
20
- pattern.gsub(/\{[^\?]*\?\}/,"")
21
- end
22
-
23
- end
24
-
25
- end
@@ -1,32 +0,0 @@
1
- class Restfulie::Client::Feature::SerializeBody
2
-
3
- def execute(flow, request, response, env)
4
-
5
- if should_have_payload?(request.verb)
6
-
7
- payload = env[:body]
8
- if payload && !(payload.kind_of?(String) && payload.empty?)
9
- type = request.headers['Content-Type']
10
- raise Restfulie::Common::Error::RestfulieError, "Missing content type related to the data to be submitted" unless type
11
-
12
- marshaller = Restfulie::Common::Converter.content_type_for(type)
13
- raise Restfulie::Common::Error::RestfulieError, "Missing content type for #{type} related to the data to be submitted" unless marshaller
14
-
15
- rel = request.respond_to?(:rel) ? request.rel : ""
16
- env[:body] = marshaller.marshal(payload, { :rel => rel, :recipe => env[:recipe] })
17
- end
18
-
19
- end
20
-
21
- flow.continue(request, response, env)
22
- end
23
-
24
- protected
25
-
26
- PAYLOAD_METHODS = {:put=>true,:post=>true,:patch=>true}
27
-
28
- def should_have_payload?(method)
29
- PAYLOAD_METHODS[method]
30
- end
31
-
32
- end
@@ -1,22 +0,0 @@
1
- module Restfulie::Client::Feature
2
-
3
- class SetupHeader
4
-
5
- def execute(flow, request, response, env)
6
- headers = request.default_headers.dup.merge(request.headers)
7
- host = request.host
8
- if host.user || host.password
9
- headers["Authorization"] = "Basic " + ["#{host.user}:#{host.password}"].pack("m").delete("\r\n")
10
- end
11
- headers.delete :recipe
12
- headers['cookie'] = request.cookies if request.cookies
13
-
14
- # gs: this should not be overriden, do it in some other way
15
- request.headers = headers
16
-
17
- flow.continue(request, response, env)
18
- end
19
-
20
- end
21
-
22
- end
@@ -1,41 +0,0 @@
1
- class Restfulie::Client::Feature::ThrowError
2
- def execute(flow, request, result_so_far, env)
3
- result = flow.continue(request, result_so_far, env)
4
- if result.kind_of? Exception
5
- Restfulie::Common::Logger.logger.error(result)
6
- raise Restfulie::Client::HTTP::Error::ServerNotAvailableError.new(request, Restfulie::Client::HTTP::Response.new(request.verb, request.path, 503, nil, {}), result )
7
- end
8
- case result.response.code.to_i
9
- when 100..299
10
- result
11
- when 300..399
12
- raise Restfulie::Client::HTTP::Error::Redirection.new(request, result)
13
- when 400
14
- raise Restfulie::Client::HTTP::Error::BadRequest.new(request, result)
15
- when 401
16
- raise Restfulie::Client::HTTP::Error::Unauthorized.new(request, result)
17
- when 403
18
- raise Restfulie::Client::HTTP::Error::Forbidden.new(request, result)
19
- when 404
20
- raise Restfulie::Client::HTTP::Error::NotFound.new(request, result)
21
- when 405
22
- raise Restfulie::Client::HTTP::Error::MethodNotAllowed.new(request, result)
23
- when 407
24
- raise Restfulie::Client::HTTP::Error::ProxyAuthenticationRequired.new(request, result)
25
- when 409
26
- raise Restfulie::Client::HTTP::Error::Conflict.new(request, result)
27
- when 410
28
- raise Restfulie::Client::HTTP::Error::Gone.new(request, result)
29
- when 412
30
- raise Restfulie::Client::HTTP::Error::PreconditionFailed.new(request, result)
31
- when 402, 406, 408, 411, 413..499
32
- raise Restfulie::Client::HTTP::Error::ClientError.new(request, result)
33
- when 501
34
- raise Restfulie::Client::HTTP::Error::NotImplemented.new(request, result)
35
- when 500, 502..599
36
- raise Restfulie::Client::HTTP::Error::ServerError.new(request, result)
37
- else
38
- raise Restfulie::Client::HTTP::Error::UnknownError.new(request, result)
39
- end
40
- end
41
- end