wrest 0.0.8-java → 0.0.9-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. data/README.rdoc +24 -17
  2. data/Rakefile +6 -6
  3. data/VERSION.yml +1 -1
  4. data/examples/delicious.rb +7 -3
  5. data/examples/redirection.rb +27 -0
  6. data/examples/twitter.rb +78 -0
  7. data/{spec/wrest/http/request_spec.rb → examples/twitter_public_timeline.rb} +15 -12
  8. data/examples/wow_realm_status.rb +7 -3
  9. data/lib/wrest/components/attributes_container.rb +23 -5
  10. data/lib/wrest/components/attributes_container/alias_accessors.rb +66 -0
  11. data/lib/wrest/components/attributes_container/typecaster.rb +44 -38
  12. data/lib/wrest/components/mutators/base.rb +1 -1
  13. data/lib/wrest/components/translators.rb +1 -1
  14. data/lib/wrest/core_ext/string/conversions.rb +1 -1
  15. data/lib/wrest/exceptions.rb +14 -2
  16. data/lib/wrest/http.rb +1 -0
  17. data/lib/wrest/http/get.rb +2 -0
  18. data/lib/wrest/http/redirection.rb +35 -0
  19. data/lib/wrest/http/request.rb +37 -13
  20. data/lib/wrest/http/response.rb +17 -0
  21. data/lib/wrest/resource/base.rb +1 -2
  22. data/lib/wrest/uri.rb +2 -0
  23. data/lib/wrest/version.rb +1 -1
  24. data/spec/functional/sample_rails_app/README +3 -0
  25. data/spec/functional/sample_rails_app/Rakefile +10 -0
  26. data/spec/functional/sample_rails_app/app/controllers/application_controller.rb +10 -0
  27. data/spec/functional/sample_rails_app/app/controllers/lead_bottles_controller.rb +7 -0
  28. data/spec/functional/sample_rails_app/app/helpers/application_helper.rb +3 -0
  29. data/spec/functional/sample_rails_app/app/models/bottle.rb +3 -0
  30. data/spec/functional/sample_rails_app/app/models/glass_bottle.rb +3 -0
  31. data/spec/functional/sample_rails_app/app/models/lead_bottle.rb +3 -0
  32. data/spec/functional/sample_rails_app/config/boot.rb +110 -0
  33. data/spec/functional/sample_rails_app/config/database.yml +16 -0
  34. data/spec/functional/sample_rails_app/config/environment.rb +42 -0
  35. data/spec/functional/sample_rails_app/config/environments/development.rb +17 -0
  36. data/spec/functional/sample_rails_app/config/environments/production.rb +28 -0
  37. data/spec/functional/sample_rails_app/config/environments/test.rb +28 -0
  38. data/spec/functional/sample_rails_app/config/initializers/backtrace_silencers.rb +7 -0
  39. data/spec/functional/sample_rails_app/config/initializers/inflections.rb +10 -0
  40. data/spec/functional/sample_rails_app/config/initializers/mime_types.rb +5 -0
  41. data/spec/functional/sample_rails_app/config/initializers/new_rails_defaults.rb +19 -0
  42. data/spec/functional/sample_rails_app/config/initializers/session_store.rb +15 -0
  43. data/spec/functional/sample_rails_app/config/locales/en.yml +5 -0
  44. data/spec/functional/sample_rails_app/config/routes.rb +3 -0
  45. data/spec/functional/sample_rails_app/db/development.sqlite3 +0 -0
  46. data/spec/functional/sample_rails_app/db/migrate/20090319115628_create_bottle.rb +13 -0
  47. data/spec/functional/sample_rails_app/db/schema.rb +20 -0
  48. data/spec/functional/sample_rails_app/db/test.sqlite3 +0 -0
  49. data/spec/functional/sample_rails_app/log/development.log +1 -0
  50. data/spec/functional/sample_rails_app/public/404.html +30 -0
  51. data/spec/functional/sample_rails_app/public/422.html +30 -0
  52. data/spec/functional/sample_rails_app/public/500.html +30 -0
  53. data/spec/functional/sample_rails_app/public/favicon.ico +0 -0
  54. data/spec/functional/sample_rails_app/public/images/rails.png +0 -0
  55. data/spec/functional/sample_rails_app/public/index.html +275 -0
  56. data/spec/functional/sample_rails_app/public/robots.txt +5 -0
  57. data/spec/functional/sample_rails_app/script/about +4 -0
  58. data/spec/functional/sample_rails_app/script/autospec +6 -0
  59. data/spec/functional/sample_rails_app/script/console +3 -0
  60. data/spec/functional/sample_rails_app/script/dbconsole +3 -0
  61. data/spec/functional/sample_rails_app/script/destroy +3 -0
  62. data/spec/functional/sample_rails_app/script/generate +3 -0
  63. data/spec/functional/sample_rails_app/script/performance/benchmarker +3 -0
  64. data/spec/functional/sample_rails_app/script/performance/profiler +3 -0
  65. data/spec/functional/sample_rails_app/script/plugin +3 -0
  66. data/spec/functional/sample_rails_app/script/runner +3 -0
  67. data/spec/functional/sample_rails_app/script/server +3 -0
  68. data/spec/functional/sample_rails_app/script/spec +10 -0
  69. data/spec/functional/sample_rails_app/script/spec_server +9 -0
  70. data/spec/functional/sample_rails_app/test/performance/browsing_test.rb +9 -0
  71. data/spec/functional/sample_rails_app/test/test_helper.rb +38 -0
  72. data/spec/functional/sample_rails_app/tmtags +2559 -0
  73. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/MIT-LICENSE +20 -0
  74. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/README.rdoc +100 -0
  75. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/Rakefile +18 -0
  76. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/init.rb +5 -0
  77. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/install.rb +1 -0
  78. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full.rb +14 -0
  79. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/base.rb +140 -0
  80. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources.rb +16 -0
  81. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources_controller.rb +26 -0
  82. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/routes_controller.rb +16 -0
  83. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/api.rb +26 -0
  84. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/exception.rb +23 -0
  85. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/from_json.rb +15 -0
  86. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/dispatch.rb +235 -0
  87. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/models/resourced_route.rb +84 -0
  88. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/query.rb +337 -0
  89. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render.rb +63 -0
  90. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/html.rb +50 -0
  91. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/json.rb +75 -0
  92. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/xml.rb +65 -0
  93. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/retrieve.rb +74 -0
  94. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/version.rb +9 -0
  95. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/base_spec.rb +88 -0
  96. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/controllers/resources_spec.rb +29 -0
  97. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/dispatch_spec.rb +262 -0
  98. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/models/resourced_route_spec.rb +62 -0
  99. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query/parameter_spec.rb +57 -0
  100. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query_spec.rb +462 -0
  101. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/html_spec.rb +4 -0
  102. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/json_spec.rb +107 -0
  103. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/xml_spec.rb +98 -0
  104. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render_spec.rb +5 -0
  105. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/retrieve_spec.rb +173 -0
  106. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/spec_helper.rb +98 -0
  107. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/uninstall.rb +1 -0
  108. data/spec/functional/spec_helper.rb +0 -0
  109. data/spec/{spec_helper.rb → unit/spec_helper.rb} +2 -2
  110. data/spec/unit/wrest/components/attributes_container/alias_accessors_spec.rb +49 -0
  111. data/spec/{wrest → unit/wrest}/components/attributes_container/typecaster_spec.rb +28 -8
  112. data/spec/{wrest → unit/wrest}/components/attributes_container_spec.rb +42 -15
  113. data/spec/{wrest → unit/wrest}/components/mutators/base_spec.rb +1 -1
  114. data/spec/{wrest → unit/wrest}/components/mutators/camel_to_snake_spec.rb +0 -0
  115. data/spec/{wrest → unit/wrest}/components/mutators/xml_mini_type_caster_spec.rb +0 -0
  116. data/spec/{wrest → unit/wrest}/components/mutators/xml_simple_type_caster_spec.rb +0 -0
  117. data/spec/{wrest → unit/wrest}/components/mutators_spec.rb +0 -0
  118. data/spec/{wrest → unit/wrest}/components/translators/xml_spec.rb +0 -0
  119. data/spec/{wrest → unit/wrest}/components/translators_spec.rb +1 -1
  120. data/spec/{wrest → unit/wrest}/core_ext/hash/conversions_spec.rb +0 -0
  121. data/spec/{wrest → unit/wrest}/core_ext/string/conversions_spec.rb +14 -0
  122. data/spec/unit/wrest/http/redirection_spec.rb +42 -0
  123. data/spec/unit/wrest/http/request_spec.rb +70 -0
  124. data/spec/unit/wrest/http/response_spec.rb +45 -0
  125. data/spec/{wrest → unit/wrest}/resource/base_spec.rb +5 -4
  126. data/spec/{wrest → unit/wrest}/uri_spec.rb +68 -67
  127. data/spec/{wrest → unit/wrest}/uri_template_spec.rb +0 -0
  128. metadata +188 -39
  129. data/lib/wrest/exceptions/method_not_overridden_exception.rb +0 -17
  130. data/lib/wrest/exceptions/unsupported_content_type_exception.rb +0 -17
  131. data/spec/wrest/http/response_spec.rb +0 -21
