restfulie 0.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/Gemfile +28 -0
  2. data/Gemfile.lock +128 -0
  3. data/LICENSE +17 -0
  4. data/README.textile +138 -0
  5. data/Rakefile +146 -0
  6. data/lib/restfulie/client/base.rb +36 -0
  7. data/lib/restfulie/client/cache/basic.rb +76 -0
  8. data/lib/restfulie/client/cache/fake.rb +15 -0
  9. data/lib/restfulie/client/cache/http_ext.rb +123 -0
  10. data/lib/restfulie/client/cache/restrictions.rb +13 -0
  11. data/lib/restfulie/client/cache.rb +11 -0
  12. data/lib/restfulie/client/configuration.rb +67 -0
  13. data/lib/restfulie/client/dsl.rb +66 -0
  14. data/lib/restfulie/client/entry_point.rb +61 -0
  15. data/lib/restfulie/client/ext/atom_ext.rb +14 -0
  16. data/lib/restfulie/client/ext/http_ext.rb +22 -0
  17. data/lib/restfulie/client/ext/json_ext.rb +16 -0
  18. data/lib/restfulie/client/feature/base.rb +75 -0
  19. data/lib/restfulie/client/feature/base_request.rb +35 -0
  20. data/lib/restfulie/client/feature/cache.rb +16 -0
  21. data/lib/restfulie/client/feature/enhance_response.rb +12 -0
  22. data/lib/restfulie/client/feature/follow_request.rb +41 -0
  23. data/lib/restfulie/client/feature/history.rb +26 -0
  24. data/lib/restfulie/client/feature/history_request.rb +19 -0
  25. data/lib/restfulie/client/feature/open_search/pattern_matcher.rb +25 -0
  26. data/lib/restfulie/client/feature/open_search.rb +21 -0
  27. data/lib/restfulie/client/feature/serialize_body.rb +32 -0
  28. data/lib/restfulie/client/feature/setup_header.rb +22 -0
  29. data/lib/restfulie/client/feature/throw_error.rb +41 -0
  30. data/lib/restfulie/client/feature/verb.rb +119 -0
  31. data/lib/restfulie/client/feature.rb +5 -0
  32. data/lib/restfulie/client/http/cache.rb +28 -0
  33. data/lib/restfulie/client/http/error.rb +77 -0
  34. data/lib/restfulie/client/http/response_holder.rb +29 -0
  35. data/lib/restfulie/client/http.rb +7 -0
  36. data/lib/restfulie/client/master_delegator.rb +31 -0
  37. data/lib/restfulie/client/mikyung/concatenator.rb +18 -0
  38. data/lib/restfulie/client/mikyung/core.rb +70 -0
  39. data/lib/restfulie/client/mikyung/languages/german.rb +24 -0
  40. data/lib/restfulie/client/mikyung/languages/portuguese.rb +23 -0
  41. data/lib/restfulie/client/mikyung/languages.rb +11 -0
  42. data/lib/restfulie/client/mikyung/rest_process_model.rb +191 -0
  43. data/lib/restfulie/client/mikyung/steady_state_walker.rb +38 -0
  44. data/lib/restfulie/client/mikyung/then_condition.rb +39 -0
  45. data/lib/restfulie/client/mikyung/when_condition.rb +57 -0
  46. data/lib/restfulie/client/mikyung.rb +15 -0
  47. data/lib/restfulie/client.rb +26 -0
  48. data/lib/restfulie/common/converter/atom/base.rb +91 -0
  49. data/lib/restfulie/common/converter/atom/builder.rb +111 -0
  50. data/lib/restfulie/common/converter/atom/helpers.rb +17 -0
  51. data/lib/restfulie/common/converter/atom.rb +12 -0
  52. data/lib/restfulie/common/converter/json/base.rb +87 -0
  53. data/lib/restfulie/common/converter/json/builder.rb +102 -0
  54. data/lib/restfulie/common/converter/json/helpers.rb +17 -0
  55. data/lib/restfulie/common/converter/json.rb +12 -0
  56. data/lib/restfulie/common/converter/open_search/descriptor.rb +32 -0
  57. data/lib/restfulie/common/converter/open_search.rb +16 -0
  58. data/lib/restfulie/common/converter/values.rb +33 -0
  59. data/lib/restfulie/common/converter/xml/base.rb +63 -0
  60. data/lib/restfulie/common/converter/xml/builder.rb +113 -0
  61. data/lib/restfulie/common/converter/xml/helpers.rb +17 -0
  62. data/lib/restfulie/common/converter/xml/link.rb +30 -0
  63. data/lib/restfulie/common/converter/xml/links.rb +21 -0
  64. data/lib/restfulie/common/converter/xml.rb +14 -0
  65. data/lib/restfulie/common/converter.rb +43 -0
  66. data/lib/restfulie/common/core_ext/hash.rb +18 -0
  67. data/lib/restfulie/common/core_ext.rb +1 -0
  68. data/lib/restfulie/common/error.rb +19 -0
  69. data/lib/restfulie/common/links.rb +9 -0
  70. data/lib/restfulie/common/logger.rb +19 -0
  71. data/lib/restfulie/common/representation/atom/atom.rng +597 -0
  72. data/lib/restfulie/common/representation/atom/base.rb +142 -0
  73. data/lib/restfulie/common/representation/atom/category.rb +41 -0
  74. data/lib/restfulie/common/representation/atom/entry.rb +59 -0
  75. data/lib/restfulie/common/representation/atom/factory.rb +43 -0
  76. data/lib/restfulie/common/representation/atom/feed.rb +110 -0
  77. data/lib/restfulie/common/representation/atom/link.rb +68 -0
  78. data/lib/restfulie/common/representation/atom/person.rb +48 -0
  79. data/lib/restfulie/common/representation/atom/source.rb +59 -0
  80. data/lib/restfulie/common/representation/atom/tag_collection.rb +38 -0
  81. data/lib/restfulie/common/representation/atom/xml.rb +90 -0
  82. data/lib/restfulie/common/representation/atom.rb +20 -0
  83. data/lib/restfulie/common/representation/generic.rb +22 -0
  84. data/lib/restfulie/common/representation/json/base.rb +27 -0
  85. data/lib/restfulie/common/representation/json/keys_as_methods.rb +74 -0
  86. data/lib/restfulie/common/representation/json/link.rb +29 -0
  87. data/lib/restfulie/common/representation/json/link_collection.rb +23 -0
  88. data/lib/restfulie/common/representation/json.rb +13 -0
  89. data/lib/restfulie/common/representation/links.rb +11 -0
  90. data/lib/restfulie/common/representation.rb +3 -0
  91. data/lib/restfulie/common.rb +18 -0
  92. data/lib/restfulie/server/action_controller/base.rb +48 -0
  93. data/lib/restfulie/server/action_controller/params_parser.rb +100 -0
  94. data/lib/restfulie/server/action_controller/patch.rb +6 -0
  95. data/lib/restfulie/server/action_controller/restful_responder.rb +12 -0
  96. data/lib/restfulie/server/action_controller/trait/cacheable.rb +81 -0
  97. data/lib/restfulie/server/action_controller/trait/created.rb +17 -0
  98. data/lib/restfulie/server/action_controller/trait.rb +9 -0
  99. data/lib/restfulie/server/action_controller.rb +11 -0
  100. data/lib/restfulie/server/action_view/helpers.rb +50 -0
  101. data/lib/restfulie/server/action_view/template_handlers/tokamak.rb +21 -0
  102. data/lib/restfulie/server/action_view/template_handlers.rb +30 -0
  103. data/lib/restfulie/server/action_view.rb +10 -0
  104. data/lib/restfulie/server/configuration.rb +24 -0
  105. data/lib/restfulie/server/controller.rb +74 -0
  106. data/lib/restfulie/server/core_ext/array.rb +61 -0
  107. data/lib/restfulie/server/core_ext.rb +1 -0
  108. data/lib/restfulie/server.rb +25 -0
  109. data/lib/restfulie/version.rb +14 -0
  110. data/lib/restfulie.rb +34 -0
  111. metadata +242 -0
