wrest 0.0.7-java → 0.0.8-java
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +41 -22
- data/Rakefile +23 -11
- data/VERSION.yml +1 -1
- data/examples/delicious.rb +58 -0
- data/examples/wow_realm_status.rb +57 -0
- data/lib/wrest/components/attributes_container.rb +3 -5
- data/lib/wrest/core_ext/string/conversions.rb +2 -2
- 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/http.rb +24 -0
- data/lib/wrest/resource/base.rb +42 -27
- data/lib/wrest/uri.rb +83 -31
- data/lib/wrest/uri_template.rb +18 -1
- data/lib/wrest/version.rb +1 -1
- data/spec/wrest/components/attributes_container_spec.rb +11 -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 +113 -21
- data/spec/wrest/uri_template_spec.rb +11 -1
- metadata +143 -130
- data/lib/wrest/response.rb +0 -42
data/README.rdoc
CHANGED
@@ -1,10 +1,21 @@
|
|
1
|
-
= Wrest
|
1
|
+
= Wrest
|
2
2
|
|
3
3
|
(c) Copyright 2009 {Sidu Ponnappa}[http://blog.sidu.in]. All Rights Reserved.
|
4
4
|
|
5
5
|
Wrest is a ruby REST client library which allows you to quickly build object oriented wrappers around any web service. It has two components - Wrest Core and Wrest::Resource.
|
6
6
|
|
7
|
-
|
7
|
+
If you were wondering why the words 'demon', 'chi' and 'puppies' (among others) show up in nearly every example and spec, it's because they all refer to characters and ideas from {Roger Zelazny's}[http://en.wikipedia.org/wiki/Roger_Zelazny] last book, 'Lord Demon.'
|
8
|
+
|
9
|
+
== Wrest Core: Features
|
10
|
+
|
11
|
+
* Provides convenient HTTP wrappers, serialisation and deserialisation with caching and redirect handling in the works.
|
12
|
+
* Designed to be used as a library, not just a command line REST client (fewer class methods, more object oriented)
|
13
|
+
* Isn't coupled to Rails (usable in a pure Ruby application to consume any HTTP/REST api)
|
14
|
+
* Can be used both stand alone as well as to build object oriented abstractions around resources and web services (Wrest::Resource is an example of the latter)
|
15
|
+
|
16
|
+
== Examples
|
17
|
+
|
18
|
+
See http://github.com/kaiwren/wrest/tree/master/examples
|
8
19
|
|
9
20
|
== Installation
|
10
21
|
|
@@ -12,20 +23,14 @@ The source is available at git://github.com/kaiwren/wrest.git
|
|
12
23
|
|
13
24
|
To install as a Rails plugin, do <tt>script/plugin install git://github.com/kaiwren/wrest.git</tt>
|
14
25
|
|
15
|
-
To install the Wrest gem, do <tt>sudo gem install wrest</tt
|
16
|
-
|
17
|
-
== Wrest Core
|
18
|
-
|
19
|
-
* Designed to be used as a library, not just a command line REST client
|
20
|
-
* Provides infrastructure components such as convenient HTTP wrappers api, caching, redirect handling, serialisation, deserialisation etc.
|
21
|
-
* Isn't coupled to Rails (usable in a pure Ruby application to consume any REST api)
|
22
|
-
* Can be used both stand alone as well as to build object oriented abstractions around web services (Wrest::Resource is an example of the latter)
|
26
|
+
To install the Wrest gem, do <tt>(sudo) gem install wrest</tt>
|
27
|
+
Wrest is also available as a gem for JRuby; you can instally it by running <tt>(sudo) jruby -S gem install wrest</tt>.
|
23
28
|
|
24
29
|
=== Usage: Shell
|
25
30
|
|
26
31
|
You can launch the interactive Wrest shell by running bin/wrest if you have the source or invoking <tt>wrest</tt> from your prompt if you've installed the gem.
|
27
32
|
$ wrest
|
28
|
-
>>
|
33
|
+
>> y 'http://twitter.com/statuses/public_timeline.json'.to_uri.get.deserialise
|
29
34
|
|
30
35
|
=== Usage: Library
|
31
36
|
|
@@ -38,11 +43,11 @@ You can launch the interactive Wrest shell by running bin/wrest if you have the
|
|
38
43
|
:results=> '3',
|
39
44
|
:start => '1'
|
40
45
|
)
|
41
|
-
=== Basic Http Calls
|
46
|
+
=== Usage: Basic Http Calls
|
42
47
|
|
43
48
|
==== GET
|
44
49
|
|
45
|
-
A couple of ways to get
|
50
|
+
A couple of ways to get Yahoo news as a hash map.
|
46
51
|
|
47
52
|
* This example simply does a get on a uri and figures out the appropriate deserialiser using the content-type (in this case 'text/javascript', which uses Wrest::Translators::Json). See content_types.rb under lib/wrest/mappers/translators.
|
48
53
|
"http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&output=json&query=India&results=3&start=1".to_uri.get.deserialise
|
@@ -60,8 +65,20 @@ A couple of ways to get the Yahoo news as hash map.
|
|
60
65
|
).deserialise_using(
|
61
66
|
Translators::Xml
|
62
67
|
).mutate_using(
|
63
|
-
Mutators::
|
64
|
-
)
|
68
|
+
Mutators::XmlMiniTypeCaster.new
|
69
|
+
)
|
70
|
+
|
71
|
+
==== DELETE
|
72
|
+
|
73
|
+
To delete a resource:
|
74
|
+
|
75
|
+
'https://api.del.icio.us/v1/posts/delete'.to_uri(
|
76
|
+
:username => 'kaiwren',
|
77
|
+
:password => 'fupupp1es'
|
78
|
+
).delete(
|
79
|
+
:url => 'http://c2.com'
|
80
|
+
)
|
81
|
+
|
65
82
|
|
66
83
|
==== OPTIONS
|
67
84
|
|
@@ -70,7 +87,7 @@ To find out what actions are permitted on a URI:
|
|
70
87
|
'http://www.yahoo.com'.to_uri.options.headers['allow']
|
71
88
|
|
72
89
|
|
73
|
-
=== Attributes Container
|
90
|
+
=== Usage: Attributes Container
|
74
91
|
|
75
92
|
Allows any class to hold an attributes hash, somewhat like ActiveResource. It also supports several extensions to this base fuctionality such as support for typecasting attribute values.
|
76
93
|
|
@@ -80,18 +97,18 @@ Example:
|
|
80
97
|
include Wrest::Components::AttributesContainer
|
81
98
|
include Wrest::Components::AttributesContainer::Typecaster
|
82
99
|
|
83
|
-
always_has
|
100
|
+
always_has :id
|
84
101
|
typecast :age => as_integer,
|
85
102
|
:chi => lambda{|chi| Chi.new(chi)}
|
86
103
|
end
|
87
104
|
|
88
105
|
kai_wren = Demon.new('id => '1', 'age' => '1500', 'chi' => '1024', 'teacher' => 'Viss')
|
89
|
-
kai_wren.id
|
90
|
-
kai_wren.age
|
91
|
-
kai_wren.chi
|
106
|
+
kai_wren.id # => '1'
|
107
|
+
kai_wren.age # => 1500
|
108
|
+
kai_wren.chi # => #<Chi:0x113af8c @count="1024">
|
92
109
|
kai_wren.teacher # => 'Viss'
|
93
110
|
|
94
|
-
=== Logging
|
111
|
+
=== Usage: Logging
|
95
112
|
|
96
113
|
The Wrest logger can be set and accessed through Wrest.logger and is configured by default to log to STDOUT. If you're using Wrest in a Rails application, you can configure logging by placing the following line in your environment file:
|
97
114
|
Wrest.logger = ActiveRecord::Base.logger
|
@@ -106,7 +123,8 @@ Wrest RDocs can be found at http://wrest.rubyforge.org
|
|
106
123
|
|
107
124
|
== Wrest::Resource
|
108
125
|
|
109
|
-
Wrest::Resource is an alternative to ActiveResource. It targets Rails REST (well, POX
|
126
|
+
Wrest::Resource is an alternative to Rails' ActiveResource. It targets Rails REST (well, POX, since Rails isn't really RESTful) services and is currently under development. Since no single REST library can provide an object oriented wrapper suitable for _all_ available web services, it follows that Wrest should focus on providing you with the tools to help you roll your own. Wrest::Resource is an example of this - an object oriented wrapper for the kind of REST APIs exposed by Rails applications, that you would otherwise use ActiveResource to consume.
|
127
|
+
|
110
128
|
|
111
129
|
* No more pretending that REST resources are the same as records in a database (yeah, no more freaking ActiveResource::Connection)
|
112
130
|
* Treat put as 'create or update,' not just 'update'
|
@@ -123,6 +141,7 @@ Wrest::Resource is an alternative to ActiveResource. It targets Rails REST (well
|
|
123
141
|
* More natural mapping of deserialised entities to existing classes
|
124
142
|
* No communication via exceptions for http error status codes
|
125
143
|
* Better extensibility - allows access to request/response objects, avoids class variables, favours symbols over strings etc.
|
144
|
+
* Content Types in request headers
|
126
145
|
* Consider support for OPTIONS and response codes 100/417
|
127
146
|
|
128
147
|
== Dependencies
|
data/Rakefile
CHANGED
@@ -7,13 +7,24 @@
|
|
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
|
-
require 'rubygems'
|
11
|
-
gem 'rspec'
|
12
|
-
require 'rake'
|
13
|
-
require 'spec'
|
14
|
-
require 'spec/rake/spectask'
|
15
|
-
|
16
10
|
puts "Building on Ruby #{RUBY_VERSION}, #{RUBY_RELEASE_DATE}, #{RUBY_PLATFORM}"
|
11
|
+
puts "Note that some optional libraries/gems that the build (not Wrest itself) uses may not be available on all implementations of Ruby."
|
12
|
+
|
13
|
+
if Object.const_defined?('RAILS_ROOT')
|
14
|
+
require File.dirname(__FILE__) + '/../../../config/environment'
|
15
|
+
else
|
16
|
+
require 'rubygems'
|
17
|
+
gem 'rspec'
|
18
|
+
require 'rake'
|
19
|
+
require 'spec'
|
20
|
+
require 'spec/rake/spectask'
|
21
|
+
|
22
|
+
begin
|
23
|
+
require 'metric_fu'
|
24
|
+
rescue LoadError
|
25
|
+
puts 'metric_fu is not available. Install it with: gem install jscruggs-metric_fu -s http://gems.github.com'
|
26
|
+
end
|
27
|
+
end
|
17
28
|
|
18
29
|
desc 'Default: run spec tests.'
|
19
30
|
task :default => :spec
|
@@ -27,9 +38,9 @@ end
|
|
27
38
|
begin
|
28
39
|
require 'hanna/rdoctask'
|
29
40
|
rescue LoadError
|
30
|
-
puts 'Hanna not available, using standard Rake rdoctask.
|
41
|
+
puts 'Hanna not available, using standard Rake rdoctask. Install it with: gem install mislav-hanna.'
|
31
42
|
require 'rake/rdoctask'
|
32
|
-
end
|
43
|
+
end
|
33
44
|
desc 'Generate documentation for Wrest'
|
34
45
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
35
46
|
rdoc.rdoc_dir = 'rdoc'
|
@@ -74,15 +85,16 @@ begin
|
|
74
85
|
gemspec.add_dependency('activesupport', '>= 2.3.2')
|
75
86
|
case RUBY_PLATFORM
|
76
87
|
when /java/
|
77
|
-
gemspec.add_dependency('json-jruby', '>= 1.1.3')
|
88
|
+
gemspec.add_dependency('json-jruby', '>= 1.1.3')
|
78
89
|
gemspec.platform = 'java'
|
79
90
|
else
|
80
|
-
gemspec.add_dependency('json', '>= 1.1.3')
|
91
|
+
gemspec.add_dependency('json', '>= 1.1.3')
|
81
92
|
gemspec.platform = Gem::Platform::RUBY
|
82
93
|
end
|
83
94
|
end
|
84
95
|
rescue LoadError
|
85
|
-
puts "Jeweler not available. Install it with:
|
96
|
+
puts "Jeweler not available. Install it with: gem install technicalpickles-jeweler -s http://gems.github.com"
|
97
|
+
puts "If you're using JRuby, install it with: jruby -S gem install technicalpickles-jeweler -s http://gems.github.com --version '=0.11.0'"
|
86
98
|
end
|
87
99
|
|
88
100
|
begin
|
data/VERSION.yml
CHANGED
@@ -0,0 +1,58 @@
|
|
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
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/wrest")
|
11
|
+
require 'pp'
|
12
|
+
|
13
|
+
Wrest.logger = Logger.new(STDOUT)
|
14
|
+
Wrest.logger.level = Logger::DEBUG # Set this to Logger::INFO or higher to disable request logging
|
15
|
+
|
16
|
+
# API reference: http://delicious.com/help/api
|
17
|
+
class Delicious
|
18
|
+
def initialize(options)
|
19
|
+
@uri = "https://api.del.icio.us/v1/posts".to_uri(options)
|
20
|
+
end
|
21
|
+
|
22
|
+
def bookmarks(parameters = {})
|
23
|
+
@uri['/get'].get(parameters)
|
24
|
+
end
|
25
|
+
|
26
|
+
def recent(parameters = {})
|
27
|
+
@uri['/recent'].get(parameters)
|
28
|
+
end
|
29
|
+
|
30
|
+
def bookmark(parameters)
|
31
|
+
@uri['/add'].post('', {}, parameters)
|
32
|
+
end
|
33
|
+
|
34
|
+
def delete(parameters)
|
35
|
+
@uri['/delete'].delete(parameters)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
account = Delicious.new :username => 'kaiwren', :password => 'fupupp1es'
|
40
|
+
|
41
|
+
pp account.bookmark(
|
42
|
+
:url => 'http://blog.sidu.in/search/label/ruby',
|
43
|
+
:description => 'The Ruby related posts on my blog!',
|
44
|
+
:extended => "All posts tagged with 'ruby'",
|
45
|
+
:tags => 'ruby hacking'
|
46
|
+
).deserialise
|
47
|
+
|
48
|
+
puts '----------'
|
49
|
+
|
50
|
+
pp account.bookmarks(:tag => 'rails', :dt => '20090712').deserialise
|
51
|
+
|
52
|
+
puts '----------'
|
53
|
+
|
54
|
+
pp recently_saved_uris = account.recent(:tag => 'ruby').deserialise["posts"]["post"].collect{|bookmark| bookmark['href']}
|
55
|
+
|
56
|
+
puts '----------'
|
57
|
+
|
58
|
+
pp account.delete(:url => 'http://blog.sidu.in/search/label/ruby').deserialise
|
@@ -0,0 +1,57 @@
|
|
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
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/wrest")
|
11
|
+
|
12
|
+
Wrest.logger = Logger.new(STDOUT)
|
13
|
+
Wrest.logger.level = Logger::DEBUG # Set this to Logger::INFO or higher to disable request logging
|
14
|
+
|
15
|
+
include Wrest
|
16
|
+
|
17
|
+
class Realm
|
18
|
+
include Components::AttributesContainer
|
19
|
+
include Components::AttributesContainer::Typecaster
|
20
|
+
|
21
|
+
typecast :t => lambda{|type|
|
22
|
+
case type
|
23
|
+
when '1' then 'Normal'
|
24
|
+
when '2' then 'PvP'
|
25
|
+
when '3' then 'RP'
|
26
|
+
when '4' then 'RP PvP'
|
27
|
+
end
|
28
|
+
},
|
29
|
+
:s => lambda{|status|
|
30
|
+
case status
|
31
|
+
when '1' then 'Available'
|
32
|
+
else 'Unavailable'
|
33
|
+
end
|
34
|
+
},
|
35
|
+
:l => lambda{|load|
|
36
|
+
case load
|
37
|
+
when '1' then 'Low'
|
38
|
+
when '2' then 'Normal'
|
39
|
+
when '3' then 'High'
|
40
|
+
when '4' then 'Max'
|
41
|
+
end
|
42
|
+
}
|
43
|
+
|
44
|
+
def available?
|
45
|
+
self.s == 'Available'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
realms = "http://www.worldofwarcraft.com/realmstatus/status.xml".to_uri.get.deserialise['page']['rs']['r'].collect{|data| Realm.new(data)}
|
50
|
+
|
51
|
+
puts "Status of Nagrand: #{realms.find{|realm| realm.n == 'Nagrand'}.s}"
|
52
|
+
puts
|
53
|
+
puts "All Available Realms:"
|
54
|
+
puts
|
55
|
+
puts "Realm\tLoad\tType"
|
56
|
+
puts "-----------"
|
57
|
+
realms.select(&:available?).each{|realm| puts "#{realm.n}\t#{realm.l}\t#{realm.t}" }
|
@@ -100,8 +100,6 @@ module Wrest::Components
|
|
100
100
|
# own class.
|
101
101
|
def initialize(attributes = {})
|
102
102
|
@attributes = attributes.symbolize_keys
|
103
|
-
@interface = Module.new
|
104
|
-
self.extend @interface
|
105
103
|
end
|
106
104
|
|
107
105
|
def [](key)
|
@@ -124,11 +122,11 @@ module Wrest::Components
|
|
124
122
|
if @attributes.include?(attribute_name.to_sym) || method_name.last == '='
|
125
123
|
case method_name.last
|
126
124
|
when '='
|
127
|
-
|
125
|
+
self.instance_eval AttributesContainer.build_attribute_setter(attribute_name)
|
128
126
|
when '?'
|
129
|
-
|
127
|
+
self.instance_eval AttributesContainer.build_attribute_queryer(attribute_name)
|
130
128
|
else
|
131
|
-
|
129
|
+
self.instance_eval AttributesContainer.build_attribute_getter(attribute_name)
|
132
130
|
end
|
133
131
|
send(method_sym, *arguments)
|
134
132
|
else
|
@@ -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/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"
|