@@ -26,19 +26,61 @@ module Wrest
26
26
  klass.class_eval{ include Typecaster::InstanceMethods }
27
27
  klass.alias_method_chain :initialize, :typecasting
28
28
  end
29
+
30
+ module Helpers
31
+ def as_base64Binary
32
+ ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['base64Binary']
33
+ end
34
+
35
+ def as_boolean
36
+ ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['boolean']
37
+ end
38
+
39
+ def as_decimal
40
+ ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['decimal']
41
+ end
42
+
43
+ def as_date
44
+ ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['date']
45
+ end
46
+
47
+ def as_datetime
48
+ ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['datetime']
49
+ end
50
+
51
+ def as_float
52
+ ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['float']
53
+ end
54
+
55
+ def as_integer
56
+ ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['integer']
57
+ end
29
58
 
59
+ def as_symbol
60
+ ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['symbol']
61
+ end
62
+
63
+ def as_yaml
64
+ ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['yaml']
65
+ end
66
+ end
67
+
30
68
  module ClassMethods
31
69
  # Accepts a set of attribute-name/lambda pairs which are used
32
70
  # to typecast string values injected through the constructor.
33
71
  # Typically needed when populating an +AttributesContainer+
34
72
  # directly from request params. Typecasting kicks in for
35
- # a given value _only_ if it is a string.
73
+ # a given value _only_ if it is a String, Hash or Array, the
74
+ # three classes that deserilisation can produce.
36
75
  #