@@ -0,0 +1,123 @@
1
+ require 'time'
2
+
3
+ # an extesion to http responses
4
+ module Restfulie::Client::HTTP::ResponseStatus
5
+
6
+ attr_accessor :previous
7
+
8
+ # determines if this response code was successful (according to http specs: 200~299)
9
+ def is_successful?
10
+ code.to_i >= 200 && code.to_i <= 299
11
+ end
12
+
13
+ # determines if this response code was successful (according to http specs: 100~199)
14
+ def is_informational?
15
+ code.to_i >= 100 && code.to_i <= 199
16
+ end
17
+
18
+ # determines if this response code was successful (according to http specs: 300~399)
19
+ def is_redirection?
20
+ code.to_i >= 300 && code.to_i <= 399
21
+ end
22
+
23
+ # determines if this response code was successful (according to http specs: 400~499)
24
+ def is_client_error?
25
+ code.to_i >= 400 && code.to_i <= 499
26
+ end
27
+
28
+ # determines if this response code was successful (according to http specs: 500~599)
29
+ def is_server_error?
30
+ code.to_i >= 500 && code.to_i <= 599
31
+ end
32
+
33
+ def etag
34
+ self['Etag']
35
+ end
36
+
37
+ def last_modified
38
+ self['Last-Modified']
39
+ end
40
+
41
+ end
42
+
43
+ module Restfulie::Client::HTTP::ResponseCacheCheck
44
+
45
+ def cache_max_age
46
+ val = header_value_from('cache-control', /^\s*max-age=(\d+)/)
47
+ if val
48
+ val.to_i
49
+ else
50
+ 0
51
+ end
52
+ end
53
+
54
+ def header_value_from(header, expression)
55
+ h = value_for(headers[header], expression)
56
+ return nil if h.nil?
57
+ h.match(expression)[1]
58
+ end
59
+
60
+ def has_expired_cache?
61
+ return true if headers['date'].nil?
62
+ max_time = Time.rfc2822(headers['date'][0]) + cache_max_age.seconds
63
+ Time.now > max_time
64
+ end
65
+
66
+ # checks if the header's max-age is available and no no-store if available.
67
+ def may_cache?
68
+ may_cache_method? && may_cache_field?(headers['cache-control'])
69
+ end
70
+
71
+ # Returns whether this cache control field allows caching
72
+ #
73
+ # may_cache_field(['max-age=2000', 'no-store']) == false
74
+ # may_cache_field('max-age=2000,no-store') == false
75
+ # may_cache_field('max-age=2000') == true
76
+ def may_cache_field?(field)
77
+ return false if field.nil?
78
+
79
+ if field.kind_of? Array
80
+ field.each do |f|
81
+ return false if !may_cache_field?(f)
82
+ end
83
+ return true
84
+ end
85
+
86
+ max_age_header = value_for(field, /^max-age=(\d+)/)
87
+ return false if max_age_header.nil?
88
+
89
+ return !value_for(field, /^no-store/)
90
+ end
91
+
92
+ # extracts the header value for an specific expression, which can be located at the start or in the middle
93
+ # of the expression
94
+ def value_for(value, expression)
95
+ value = value[0] if value.kind_of? Array
96
+ value.split(",").find { |obj| obj.strip =~ expression }
97
+ end
98
+
99
+ # extracts all header values related to the Vary header from this response, in order
100
+ # to implement Vary support from the HTTP Specification
101
+ #
102
+ # example
103
+ # if the response Vary header is 'Accept','Accept-Language', we have
104
+ # vary_headers_for({'Accept'=>'application/xml', 'Date' =>'...', 'Accept-Language'=>'de'}) == ['application/xml', 'de']
105
+ # vary_headers_for({'Date' => '...', 'Accept-Language'=>'de'}) == [nil, 'de']
106
+ def vary_headers_for(request)
107
+ return nil if headers['vary'].nil?
108
+ headers['vary'].split(',').map do |key|
109
+ request[key.strip]
110
+ end
111
+ end
112
+
113
+ private
114
+ def may_cache_method?
115
+ verb == :get || verb == :post
116
+ end
117
+ end
118
+
119
+ class Net::HTTPResponse
120
+ include Restfulie::Client::HTTP::ResponseStatus
121
+ include Restfulie::Client::HTTP::ResponseCacheCheck
122
+
123
+ end
@@ -0,0 +1,13 @@
1
+ module Restfulie::Client::Cache
2
+ module Restrictions
3
+ class << self
4
+
5
+ # checks whether this request verb and its cache headers allow caching
6
+ def may_cache?(response)
7
+ response && response.may_cache?
8
+ end
9
+
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Restfulie
2
+ module Client
3
+ module Cache
4
+ autoload :Basic, 'restfulie/client/cache/basic'
5
+ autoload :Fake, 'restfulie/client/cache/fake'
6
+ autoload :Restrictions, 'restfulie/client/cache/restrictions'
7
+ end
8
+ end
9
+ end
10
+ require 'restfulie/client/cache/http_ext'
11
+
@@ -0,0 +1,67 @@
1
+ module Restfulie
2
+ module Client #:nodoc:
3
+ # Use this class to configure the entry point and other relevant behaviors related to accessing or interacting with resources
4
+ #
5
+ # The available options are:
6
+ #
7
+ # * <tt>:entry_point</tt> - The URI for an entry point, such as http://resource.entrypoint.com/post
8
+ # * <tt>:representations</tt> - Representations.
9
+ #
10
+ # You can also store any other custom configuration.
11
+ #
12
+ # ==== Example
13
+ #
14
+ # configuration = Configuration.new
15
+ # configuration[:entry_point] = 'http://resource.entrypoint.com/post'
16
+ # configuration[:entry_point] # => 'http://resource.entrypoint.com/post'
17
+ #
18
+ # or you can use:
19
+ #
20
+ # configuration.entry_point = 'http://resource.entrypoint.com/post'
21
+ # configuration.entry_point # => 'http://resource.entrypoint.com/post'
22
+ class Configuration < ::Hash
23
+ # the current environment
24
+ attr_reader :environment
25
+
26
+ @@default_configuration = {
27
+ :entry_point => '',
28
+ :representations => {}
29
+ }
30
+
31
+ def initialize
32
+ super
33
+ self.environment = :development
34
+ end
35
+
36
+ # this will store a new configuration (based on the default) for the environment passed by value.
37
+ def environment=(value)
38
+ @environment = value
39
+ unless has_key?(@environment)
40
+ dee_clone = Marshal::load(Marshal::dump(@@default_configuration))
41
+ store(@environment,dee_clone)
42
+ end
43
+ @environment
44
+ end
45
+
46
+ # access (key) configuration value
47
+ def [](key)
48
+ fetch(@environment)[key]
49
+ end
50
+
51
+ # store on (key) configuration the value
52
+ def []=(key,value)
53
+ fetch(@environment)[key] = value
54
+ end
55
+
56
+ def method_missing(name, *args, &block)
57
+ method_name = name.to_s
58
+ if method_name.last == '='
59
+ fetch(environment)[method_name.chop.to_sym] = args[0]
60
+ else
61
+ value = fetch(environment)[name]
62
+ value ? value : super
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,66 @@
1
+ module Restfulie::Client
2
+ class Dsl
3
+
4
+ def initialize
5
+ @requests = []
6
+ trait :base
7
+ trait :verb
8
+ request :base_request
9
+ request :setup_header
10
+ request :serialize_body
11
+ request :enhance_response
12
+ # request :cache
13
+ request :follow_request
14
+ end
15
+
16
+ def request(what)
17
+ req = "Restfulie::Client::Feature::#{what.to_s.classify}".constantize
18
+ @requests << req
19
+ self
20
+ end
21
+
22
+ def trait(sym)
23
+ t = "Restfulie::Client::Feature::#{sym.to_s.classify}".constantize
24
+ self.extend t
25
+ self
26
+ end
27
+
28
+ def method_missing(sym, *args)
29
+ if Restfulie::Client::Feature.const_defined? sym.to_s.classify
30
+ loaded = true
31
+ trait sym
32
+ end
33
+ if Restfulie::Client::Feature.const_defined? "#{sym.to_s.classify}Request"
34
+ loaded = true
35
+ request "#{sym.to_s}Request"
36
+ end
37
+ if loaded
38
+ self
39
+ else
40
+ super sym, *args
41
+ end
42
+ end
43
+
44
+ def request_flow(env = {})
45
+ Parser.new(@requests).continue(self, nil, env)
46
+ end
47
+
48
+ end
49
+
50
+ class Parser
51
+
52
+ def initialize(stack)
53
+ @stack = stack.dup
54
+ end
55
+
56
+ def continue(request, response, env)
57
+ current = @stack.pop
58
+ if current.nil?
59
+ return response
60
+ end
61
+ filter = current.new
62
+ filter.execute(self, request, response, env)
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,61 @@
1
+
2
+ module Restfulie
3
+ module Client#:nodoc
4
+
5
+ module HTTP::RecipeModule
6
+ def recipe(converter_sym, options={}, &block)
7
+ raise 'Undefined block' unless block_given?
8
+ converter = "Restfulie::Common::Converter::#{converter_sym.to_s.camelize}".constantize
9
+ converter.describe_recipe(options[:name], &block)
10
+ end
11
+
12
+ @resources_configurations = {}
13
+ def configuration_of(resource_name)
14
+ @resources_configurations[resource_name]
15
+ end
16
+
17
+ def configuration_for(resource_name,configuration = Configuration.new)
18
+ yield configuration if block_given?
19
+ @resources_configurations[resource_name] = configuration
20
+ end
21
+
22
+ def retrieve(resource_name)
23
+ returning Object.new do |resource|
24
+ resource.extend(Base)
25
+ resource.configure
26
+ end
27
+ end
28
+ end
29
+
30
+ class HTTP::Recipe < MasterDelegator
31
+
32
+ def initialize(requester)
33
+ @requester = requester
34
+ @resources_configurations = {}
35
+ end
36
+
37
+ include Restfulie::Client::HTTP::RecipeModule
38
+
39
+ end
40
+
41
+ class EntryPoint
42
+
43
+ @resources_configurations = {}
44
+ extend Restfulie::Client::HTTP::RecipeModule
45
+
46
+ def initialize(requester)
47
+ @requester = requester
48
+ end
49
+
50
+ def self.at(uri)
51
+ Restfulie.using {
52
+ recipe
53
+ follow_link
54
+ request_marshaller
55
+ verb_request
56
+ }.at(uri)
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,14 @@
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
@@ -0,0 +1,22 @@
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
+
@@ -0,0 +1,16 @@
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
@@ -0,0 +1,75 @@
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
@@ -0,0 +1,35 @@
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
@@ -0,0 +1,16 @@
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
@@ -0,0 +1,12 @@
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
@@ -0,0 +1,41 @@
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
@@ -0,0 +1,26 @@
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
@@ -0,0 +1,19 @@
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
@@ -0,0 +1,25 @@
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
@@ -0,0 +1,21 @@
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