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.
- data/README.rdoc +24 -17
- data/Rakefile +6 -6
- data/VERSION.yml +1 -1
- data/examples/delicious.rb +7 -3
- data/examples/redirection.rb +27 -0
- data/examples/twitter.rb +78 -0
- data/{spec/wrest/http/request_spec.rb → examples/twitter_public_timeline.rb} +15 -12
- data/examples/wow_realm_status.rb +7 -3
- data/lib/wrest/components/attributes_container.rb +23 -5
- data/lib/wrest/components/attributes_container/alias_accessors.rb +66 -0
- data/lib/wrest/components/attributes_container/typecaster.rb +44 -38
- data/lib/wrest/components/mutators/base.rb +1 -1
- data/lib/wrest/components/translators.rb +1 -1
- data/lib/wrest/core_ext/string/conversions.rb +1 -1
- data/lib/wrest/exceptions.rb +14 -2
- data/lib/wrest/http.rb +1 -0
- data/lib/wrest/http/get.rb +2 -0
- data/lib/wrest/http/redirection.rb +35 -0
- data/lib/wrest/http/request.rb +37 -13
- data/lib/wrest/http/response.rb +17 -0
- data/lib/wrest/resource/base.rb +1 -2
- data/lib/wrest/uri.rb +2 -0
- data/lib/wrest/version.rb +1 -1
- data/spec/functional/sample_rails_app/README +3 -0
- data/spec/functional/sample_rails_app/Rakefile +10 -0
- data/spec/functional/sample_rails_app/app/controllers/application_controller.rb +10 -0
- data/spec/functional/sample_rails_app/app/controllers/lead_bottles_controller.rb +7 -0
- data/spec/functional/sample_rails_app/app/helpers/application_helper.rb +3 -0
- data/spec/functional/sample_rails_app/app/models/bottle.rb +3 -0
- data/spec/functional/sample_rails_app/app/models/glass_bottle.rb +3 -0
- data/spec/functional/sample_rails_app/app/models/lead_bottle.rb +3 -0
- data/spec/functional/sample_rails_app/config/boot.rb +110 -0
- data/spec/functional/sample_rails_app/config/database.yml +16 -0
- data/spec/functional/sample_rails_app/config/environment.rb +42 -0
- data/spec/functional/sample_rails_app/config/environments/development.rb +17 -0
- data/spec/functional/sample_rails_app/config/environments/production.rb +28 -0
- data/spec/functional/sample_rails_app/config/environments/test.rb +28 -0
- data/spec/functional/sample_rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/functional/sample_rails_app/config/initializers/inflections.rb +10 -0
- data/spec/functional/sample_rails_app/config/initializers/mime_types.rb +5 -0
- data/spec/functional/sample_rails_app/config/initializers/new_rails_defaults.rb +19 -0
- data/spec/functional/sample_rails_app/config/initializers/session_store.rb +15 -0
- data/spec/functional/sample_rails_app/config/locales/en.yml +5 -0
- data/spec/functional/sample_rails_app/config/routes.rb +3 -0
- data/spec/functional/sample_rails_app/db/development.sqlite3 +0 -0
- data/spec/functional/sample_rails_app/db/migrate/20090319115628_create_bottle.rb +13 -0
- data/spec/functional/sample_rails_app/db/schema.rb +20 -0
- data/spec/functional/sample_rails_app/db/test.sqlite3 +0 -0
- data/spec/functional/sample_rails_app/log/development.log +1 -0
- data/spec/functional/sample_rails_app/public/404.html +30 -0
- data/spec/functional/sample_rails_app/public/422.html +30 -0
- data/spec/functional/sample_rails_app/public/500.html +30 -0
- data/spec/functional/sample_rails_app/public/favicon.ico +0 -0
- data/spec/functional/sample_rails_app/public/images/rails.png +0 -0
- data/spec/functional/sample_rails_app/public/index.html +275 -0
- data/spec/functional/sample_rails_app/public/robots.txt +5 -0
- data/spec/functional/sample_rails_app/script/about +4 -0
- data/spec/functional/sample_rails_app/script/autospec +6 -0
- data/spec/functional/sample_rails_app/script/console +3 -0
- data/spec/functional/sample_rails_app/script/dbconsole +3 -0
- data/spec/functional/sample_rails_app/script/destroy +3 -0
- data/spec/functional/sample_rails_app/script/generate +3 -0
- data/spec/functional/sample_rails_app/script/performance/benchmarker +3 -0
- data/spec/functional/sample_rails_app/script/performance/profiler +3 -0
- data/spec/functional/sample_rails_app/script/plugin +3 -0
- data/spec/functional/sample_rails_app/script/runner +3 -0
- data/spec/functional/sample_rails_app/script/server +3 -0
- data/spec/functional/sample_rails_app/script/spec +10 -0
- data/spec/functional/sample_rails_app/script/spec_server +9 -0
- data/spec/functional/sample_rails_app/test/performance/browsing_test.rb +9 -0
- data/spec/functional/sample_rails_app/test/test_helper.rb +38 -0
- data/spec/functional/sample_rails_app/tmtags +2559 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/MIT-LICENSE +20 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/README.rdoc +100 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/Rakefile +18 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/init.rb +5 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/install.rb +1 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full.rb +14 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/base.rb +140 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources.rb +16 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources_controller.rb +26 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/routes_controller.rb +16 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/api.rb +26 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/exception.rb +23 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/from_json.rb +15 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/dispatch.rb +235 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/models/resourced_route.rb +84 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/query.rb +337 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render.rb +63 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/html.rb +50 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/json.rb +75 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/xml.rb +65 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/retrieve.rb +74 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/version.rb +9 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/base_spec.rb +88 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/controllers/resources_spec.rb +29 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/dispatch_spec.rb +262 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/models/resourced_route_spec.rb +62 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query/parameter_spec.rb +57 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query_spec.rb +462 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/html_spec.rb +4 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/json_spec.rb +107 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/xml_spec.rb +98 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render_spec.rb +5 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/retrieve_spec.rb +173 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/spec_helper.rb +98 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/uninstall.rb +1 -0
- data/spec/functional/spec_helper.rb +0 -0
- data/spec/{spec_helper.rb → unit/spec_helper.rb} +2 -2
- data/spec/unit/wrest/components/attributes_container/alias_accessors_spec.rb +49 -0
- data/spec/{wrest → unit/wrest}/components/attributes_container/typecaster_spec.rb +28 -8
- data/spec/{wrest → unit/wrest}/components/attributes_container_spec.rb +42 -15
- data/spec/{wrest → unit/wrest}/components/mutators/base_spec.rb +1 -1
- data/spec/{wrest → unit/wrest}/components/mutators/camel_to_snake_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/components/mutators/xml_mini_type_caster_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/components/mutators/xml_simple_type_caster_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/components/mutators_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/components/translators/xml_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/components/translators_spec.rb +1 -1
- data/spec/{wrest → unit/wrest}/core_ext/hash/conversions_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/core_ext/string/conversions_spec.rb +14 -0
- data/spec/unit/wrest/http/redirection_spec.rb +42 -0
- data/spec/unit/wrest/http/request_spec.rb +70 -0
- data/spec/unit/wrest/http/response_spec.rb +45 -0
- data/spec/{wrest → unit/wrest}/resource/base_spec.rb +5 -4
- data/spec/{wrest → unit/wrest}/uri_spec.rb +68 -67
- data/spec/{wrest → unit/wrest}/uri_template_spec.rb +0 -0
- metadata +188 -39
- data/lib/wrest/exceptions/method_not_overridden_exception.rb +0 -17
- data/lib/wrest/exceptions/unsupported_content_type_exception.rb +0 -17
- 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
|
|
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
|
|
@@ -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::
|
|
20
|
+
translator || (raise Wrest::Exceptions::UnsupportedContentType.new("Unsupported content type #{content_type}"))
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
end
|
data/lib/wrest/exceptions.rb
CHANGED
|
@@ -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
data/lib/wrest/http/get.rb
CHANGED
|
@@ -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
|
data/lib/wrest/http/request.rb
CHANGED
|
@@ -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
|
-
|
|
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(
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
43
|
-
|
|
66
|
+
connection.use_ssl = true
|
|
67
|
+
connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
44
68
|
end
|
|
45
|
-
|
|
69
|
+
connection
|
|
46
70
|
end
|
|
47
71
|
end
|
|
48
72
|
end
|
data/lib/wrest/http/response.rb
CHANGED
|
@@ -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
|
data/lib/wrest/resource/base.rb
CHANGED
|
@@ -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
|
@@ -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,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!
|