37
76
  # Typecast information is inherited by subclasses; however be
38
77
  # aware that explicitly invoking +typecast+ in a subclass will
39
78
  # discard inherited typecast information leaving only the casts
40
79
  # defined in the subclass.
41
80
  #
81
+ # Note that this _will_ increase the time needed to initialize
82
+ # instances.
83
+ #
42
84
  # Common typecasts such as integer, float, datetime etc. are
43
85
  # available through predefined helpers. See TypecastHelpers
44
86
  # for a full list.
@@ -69,42 +111,6 @@ module Wrest
69
111
  {}
70
112
  end
71
113
  end
72
-
73
- def as_base64Binary
74
- ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['base64Binary']
75
- end
76
-
77
- def as_boolean
78
- ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['boolean']
79
- end
80
-
81
- def as_decimal
82
- ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['decimal']
83
- end
84
-
85
- def as_date
86
- ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['date']
87
- end
88
-
89
- def as_datetime
90
- ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['datetime']
91
- end
92
-
93
- def as_float
94
- ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['float']
95
- end
96
-
97
- def as_integer
98
- ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['integer']
99
- end
100
-
101
- def as_symbol
102
- ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['symbol']
103
- end
104
-
105
- def as_yaml
106
- ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['yaml']
107
- end
108
114
  end
109
115
 
110
116
  module InstanceMethods # :nodoc:
