kaiwren-wrest 0.0.6 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +83 -31
- data/Rakefile +29 -13
- data/VERSION.yml +1 -1
- data/examples/delicious.rb +58 -0
- data/examples/wow_realm_status.rb +57 -0
- data/lib/wrest.rb +11 -2
- data/lib/wrest/components.rb +1 -2
- data/lib/wrest/components/attributes_container.rb +34 -74
- data/lib/wrest/components/attributes_container/typecaster.rb +121 -0
- data/lib/wrest/components/mutators.rb +18 -1
- data/lib/wrest/components/mutators/base.rb +52 -39
- data/lib/wrest/components/mutators/camel_to_snake_case.rb +7 -5
- data/lib/wrest/components/mutators/xml_mini_type_caster.rb +43 -0
- data/lib/wrest/components/mutators/xml_simple_type_caster.rb +22 -20
- data/lib/wrest/components/translators.rb +20 -17
- data/lib/wrest/components/translators/content_types.rb +2 -2
- data/lib/wrest/components/translators/json.rb +11 -8
- data/lib/wrest/components/translators/xml.rb +9 -12
- data/lib/wrest/core_ext/hash/conversions.rb +1 -1
- data/lib/wrest/core_ext/string/conversions.rb +2 -2
- data/lib/wrest/http.rb +24 -0
- data/lib/wrest/http/delete.rb +23 -0
- data/lib/wrest/http/get.rb +23 -0
- data/lib/wrest/http/options.rb +23 -0
- data/lib/wrest/http/post.rb +23 -0
- data/lib/wrest/http/put.rb +23 -0
- data/lib/wrest/http/request.rb +48 -0
- data/lib/wrest/http/response.rb +44 -0
- data/lib/wrest/resource/base.rb +52 -25
- data/lib/wrest/resource/state.rb +6 -0
- data/lib/wrest/uri.rb +85 -29
- data/lib/wrest/uri_template.rb +18 -1
- data/lib/wrest/version.rb +1 -1
- data/spec/spec.opts +1 -1
- data/spec/spec_helper.rb +8 -1
- data/spec/wrest/components/attributes_container/typecaster_spec.rb +63 -0
- data/spec/wrest/components/attributes_container_spec.rb +17 -61
- data/spec/wrest/components/mutators/base_spec.rb +5 -1
- data/spec/wrest/components/mutators/xml_mini_type_caster_spec.rb +75 -0
- data/spec/wrest/components/mutators_spec.rb +21 -0
- data/spec/wrest/components/translators/xml_spec.rb +1 -1
- data/spec/wrest/components/translators_spec.rb +9 -0
- data/spec/wrest/core_ext/string/conversions_spec.rb +9 -0
- data/spec/wrest/http/request_spec.rb +22 -0
- data/spec/wrest/{response_spec.rb → http/response_spec.rb} +4 -4
- data/spec/wrest/resource/base_spec.rb +126 -11
- data/spec/wrest/uri_spec.rb +124 -20
- data/spec/wrest/uri_template_spec.rb +11 -1
- metadata +33 -20
- data/lib/wrest/components/typecast_helpers.rb +0 -41
- data/lib/wrest/response.rb +0 -38
@@ -7,20 +7,17 @@
|
|
7
7
|
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
8
8
|
# See the License for the specific language governing permissions and limitations under the License.
|
9
9
|
|
10
|
-
|
10
|
+
module Wrest
|
11
|
+
module Components::Translators
|
12
|
+
module Xml
|
13
|
+
extend self
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
+
def deserialise(response)
|
16
|
+
ActiveSupport::XmlMini.parse(response.body)
|
17
|
+
end
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
response.body,
|
19
|
-
'keeproot' => true
|
20
|
-
)
|
21
|
-
end
|
22
|
-
|
23
|
-
def serialise(hash)
|
19
|
+
def serialise(hash)
|
20
|
+
end
|
24
21
|
end
|
25
22
|
end
|
26
23
|
end
|
@@ -22,7 +22,7 @@ module Wrest
|
|
22
22
|
# Yes, the name is misleading in that respect. However, one
|
23
23
|
# hopes the absence of an exclamation mark will increase clarity.
|
24
24
|
#
|
25
|
-
# Uses include mutating the hash produced by xml
|
25
|
+
# Uses include mutating the hash produced by deserialising xml
|
26
26
|
# by using the meta data in the hash to type cast values.
|
27
27
|
#
|
28
28
|
# Example:
|
data/lib/wrest/http.rb
ADDED
@@ -0,0 +1,24 @@
|
|
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
|
+
|
12
|
+
# Contains all HTTP protocol related classes such as
|
13
|
+
# Get, Post, Request, Response etc.
|
14
|
+
module Http
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
require "#{WREST_ROOT}/wrest/http/response"
|
19
|
+
require "#{WREST_ROOT}/wrest/http/request"
|
20
|
+
require "#{WREST_ROOT}/wrest/http/get"
|
21
|
+
require "#{WREST_ROOT}/wrest/http/put"
|
22
|
+
require "#{WREST_ROOT}/wrest/http/post"
|
23
|
+
require "#{WREST_ROOT}/wrest/http/delete"
|
24
|
+
require "#{WREST_ROOT}/wrest/http/options"
|
@@ -0,0 +1,23 @@
|
|
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::Http
|
11
|
+
class Delete < Request
|
12
|
+
def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
|
13
|
+
super(
|
14
|
+
wrest_uri,
|
15
|
+
Net::HTTP::Delete,
|
16
|
+
parameters,
|
17
|
+
nil,
|
18
|
+
headers,
|
19
|
+
options
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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::Http
|
11
|
+
class Get < Request
|
12
|
+
def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
|
13
|
+
super(
|
14
|
+
wrest_uri,
|
15
|
+
Net::HTTP::Get,
|
16
|
+
parameters,
|
17
|
+
nil,
|
18
|
+
headers,
|
19
|
+
options
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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::Http
|
11
|
+
class Options < Request
|
12
|
+
def initialize(wrest_uri, options = {})
|
13
|
+
super(
|
14
|
+
wrest_uri,
|
15
|
+
Net::HTTP::Options,
|
16
|
+
{},
|
17
|
+
nil,
|
18
|
+
{},
|
19
|
+
options
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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::Http
|
11
|
+
class Post < Request
|
12
|
+
def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
|
13
|
+
super(
|
14
|
+
wrest_uri,
|
15
|
+
Net::HTTP::Post,
|
16
|
+
parameters,
|
17
|
+
body,
|
18
|
+
headers,
|
19
|
+
options
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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::Http
|
11
|
+
class Put < Request
|
12
|
+
def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
|
13
|
+
super(
|
14
|
+
wrest_uri,
|
15
|
+
Net::HTTP::Put,
|
16
|
+
parameters,
|
17
|
+
body,
|
18
|
+
headers,
|
19
|
+
options
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,48 @@
|
|
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::Http
|
11
|
+
class Request
|
12
|
+
attr_reader :http_request, :uri, :body, :headers, :username, :password
|
13
|
+
def initialize(wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, options = {})
|
14
|
+
@uri = wrest_uri
|
15
|
+
@headers = headers.stringify_keys
|
16
|
+
@http_request = http_request_klass.new(parameters.empty? ? wrest_uri.full_path : "#{wrest_uri.full_path}?#{parameters.to_query}", @headers)
|
17
|
+
@body = body
|
18
|
+
@options = options
|
19
|
+
@username = options[:username]
|
20
|
+
@password = options[:password]
|
21
|
+
end
|
22
|
+
|
23
|
+
# Makes a request and returns a Wrest::Http::Response.
|
24
|
+
# Data about the request is and logged to Wrest.logger
|
25
|
+
def invoke
|
26
|
+
response = nil
|
27
|
+
|
28
|
+
prefix = "#{http_request.method} #{http_request.hash}"
|
29
|
+
http_request.basic_auth username, password
|
30
|
+
|
31
|
+
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) ) }
|
33
|
+
Wrest.logger.debug "<-- (#{prefix}) %d %s (%d bytes %.2fs)" % [response.code, response.message, response.body ? response.body.length : 0, time]
|
34
|
+
|
35
|
+
response
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def http
|
40
|
+
http = Net::HTTP.new(@uri.host, @uri.port)
|
41
|
+
if @uri.https?
|
42
|
+
http.use_ssl = true
|
43
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
44
|
+
end
|
45
|
+
http
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,44 @@
|
|
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
|
+
# Decorates a response providing support for deserialisation.
|
13
|
+
#
|
14
|
+
# The following methods are also available (unlisted by rdoc because they're forwarded):
|
15
|
+
#
|
16
|
+
# <tt>:@Http_response, :code, :message, :body, :Http_version,
|
17
|
+
# :[], :content_length, :content_type, :each_header, :each_name, :each_value, :fetch,
|
18
|
+
# :get_fields, :key?, :type_params</tt>
|
19
|
+
#
|
20
|
+
# They behave exactly like their Net::HttpResponse equivalents.
|
21
|
+
class Response
|
22
|
+
extend Forwardable
|
23
|
+
def_delegators :@http_response, :code, :message, :body, :Http_version,
|
24
|
+
:[], :content_length, :content_type, :each_header, :each_name, :each_value, :fetch,
|
25
|
+
:get_fields, :key?, :type_params
|
26
|
+
|
27
|
+
def initialize(http_response)
|
28
|
+
@http_response = http_response
|
29
|
+
end
|
30
|
+
|
31
|
+
def deserialise
|
32
|
+
deserialise_using(Wrest::Components::Translators.lookup(@http_response.content_type))
|
33
|
+
end
|
34
|
+
|
35
|
+
def deserialise_using(translator)
|
36
|
+
translator.deserialise(@http_response)
|
37
|
+
end
|
38
|
+
|
39
|
+
def headers
|
40
|
+
@http_response.to_hash
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/wrest/resource/base.rb
CHANGED
@@ -12,61 +12,88 @@ 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
|
15
16
|
|
16
|
-
|
17
|
+
always_has :id
|
17
18
|
typecast :id => as_integer
|
18
19
|
attr_reader :attributes
|
19
|
-
|
20
|
+
|
21
|
+
def ==(other)
|
22
|
+
return true if self.equal?(other)
|
23
|
+
return false unless other.class == self.class
|
24
|
+
return self.attributes == other.attributes
|
25
|
+
end
|
26
|
+
|
27
|
+
def hash
|
28
|
+
id.hash
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_xml(options={})
|
32
|
+
attributes.to_xml({:root => self.class.resource_name.gsub('_', '-')}.merge(options))
|
33
|
+
end
|
34
|
+
|
20
35
|
class << self
|
21
36
|
def inherited(klass)
|
22
37
|
klass.set_resource_name klass.name
|
23
38
|
end
|
24
|
-
|
39
|
+
|
25
40
|
# Allows the resource name to be configured and creates
|
26
|
-
# a getter method for it.
|
41
|
+
# a getter method for it.
|
27
42
|
# This is a useful feature when using anonymous classes like
|
28
43
|
# we often do while writing tests.
|
29
44
|
# By default, the resource name is set to the name of the class.
|
30
45
|
def set_resource_name(resource_name)
|
31
|
-
self.class_eval "def self.resource_name; '#{resource_name}';end"
|
46
|
+
self.class_eval "def self.resource_name; '#{resource_name.underscore}';end"
|
32
47
|
end
|
33
|
-
|
48
|
+
|
34
49
|
# Allows the host url at which the resource is found to be configured
|
35
|
-
# and creates a getter method for it.
|
50
|
+
# and creates a getter method for it.
|
36
51
|
# For example in the url
|
37
52
|
# http://localhost:3000/users/1/settings
|
38
|
-
# you would set
|
53
|
+
# you would set
|
39
54
|
# http://localhost:3000
|
40
55
|
# as the host url.
|
41
56
|
def set_host(host)
|
42
57
|
self.class_eval "def self.host; '#{host}';end"
|
43
58
|
end
|
44
|
-
|
45
|
-
def
|
59
|
+
|
60
|
+
def set_default_format(format)
|
61
|
+
self.class_eval "def self.default_format; '#{format.to_s}';end"
|
46
62
|
end
|
47
|
-
|
48
|
-
def
|
49
|
-
@resource_path ||= "/#{resource_name.underscore.pluralize}"
|
63
|
+
|
64
|
+
def set_redirect_handler(method_object)
|
50
65
|
end
|
51
66
|
|
52
|
-
def
|
53
|
-
"#{
|
67
|
+
def resource_collection_name
|
68
|
+
@resource_collection_name ||= "#{resource_name.underscore.pluralize}"
|
54
69
|
end
|
55
|
-
|
56
|
-
def
|
70
|
+
|
71
|
+
def find_one_uri_template
|
72
|
+
@find_one_template ||= Wrest::UriTemplate.new(':host/:resource_collection_name/:id.:format')
|
57
73
|
end
|
58
74
|
|
59
75
|
def find(id)
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
76
|
+
response = find_one_uri_template.to_uri(
|
77
|
+
:host => host,
|
78
|
+
:resource_collection_name => resource_collection_name,
|
79
|
+
:id => id,
|
80
|
+
:format => default_format
|
81
|
+
).get
|
82
|
+
self.new(response.deserialise.mutate_using(
|
83
|
+
Wrest::Components::Mutators.chain(:xml_mini_type_caster, :camel_to_snake_case)
|
84
|
+
).shift.last)
|
67
85
|
end
|
68
86
|
|
69
|
-
def
|
87
|
+
def create(attributes = {})
|
88
|
+
response = Wrest::UriTemplate.new(':host/:resource_collection_name.:format').to_uri(
|
89
|
+
:host => host,
|
90
|
+
:resource_collection_name => resource_collection_name,
|
91
|
+
:format => default_format
|
92
|
+
).post(self.new(attributes).to_xml, 'Content-Type' => "application/#{default_format}")
|
93
|
+
|
94
|
+
self.new(response.deserialise.mutate_using(
|
95
|
+
Wrest::Components::Mutators.chain(:xml_mini_type_caster, :camel_to_snake_case)
|
96
|
+
).shift.last)
|
70
97
|
end
|
71
98
|
end
|
72
99
|
end
|
data/lib/wrest/uri.rb
CHANGED
@@ -11,10 +11,44 @@ module Wrest #:nodoc:
|
|
11
11
|
# Wrest::Uri provides a simple api for
|
12
12
|
# REST calls. String#to_uri is a convenience
|
13
13
|
# method to build a Wrest::Uri from a string url.
|
14
|
+
# Note that a Wrest::Uri is immutable.
|
15
|
+
#
|
16
|
+
# Basic HTTP Authentication is supported.
|
17
|
+
# Example:
|
18
|
+
# "http://kaiwren:fupuppies@coathangers.com/portal/1".to_uri
|
19
|
+
# "http://coathangers.com/portal/1".to_uri(:username => 'kaiwren', :password => 'fupuppies')
|
20
|
+
#
|
21
|
+
# The second form is preferred as it can handle passwords with special characters like ^ and @
|
22
|
+
#
|
23
|
+
# You can find examples that use real APIs (like delicious) under the wrest/examples directory.
|
14
24
|
class Uri
|
15
|
-
attr_reader :uri
|
16
|
-
def initialize(uri_string)
|
25
|
+
attr_reader :uri, :username, :password
|
26
|
+
def initialize(uri_string, options = {})
|
27
|
+
@options = options
|
28
|
+
@uri_string = uri_string.clone
|
17
29
|
@uri = URI.parse(uri_string)
|
30
|
+
@options = options
|
31
|
+
@username = (@options[:username] ||= @uri.user)
|
32
|
+
@password = (@options[:password] ||= @uri.password)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Build a new Wrest::Uri by appending _path_ to
|
36
|
+
# the current uri. If the original Wrest::Uri
|
37
|
+
# has a username and password, that will be
|
38
|
+
# copied to the new Wrest::Uri as well.
|
39
|
+
#
|
40
|
+
# Example:
|
41
|
+
# uri = "https://localhost:3000/v1".to_uri
|
42
|
+
# uri['/oogas/1'].get
|
43
|
+
#
|
44
|
+
# To change the username and password on the new
|
45
|
+
# instance, simply pass them as an options map.
|
46
|
+
#
|
47
|
+
# Example:
|
48
|
+
# uri = "https://localhost:3000/v1".to_uri(:username => 'foo', :password => 'bar')
|
49
|
+
# uri['/oogas/1', {:username => 'meh', :password => 'baz'}].get
|
50
|
+
def [](path, options = nil)
|
51
|
+
Uri.new(@uri_string+path, options || @options)
|
18
52
|
end
|
19
53
|
|
20
54
|
def eql?(other)
|
@@ -23,52 +57,74 @@ module Wrest #:nodoc:
|
|
23
57
|
|
24
58
|
def ==(other)
|
25
59
|
return false if other.class != self.class
|
26
|
-
return other.uri == self.uri
|
60
|
+
return other.uri == self.uri && self.username == other.username && self.password == other.password
|
27
61
|
end
|
28
62
|
|
29
63
|
def hash
|
30
|
-
|
64
|
+
@uri.hash + @username.hash + @password.hash + 20090423
|
31
65
|
end
|
32
66
|
|
33
|
-
# Make a
|
34
|
-
#
|
67
|
+
# Make a GET request to this URI. This is a convenience API
|
68
|
+
# that creates a Wrest::Http::Get, executes it and returns a Wrest::Http::Response.
|
69
|
+
#
|
70
|
+
# Remember to escape all parameter strings if necessary, using URI.escape
|
35
71
|
def get(parameters = {}, headers = {})
|
36
|
-
|
72
|
+
Http::Get.new(self, parameters, headers, @options).invoke
|
37
73
|
end
|
38
74
|
|
39
|
-
|
40
|
-
|
75
|
+
# Make a PUT request to this URI. This is a convenience API
|
76
|
+
# that creates a Wrest::Http::Put, executes it and returns a Wrest::Http::Response.
|
77
|
+
#
|
78
|
+
# Remember to escape all parameter strings if necessary, using URI.escape
|
79
|
+
def put(body = '', headers = {}, parameters = {})
|
80
|
+
Http::Put.new(self, body.to_s, headers, parameters, @options).invoke
|
41
81
|
end
|
42
82
|
|
43
|
-
|
44
|
-
|
83
|
+
# Makes a POST request to this URI. This is a convenience API
|
84
|
+
# that creates a Wrest::Http::Post, executes it and returns a Wrest::Http::Response.
|
85
|
+
#
|
86
|
+
# Remember to escape all parameter strings if necessary, using URI.escape
|
87
|
+
def post(body = '', headers = {}, parameters = {})
|
88
|
+
Http::Post.new(self, body.to_s, headers, parameters, @options).invoke
|
45
89
|
end
|
46
90
|
|
91
|
+
# Makes a DELETE request to this URI. This is a convenience API
|
92
|
+
# that creates a Wrest::Http::Delete, executes it and returns a Wrest::Http::Response.
|
93
|
+
#
|
94
|
+
# Remember to escape all parameter strings if necessary, using URI.escape
|
47
95
|
def delete(parameters = {}, headers = {})
|
48
|
-
|
96
|
+
Http::Delete.new(self, parameters, headers, @options).invoke
|
49
97
|
end
|
50
|
-
|
51
|
-
def do_request(method, url, *args)
|
52
|
-
response = nil
|
53
|
-
|
54
|
-
Wrest.logger.info "#{method} -> #{url}"
|
55
|
-
time = Benchmark.realtime { response = Wrest::Response.new(http.send(method, url, *args)) }
|
56
|
-
Wrest.logger.info "--> %d %s (%d %.2fs)" % [response.code, response.message, response.body ? response.body.length : 0, time]
|
57
98
|
|
58
|
-
|
99
|
+
# Makes an OPTIONS request to this URI. This is a convenience API
|
100
|
+
# that creates a Wrest::Http::Options, executes it and returns the Wrest::Http::Response.
|
101
|
+
def options
|
102
|
+
Http::Options.new(self, @options).invoke
|
59
103
|
end
|
60
|
-
|
104
|
+
|
61
105
|
def https?
|
62
106
|
@uri.is_a?(URI::HTTPS)
|
63
107
|
end
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
108
|
+
|
109
|
+
# Provides the full path of a request.
|
110
|
+
# For example, for
|
111
|
+
# http://localhost:3000/demons/1/chi?sort=true
|
112
|
+
# this would return
|
113
|
+
# /demons/1/chi?sort=true
|
114
|
+
def full_path
|
115
|
+
uri.request_uri
|
116
|
+
end
|
117
|
+
|
118
|
+
def protocol
|
119
|
+
uri.scheme
|
120
|
+
end
|
121
|
+
|
122
|
+
def host
|
123
|
+
uri.host
|
124
|
+
end
|
125
|
+
|
126
|
+
def port
|
127
|
+
uri.port
|
72
128
|
end
|
73
129
|
end
|
74
130
|
end
|