@@ -112,7 +118,7 @@ module Wrest
112
118
  initialize_without_typecasting(attributes)
113
119
  self.class.typecast_map.each do |key, typecaster|
114
120
  value = @attributes[key]
115
- @attributes[key] = typecaster.call(value) if value.is_a?(String)
121
+ @attributes[key] = typecaster.call(value) if (value.is_a?(String) || value.is_a?(Hash) || value.is_a?(Array))
116
122
  end
117
123
  end
118
124
  end
@@ -49,7 +49,7 @@ module Wrest
49
49
 
50
50
  protected
51
51
  def do_mutate(tuple)
52
- raise Wrest::Exceptions::MethodNotOverriddenException
52
+ raise Wrest::Exceptions::MethodNotOverridden
53
53
  end
54
54
  end
55
55
  end
@@ -17,7 +17,7 @@ module Wrest
17
17
  # the content type
18
18
  def self.lookup(content_type)
19
19
  translator = CONTENT_TYPES[content_type]
20
- translator || (raise Wrest::Exceptions::UnsupportedContentTypeException.new("Unsupported content type #{content_type}"))
20
+ translator || (raise Wrest::Exceptions::UnsupportedContentType.new("Unsupported content type #{content_type}"))
21
21
  end
22
22
  end
23
23
  end
@@ -15,7 +15,7 @@ module Wrest
15
15
 
16
16
  # A convenience method equivalent to Wrest::Uri.new(string)
17
17
  def to_uri(options = {})
18
- Wrest::Uri.new(self, options)
18
+ Wrest::Uri.new(self.strip, options)
19
19
  end
20
20
  end
21
21
  end
@@ -1,6 +1,18 @@
1
1
  module Wrest
2
2
  module Exceptions #:nodoc:
3
+ # Raised when a base method that should be overriden
4
+ # is invoked on a subclass that has not implemented it.
5
+ class MethodNotOverridden < StandardError
6
+ end
7
+
8
+ # Raised when a translator for an unregisterd response content type
9
+ # is requested. See Translators.
10
+ class UnsupportedContentType < StandardError
11
+ end
12
+
13
+ # Raised when a request auto redirects more times than are allowed
14
+ # by its follow_redirects_limit. See Wrest::Http::Redirection.
15
+ class AutoRedirectLimitExceeded < StandardError
16
+ end
3
17
  end
4
18
  end
5
- require "#{WREST_ROOT}/wrest/exceptions/unsupported_content_type_exception"
6
- require "#{WREST_ROOT}/wrest/exceptions/method_not_overridden_exception"
data/lib/wrest/http.rb CHANGED
@@ -16,6 +16,7 @@ module Wrest #:nodoc:
16
16
  end
17
17
 
18
18
  require "#{WREST_ROOT}/wrest/http/response"
19
+ require "#{WREST_ROOT}/wrest/http/redirection"
19
20
  require "#{WREST_ROOT}/wrest/http/request"
20
21
  require "#{WREST_ROOT}/wrest/http/get"
21
22
  require "#{WREST_ROOT}/wrest/http/put"
@@ -10,6 +10,8 @@
10
10
  module Wrest::Http
11
11
  class Get < Request
12
12
  def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
13
+ follow_redirects = options[:follow_redirects]
14
+ options[:follow_redirects] = (follow_redirects == nil ? true : follow_redirects)
13
15
  super(
14
16
  wrest_uri,
15
17
  Net::HTTP::Get,
@@ -0,0 +1,35 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at Http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
7
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
+ # See the License for the specific language governing permissions and limitations under the License.
9
+
10
+ module Wrest #:nodoc:
11
+ module Http #:nodoc:
12
+ # Constructed by Wrest::Response.new if the HTTP response code is 3xx
13
+ # (http://en.wikipedia.org/wiki/300_Multiple_Choices#3xx_Redirection)
14
+ class Redirection < Response
15
+
16
+ # A get is invoked on the url stored in the response headers
17
+ # under the key 'location' and the new Response is returned.
18
+ #
19
+ # The follow_redirects_count and follow_redirects_limit options
20
+ # should be present. follow_redirects_count will be incremented by 1.
21
+ #
22
+ # This method will raise a Wrest::Exceptions::AutoRedirectLimitExceeded
23
+ # if the follow_redirects_count equals the follow_redirects_limit.
24
+ def follow(redirect_request_options = {})
25
+ target = self['location']
26
+ redirect_request_options = redirect_request_options.clone
27
+
28
+ raise Wrest::Exceptions::AutoRedirectLimitExceeded if (redirect_request_options[:follow_redirects_count] += 1) >= redirect_request_options[:follow_redirects_limit]
29
+
30
+ Wrest.logger.debug "--| Redirecting to #{target}"
31
+ target.to_uri(redirect_request_options).get
32
+ end
33
+ end
34
+ end
35
+ end
@@ -8,16 +8,39 @@
8
8
  # See the License for the specific language governing permissions and limitations under the License.
9
9
 
10
10
  module Wrest::Http
11
+ # This represents a HTTP request. Typically you will never need to instantiate
12
+ # one of these yourself - you can use one of the more conveient APIs via Wrest::Uri
13
+ # or Wrest::Http::Get etc. instead.
11
14
  class Request
12
- attr_reader :http_request, :uri, :body, :headers, :username, :password
15
+ attr_reader :http_request, :uri, :body, :headers, :username, :password, :follow_redirects,
16
+ :follow_redirects_limit, :follow_redirects_count, :timeout
17
+ # Valid tuples for the options are:
18
+ # :username => String, defaults to nil
19
+ # :password => String, defaults to nil
20
+ # :follow_redirects => Boolean, defaults to true for Get, false for anything else
21
+ # :follow_redirects_limit => Integer, defaults to 5. This is the number of redirects
22
+ # that Wrest will automatically follow before raising an
23
+ # Wrest::Exceptions::AutoRedirectLimitExceeded exception.
24
+ # For example, if you set this to 1, the very first redirect
25
+ # will raise the exception.
26
+ # :follow_redirects_count => Integer, defaults to 0. This is a count of the hops made to
27
+ # get to this request and increases by one for every redirect
28
+ # until the follow_redirects_limit is hit. You should never set
29
+ # this option yourself.
30
+ # :timeout => The period, in seconds, after which a Timeout::Error is raised
31
+ # in the event of a connection failing to open. Defaults to 60.
13
32
  def initialize(wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, options = {})
14
33
  @uri = wrest_uri
15
34
  @headers = headers.stringify_keys
16
35
  @http_request = http_request_klass.new(parameters.empty? ? wrest_uri.full_path : "#{wrest_uri.full_path}?#{parameters.to_query}", @headers)
17
36
  @body = body
18
- @options = options
19
- @username = options[:username]
20
- @password = options[:password]
37
+ @options = options.clone
38
+ @username = @options[:username]
39
+ @password = @options[:password]
40
+ @follow_redirects = (@options[:follow_redirects] ||= false)
41
+ @follow_redirects_count = (@options[:follow_redirects_count] ||= 0)
42
+ @follow_redirects_limit = (@options[:follow_redirects_limit] ||= 5)
43
+ @timeout = (@options[:timeout] ||= 60)
21
44
  end
22
45
 
23
46
  # Makes a request and returns a Wrest::Http::Response.
@@ -26,23 +49,24 @@ module Wrest::Http
26
49
  response = nil
27
50
 
28
51
  prefix = "#{http_request.method} #{http_request.hash}"
29
- http_request.basic_auth username, password
52
+ http_connection = create_connection(timeout)
53
+ http_request.basic_auth username, password
30
54
 
31
55
  Wrest.logger.debug "--> (#{prefix}) #{@uri.protocol}://#{@uri.host}:#{@uri.port}#{@http_request.path}"
32
- time = Benchmark.realtime { response = Wrest::Http::Response.new( http.request(@http_request, @body) ) }
56
+ time = Benchmark.realtime { response = Wrest::Http::Response.new( http_connection.request(@http_request, @body) ) }
33
57
  Wrest.logger.debug "<-- (#{prefix}) %d %s (%d bytes %.2fs)" % [response.code, response.message, response.body ? response.body.length : 0, time]
34
58
 
35
- response
59
+ @follow_redirects ? response.follow(@options) : response
36
60
  end
37
61
 
38
- private
39
- def http
40
- http = Net::HTTP.new(@uri.host, @uri.port)
62
+ def create_connection(timeout)
63
+ connection = Net::HTTP.new(@uri.host, @uri.port)
64
+ connection.read_timeout = timeout
41
65
  if @uri.https?
42
- http.use_ssl = true
43
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
66
+ connection.use_ssl = true
67
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
44
68
  end
45
- http
69
+ connection
46
70
  end
47
71
  end
48
72
  end
@@ -24,6 +24,14 @@ module Wrest #:nodoc:
24
24
  :[], :content_length, :content_type, :each_header, :each_name, :each_value, :fetch,
25
25
  :get_fields, :key?, :type_params
26
26
 
27
+ # We're overriding :new to act as a factory so
28
+ # we can build the appropriate Response instance
29
+ def self.new(http_response)
30
+ instance = ((300..399).include?(http_response.code.to_i) ? Wrest::Http::Redirection : self).allocate
31
+ instance.send :initialize, http_response
32
+ instance
33
+ end
34
+
27
35
  def initialize(http_response)
28
36
  @http_response = http_response
29
37
  end
@@ -39,6 +47,15 @@ module Wrest #:nodoc:
39
47
  def headers
40
48
  @http_response.to_hash
41
49
  end
50
+
51
+ # A null object implementation - invoking this method on
52
+ # a response simply returns the same response unless
53
+ # the response is a Redirection (code 3xx), in which case a
54
+ # get is invoked on the url stored in the response headers
55
+ # under the key 'location' and the new Response is returned.
56
+ def follow(redirect_request_options = {})
57
+ self
58
+ end
42
59
  end
43
60
  end
44
61
  end
@@ -12,7 +12,6 @@ module Wrest::Resource #:nodoc:
12
12
  # It is a REST client targetted at Rails REST apps.
13
13
  class Base
14
14
  include Wrest::Components::AttributesContainer
15
- include Wrest::Components::AttributesContainer::Typecaster
16
15
 
17
16
  always_has :id
18
17
  typecast :id => as_integer
@@ -43,7 +42,7 @@ module Wrest::Resource #:nodoc:
43
42
  # we often do while writing tests.
44
43
  # By default, the resource name is set to the name of the class.
45
44
  def set_resource_name(resource_name)
46
- self.class_eval "def self.resource_name; '#{resource_name.underscore}';end"
45
+ self.class_eval "def self.resource_name; '#{resource_name.to_s.underscore}';end"
47
46
  end
48
47
 
49
48
  # Allows the host url at which the resource is found to be configured
data/lib/wrest/uri.rb CHANGED
@@ -23,6 +23,8 @@ module Wrest #:nodoc:
23
23
  # You can find examples that use real APIs (like delicious) under the wrest/examples directory.
24
24
  class Uri
25
25
  attr_reader :uri, :username, :password
26
+
27
+ # See Wrest::Http::Request for the available options and their default values.
26
28
  def initialize(uri_string, options = {})
27
29
  @options = options
28
30
  @uri_string = uri_string.clone
data/lib/wrest/version.rb CHANGED
@@ -12,7 +12,7 @@ module Wrest
12
12
  unless defined? MAJOR
13
13
  MAJOR = 0
14
14
  MINOR = 0
15
- TINY = 8
15
+ TINY = 9
16
16
 
17
17
  STRING = [MAJOR, MINOR, TINY].join('.')
18
18
 
@@ -0,0 +1,3 @@
1
+ == Wrest's Sample Rails App
2
+
3
+ This dummy application is what Wrest's live functional tests run against. RESTful behaviour is supported by the {resource_full}[http://github.com/bguthrie/resource_full] plugin (see vendor/plugins inside the sample app).
@@ -0,0 +1,10 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require(File.join(File.dirname(__FILE__), 'config', 'boot'))
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+ require 'rake/rdoctask'
9
+
10
+ require 'tasks/rails'
@@ -0,0 +1,10 @@
1
+ # Filters added to this controller apply to all controllers in the application.
2
+ # Likewise, all the methods added will be available for all controllers.
3
+
4
+ class ApplicationController < ActionController::Base
5
+ helper :all # include all helpers, all the time
6
+ # protect_from_forgery # See ActionController::RequestForgeryProtection for details
7
+ # session :off
8
+ # Scrub sensitive parameters from your log
9
+ # filter_parameter_logging :password
10
+ end
@@ -0,0 +1,7 @@
1
+ class LeadBottlesController < ResourceFull::Base
2
+ queryable_with :name, :fuzzy => true
3
+
4
+
5
+ responds_to :xml
6
+ responds_to :json
7
+ end
@@ -0,0 +1,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1,3 @@
1
+ class Bottle < ActiveRecord::Base
2
+ end
3
+
@@ -0,0 +1,3 @@
1
+ class GlassBottle < Bottle
2
+ end
3
+
@@ -0,0 +1,3 @@
1
+ class LeadBottle < Bottle
2
+ end
3
+
@@ -0,0 +1,110 @@
1
+ # Don't change this file!
2
+ # Configure your app in config/environment.rb and config/environments/*.rb
3
+
4
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
+
6
+ module Rails
7
+ class << self
8
+ def boot!
9
+ unless booted?
10
+ preinitialize
11
+ pick_boot.run
12
+ end
13
+ end
14
+
15
+ def booted?
16
+ defined? Rails::Initializer
17
+ end
18
+
19
+ def pick_boot
20
+ (vendor_rails? ? VendorBoot : GemBoot).new
21
+ end
22
+
23
+ def vendor_rails?
24
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
25
+ end
26
+
27
+ def preinitialize
28
+ load(preinitializer_path) if File.exist?(preinitializer_path)
29
+ end
30
+
31
+ def preinitializer_path
32
+ "#{RAILS_ROOT}/config/preinitializer.rb"
33
+ end
34
+ end
35
+
36
+ class Boot
37
+ def run
38
+ load_initializer
39
+ Rails::Initializer.run(:set_load_path)
40
+ end
41
+ end
42
+
43
+ class VendorBoot < Boot
44
+ def load_initializer
45
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
+ Rails::Initializer.run(:install_gem_spec_stubs)
47
+ Rails::GemDependency.add_frozen_gem_path
48
+ end
49
+ end
50
+
51
+ class GemBoot < Boot
52
+ def load_initializer
53
+ self.class.load_rubygems
54
+ load_rails_gem
55
+ require 'initializer'
56
+ end
57
+
58
+ def load_rails_gem
59
+ if version = self.class.gem_version
60
+ gem 'rails', version
61
+ else
62
+ gem 'rails'
63
+ end
64
+ rescue Gem::LoadError => load_error
65
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66
+ exit 1
67
+ end
68
+
69
+ class << self
70
+ def rubygems_version
71
+ Gem::RubyGemsVersion rescue nil
72
+ end
73
+
74
+ def gem_version
75
+ if defined? RAILS_GEM_VERSION
76
+ RAILS_GEM_VERSION
77
+ elsif ENV.include?('RAILS_GEM_VERSION')
78
+ ENV['RAILS_GEM_VERSION']
79
+ else
80
+ parse_gem_version(read_environment_rb)
81
+ end
82
+ end
83
+
84
+ def load_rubygems
85
+ require 'rubygems'
86
+ min_version = '1.3.1'
87
+ unless rubygems_version >= min_version
88
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
89
+ exit 1
90
+ end
91
+
92
+ rescue LoadError
93
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
94
+ exit 1
95
+ end
96
+
97
+ def parse_gem_version(text)
98
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
99
+ end
100
+
101
+ private
102
+ def read_environment_rb
103
+ File.read("#{RAILS_ROOT}/config/environment.rb")
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ # All that for this:
110
+ Rails.boot!