wrest 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -7,11 +7,15 @@ Features under a numbered section are complete and available in the Wrest gem.
7
7
  == Coming soon
8
8
  * Cookie support
9
9
  * Proxy support
10
+ * Multi-part post support on curl
10
11
 
11
12
  == In progress
12
13
  * 304/ETag response caching
13
14
  * Keep-alive support for libcurl
14
15
 
16
+ == Current
17
+ * Multipart post and put using Net::Http
18
+
15
19
  == 0.1.0
16
20
  * Added Nokogiri as a fallback for LibXML Ruby before we give up and use REXML. (Nokogiri is available on JRuby, unlike LibXML-ruby)
17
21
  * Added code to attempt to load JREXML (for what it's worth) when using REXML on JRuby
data/README.rdoc CHANGED
@@ -1,13 +1,13 @@
1
- = Wrest 0.1.0
1
+ = Wrest 0.1.1
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
6
6
  * allows allows you to quickly build object oriented wrappers around any web service
7
7
  * is spec driven, strongly favours immutable objects and avoids class methods and setters making it better suited for use as a library, especially in multi-threaded environments
8
- * runs on Ruby 1.8, 1.9 and JRuby, with MacRuy and IronRuby support on the way
8
+ * runs on Ruby 1.8, 1.9 and JRuby, with MacRuby and IronRuby support on the way
9
9
 
10
- To receive notifications whenever new features are added to Wrest, please subscribe to my {twitter feed}[http://twitter.com/ponnappa].
10
+ To receive notifications whenever new features are added to Wrest, pleasesubscribe to my {twitter feed}[http://twitter.com/ponnappa].
11
11
 
12
12
  (If you were wondering why the words 'demon', 'chi' and 'fu-puppies' show up in nearly every example and spec, it's because they're all references to {Roger Zelazny's}[http://en.wikipedia.org/wiki/Roger_Zelazny] last book, 'Lord Demon.')
13
13
 
@@ -44,12 +44,12 @@ You can launch the interactive Wrest shell by running bin/wrest if you have the
44
44
  require 'rubygems'
45
45
  require 'wrest'
46
46
  y "http://search.yahooapis.com/NewsSearchService/V1/newsSearch".to_uri.get(
47
- :appid => 'YahooDemo',
48
- :output => 'xml',
49
- :query => 'India',
50
- :results=> '3',
51
- :start => '1'
52
- )
47
+ :appid => 'YahooDemo',
48
+ :output => 'xml',
49
+ :query => 'India',
50
+ :results=> '3',
51
+ :start => '1'
52
+ )
53
53
  === Usage: Basic Http Calls
54
54
 
55
55
  ==== GET
@@ -64,16 +64,16 @@ A couple of ways to get Yahoo news as a hash map.
64
64
  require 'wrest'
65
65
  include Wrest::Components
66
66
  y "http://search.yahooapis.com/NewsSearchService/V1/newsSearch".to_uri.get(
67
- :appid => 'YahooDemo',
68
- :output => 'xml',
69
- :query => 'India',
70
- :results=> '3',
71
- :start => '1'
72
- ).deserialise_using(
73
- Translators::Xml
74
- ).mutate_using(
75
- Mutators::XmlMiniTypeCaster.new
76
- )
67
+ :appid => 'YahooDemo',
68
+ :output => 'xml',
69
+ :query => 'India',
70
+ :results=> '3',
71
+ :start => '1'
72
+ ).deserialise_using(
73
+ Translators::Xml
74
+ ).mutate_using(
75
+ Mutators::XmlMiniTypeCaster.new
76
+ )
77
77
 
78
78
  ==== DELETE
79
79
 
@@ -101,7 +101,7 @@ Allows any class to hold an attributes hash, somewhat like ActiveResource. It al
101
101
  Example:
102
102
 
103
103
  class Demon
104
- include Wrest::Components::AttributesContainer
104
+ include Wrest::Components::Container
105
105
 
106
106
  always_has :id
107
107
  typecast :age => as_integer,
@@ -119,7 +119,7 @@ Example:
119
119
 
120
120
  === Usage: Logging
121
121
 
122
- 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:
122
+ 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 adding a config/initializers/wrest.rb file with the following contents :
123
123
  Wrest.logger = ActiveRecord::Base.logger
124
124
 
125
125
  === Build
@@ -163,6 +163,7 @@ If you're looking for help doing this on Rails on the server side, take a look a
163
163
  * json (json-jruby on JRuby)
164
164
  * active_support
165
165
  * ruby-libxml or Nokogiri (Wrest tries ruby-libxml first, then nokogiri, and will finally fall back on REXML; be aware that Wrest uses ActiveSupport::XmlMini, so if you're using Wrest as a plugin in a Rails application, all xml parsing across the application will switch to using libxml if it's available. You're free to change this by hand by using the ActiveSupport::XmlMini.backend= method.)
166
+ * multipart-post (needed only if multipart support is enabled)
166
167
 
167
168
  === Build
168
169
 
@@ -176,4 +177,5 @@ Features that are planned, in progress or already implemented are documented in
176
177
 
177
178
  == Licence
178
179
 
179
- Wrest is released under the Apache 2.0 licence
180
+ Wrest is released under the Apache 2.0 licence
181
+
data/Rakefile CHANGED
@@ -36,7 +36,7 @@ namespace :spec do
36
36
  task.spec_opts = ['--options', 'spec/spec.opts']
37
37
  end
38
38
 
39
- desc "Run unit all specs"
39
+ desc "Run all live functional specs"
40
40
  Spec::Rake::SpecTask.new(:functional) do |task|
41
41
  task.spec_files = FileList['spec/functional/wrest/**/*_spec.rb']
42
42
  task.spec_opts = ['--options', 'spec/spec.opts']
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 0
2
+ :patch: 1
3
3
  :minor: 1
4
4
  :major: 0
@@ -0,0 +1,60 @@
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
+ require 'openssl'
13
+ require 'time'
14
+ require "base64"
15
+
16
+ Wrest.logger = Logger.new(STDOUT)
17
+ Wrest.logger.level = Logger::DEBUG # Set this to Logger::INFO or higher to disable request logging
18
+
19
+ # AWS SDB API Reference
20
+ # http://docs.amazonwebservices.com/AmazonSimpleDB/latest/DeveloperGuide/index.html?SDB_API.html
21
+
22
+ module SimpleDB
23
+ Config = {
24
+ :aws_access_key_id => 'AKIAJOTOWFTQ6FRQIZZA',
25
+ :aws_secret_access_key => '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'
26
+ }
27
+ Uri = "https://sdb.amazonaws.com".to_uri
28
+ Digest = OpenSSL::Digest::Digest.new('sha1')
29
+
30
+ extend self
31
+ def invoke(action)
32
+ options = {
33
+ 'Action' => action,
34
+ 'AWSAccessKeyId' => SimpleDB::Config[:aws_access_key_id],
35
+ 'DomainName' => 'LEDev',
36
+ 'SignatureVersion' => '2',
37
+ 'SignatureMethod' => 'HmacSHA256',
38
+ 'Timestamp' => Time.now.iso8601,
39
+ 'Version' => '2009-04-15'
40
+ }
41
+ Uri.get(options.merge('Signature' => signature_for(options)))
42
+ end
43
+
44
+ # http://docs.amazonwebservices.com/AmazonSimpleDB/latest/DeveloperGuide/index.html?REST_RESTAuth.html
45
+ def signature_for(options)
46
+ data = [
47
+ "GET",
48
+ "sdb.amazonaws.com",
49
+ options.keys.sort.map{|k| "#{k}=#{CGI::escape(options[k])}"}.join('&')
50
+ ].join("\n")
51
+ CGI::escape(Base64.encode64(OpenSSL::HMAC.digest(Digest, Config[:aws_secret_access_key], data)))
52
+ end
53
+
54
+ def list_domains
55
+ invoke('ListDomains').body
56
+ end
57
+ end
58
+
59
+
60
+ pp SimpleDB.list_domains
data/lib/wrest.rb CHANGED
@@ -20,8 +20,11 @@ require 'benchmark'
20
20
  require 'active_support'
21
21
 
22
22
 
23
- module Wrest #:nodoc:
23
+ module Wrest
24
24
  Root = File.dirname(__FILE__)
25
+
26
+ $:.unshift Root
27
+
25
28
  def self.logger=(logger)
26
29
  @logger = logger
27
30
  end
@@ -30,10 +33,12 @@ module Wrest #:nodoc:
30
33
  @logger
31
34
  end
32
35
 
36
+ # Switch Wrest to using Net::HTTP.
33
37
  def self.use_native
34
38
  silence_warnings{ Wrest.const_set('Http', Wrest::Native) }
35
39
  end
36
40
 
41
+ # Switch Wrest to using libcurl.
37
42
  def self.use_curl
38
43
  require "#{Wrest::Root}/wrest/curl"
39
44
  silence_warnings{ Wrest.const_set('Http', Wrest::Curl) }
@@ -68,13 +73,13 @@ end
68
73
  Dir["#{File.expand_path(File.dirname(__FILE__))}/wrest/core_ext/*.rb"].each { |file| require file }
69
74
 
70
75
  # Load Wrest Core
76
+ require "#{Wrest::Root}/wrest/version"
71
77
  require "#{Wrest::Root}/wrest/http_shared"
72
78
  require "#{Wrest::Root}/wrest/native"
73
79
 
74
80
  # Load Wrest Wrappers
75
81
  require "#{Wrest::Root}/wrest/uri"
76
82
  require "#{Wrest::Root}/wrest/uri_template"
77
- require "#{Wrest::Root}/wrest/version"
78
83
  require "#{Wrest::Root}/wrest/exceptions"
79
84
  require "#{Wrest::Root}/wrest/components"
80
85
 
data/lib/wrest/curl.rb CHANGED
@@ -15,8 +15,7 @@ rescue Gem::LoadError => e
15
15
  end
16
16
  require 'patron'
17
17
 
18
- module Wrest #:nodoc:
19
-
18
+ module Wrest
20
19
  # Contains all HTTP protocol related classes such as
21
20
  # Get, Post, Request, Response etc. and uses Curl for
22
21
  # better performance, but works only on CRuby and only on a *nix OS.
@@ -33,7 +32,8 @@ module Wrest #:nodoc:
33
32
  end
34
33
  end
35
34
 
36
- module Patron
35
+ module Patron #:nodoc:
36
+ # Patching Patron::Session#handle_request to make it public.
37
37
  class Session
38
38
  public :handle_request
39
39
  end
@@ -7,89 +7,91 @@
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
- module Wrest::Curl
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.
14
- class Request
15
- attr_reader :http_request, :uri, :body, :headers, :username, :password, :follow_redirects,
16
- :follow_redirects_limit, :timeout, :connection, :parameters, :auth_type
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
- # :timeout => The period, in seconds, after which a Timeout::Error is raised
27
- # in the event of a connection failing to open. Defaulted to 60 by Uri#create_connection.
28
- # :connection => The HTTP Connection object to use. This is how a keep-alive connection can be
29
- # used for multiple requests. Not yet fully implemented for Curl.
30
- #
31
- # Curl specific options:
32
- # :auth_type => This is a curl specific option and can be one of :basic, :digest, or :any. The default is :basic.
33
- def initialize(wrest_uri, http_verb, parameters = {}, body = nil, headers = {}, options = {})
34
- @uri = wrest_uri
35
- @headers = headers.stringify_keys
36
- @parameters = parameters
37
- @body = body
38
-
39
- @options = options.clone
40
- @auth_type = @options[:auth_type] || :basic
41
- @username = @options[:username]
42
- @password = @options[:password]
43
- @follow_redirects = (@options[:follow_redirects] ||= false)
10
+ module Wrest
11
+ module Curl
12
+ # This represents a HTTP request. Typically you will never need to instantiate
13
+ # one of these yourself - you can use one of the more conveient APIs via Wrest::Uri
14
+ # or Wrest::Curl::Get etc. instead.
15
+ class Request
16
+ attr_reader :http_request, :uri, :body, :headers, :username, :password, :follow_redirects,
17
+ :follow_redirects_limit, :timeout, :connection, :parameters, :auth_type
18
+ # Valid tuples for the options are:
19
+ # :username => String, defaults to nil
20
+ # :password => String, defaults to nil
21
+ # :follow_redirects => Boolean, defaults to true for Get, false for anything else
22
+ # :follow_redirects_limit => Integer, defaults to 5. This is the number of redirects
23
+ # that Wrest will automatically follow before raising an
24
+ # Wrest::Exceptions::AutoRedirectLimitExceeded exception.
25
+ # For example, if you set this to 1, the very first redirect
26
+ # will raise the exception.
27
+ # :timeout => The period, in seconds, after which a Timeout::Error is raised
28
+ # in the event of a connection failing to open. Defaulted to 60 by Uri#create_connection.
29
+ # :connection => The HTTP Connection object to use. This is how a keep-alive connection can be
30
+ # used for multiple requests. Not yet fully implemented for Curl.
31
+ #
32
+ # Curl specific options:
33
+ # :auth_type => This is a curl specific option and can be one of :basic, :digest, or :any. The default is :basic.
34
+ def initialize(wrest_uri, http_verb, parameters = {}, body = nil, headers = {}, options = {})
35
+ @uri = wrest_uri
36
+ @headers = headers.stringify_keys
37
+ @parameters = parameters
38
+ @body = body
44
39
 
45
- @follow_redirects_limit = (@options[:follow_redirects_limit] ||= 5)
46
- @timeout = @options[:timeout] || 60
47
- @connection = @options[:connection]
48
-
49
- @http_request = Patron::Request.new
50
- @http_request.action = http_verb
51
- @http_request.upload_data = body
52
- @http_request.headers = headers
53
- @http_request.username = username
54
- @http_request.password = password
55
- @http_request.auth_type = auth_type
56
- @http_request.url = parameters.empty? ? uri.to_s : "#{uri.to_s}?#{parameters.to_query}"
57
- @http_request.max_redirects = follow_redirects_limit if follow_redirects
58
- @http_request.timeout = @timeout
59
- end
40
+ @options = options.clone
41
+ @auth_type = @options[:auth_type] || :basic
42
+ @username = @options[:username]
43
+ @password = @options[:password]
44
+ @follow_redirects = (@options[:follow_redirects] ||= false)
45
+
46
+ @follow_redirects_limit = (@options[:follow_redirects_limit] ||= 5)
47
+ @timeout = @options[:timeout] || 60
48
+ @connection = @options[:connection]
49
+
50
+ @http_request = Patron::Request.new
51
+ @http_request.action = http_verb
52
+ @http_request.upload_data = body
53
+ @http_request.headers = headers
54
+ @http_request.username = username
55
+ @http_request.password = password
56
+ @http_request.auth_type = auth_type
57
+ @http_request.url = parameters.empty? ? uri.to_s : "#{uri.to_s}?#{parameters.to_query}"
58
+ @http_request.max_redirects = follow_redirects_limit if follow_redirects
59
+ @http_request.timeout = @timeout
60
+ end
61
+
62
+ # Makes a request and returns a Wrest::Http::Response.
63
+ # Data about the request is and logged to Wrest.logger
64
+ # The log entry contains the following information:
65
+ #
66
+ # --> indicates a request
67
+ # <-- indicates a response
68
+ #
69
+ # The type of request is mentioned in caps, followed by a hash
70
+ # uniquely uniquely identifying a particular request/response pair.
71
+ # In a multi-process or multi-threaded scenario, this can be used
72
+ # to identify request-response pairs.
73
+ #
74
+ # The request hash is followed by a connection hash; requests using the
75
+ # same connection (effectively a keep-alive connection) will have the
76
+ # same connection hash.
77
+ #
78
+ # This is followed by the response code, the payload size and the time taken.
79
+ def invoke
80
+ response = nil
60
81
 
61
- # Makes a request and returns a Wrest::Http::Response.
62
- # Data about the request is and logged to Wrest.logger
63
- # The log entry contains the following information:
64
- #
65
- # --> indicates a request
66
- # <-- indicates a response
67
- #
68
- # The type of request is mentioned in caps, followed by a hash
69
- # uniquely uniquely identifying a particular request/response pair.
70
- # In a multi-process or multi-threaded scenario, this can be used
71
- # to identify request-response pairs.
72
- #
73
- # The request hash is followed by a connection hash; requests using the
74
- # same connection (effectively a keep-alive connection) will have the
75
- # same connection hash.
76
- #
77
- # This is followed by the response code, the payload size and the time taken.
78
- def invoke
79
- response = nil
82
+ @connection ||= Patron::Session.new
83
+ raise ArgumentError, "Empty URL" if http_request.url.empty?
80
84
 
81
- @connection ||= Patron::Session.new
82
- raise ArgumentError, "Empty URL" if http_request.url.empty?
85
+ prefix = "#{http_request.action.to_s.upcase} #{http_request.hash} #{connection.hash}"
83
86
 
84
- prefix = "#{http_request.action.to_s.upcase} #{http_request.hash} #{connection.hash}"
87
+ Wrest.logger.debug "--> (#{prefix}) #{http_request.url}"
88
+ time = Benchmark.realtime { response = Wrest::Curl::Response.new(connection.handle_request(http_request))}
89
+ Wrest.logger.debug "<-- (#{prefix}) %s (%d bytes %.2fs)" % [response.message, response.body ? response.body.length : 0, time]
85
90
 
86
- Wrest.logger.debug "--> (#{prefix}) #{http_request.url}"
87
- time = Benchmark.realtime { response = Wrest::Curl::Response.new(connection.handle_request(http_request))}
88
- Wrest.logger.debug "<-- (#{prefix}) %s (%d bytes %.2fs)" % [response.message, response.body ? response.body.length : 0, time]
89
-
90
- response
91
+ response
91
92
  rescue Patron::TimeoutError => e
92
93
  raise Wrest::Exceptions::Timeout.new(e)
94
+ end
93
95
  end
94
96
  end
95
97
  end
@@ -7,9 +7,8 @@
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
- module Wrest #:nodoc:
11
-
12
- # Contains functionality the is independent of
10
+ module Wrest
11
+ # Contains functionality that is independent of
13
12
  # the underlying HTTP libs
14
13
  module HttpShared
15
14
  end
@@ -7,8 +7,8 @@
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
- module Wrest #:nodoc:
11
- module HttpShared #:nodoc:
10
+ module Wrest
11
+ module HttpShared
12
12
  module StandardHeaders
13
13
  Connection = 'connection'
14
14
  ContentType = 'content-type'
@@ -7,8 +7,8 @@
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
- module Wrest #:nodoc:
11
- module HttpShared #:nodoc:
10
+ module Wrest
11
+ module HttpShared
12
12
  module StandardTokens
13
13
  Close = 'close'
14
14
  KeepAlive = 'keep-alive'
@@ -0,0 +1,52 @@
1
+ # Copyright 2009 - 2010 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
+ begin
11
+ gem 'multipart-post', '>= 1.0'
12
+ rescue Gem::LoadError
13
+ Wrest.logger.debug "Multipart Post >= 1.0 not found. Multipart Post is necessary to be able to post multipart. To install Multipart Post run `sudo gem install multipart-post`"
14
+ raise e
15
+ end
16
+
17
+ require "wrest/native/post_multipart"
18
+ require "wrest/native/put_multipart"
19
+
20
+ module Wrest
21
+ # To enable Multipart support, use
22
+ # require 'wrest/multipart'
23
+ #
24
+ # Multipart support is currently only available on Net::Http and not when using libcurl.
25
+ # It depends on the multipart-post gem being available. To install multipart-post
26
+ # (sudo) gem install multipart-post
27
+ #
28
+ # The methods in this module are mixed into Wrest::Uri.
29
+ module Multipart
30
+ # Makes a multipart/form-data encoded POST request to this URI. This is a convenience API
31
+ # that mimics a multipart form being posted; some allegly RESTful APIs like FCBK require
32
+ # this for file uploads.
33
+ #
34
+ # File.open('/path/to/image.jpg') do |file|
35
+ # 'http://localhost:3000/uploads'.to_uri.post_multipart('file' => UploadIO.new(file, "image/jpg", '/path/to/image.jpg'))
36
+ # end
37
+ def post_multipart(parameters = {}, headers = {})
38
+ Http::PostMultipart.new(self, parameters, headers, @options).invoke
39
+ end
40
+
41
+ # Makes a multipart/form-data encoded PUT request to this URI. This is a convenience API
42
+ # that mimics a multipart form being put. I sincerely hope you never need to use this.
43
+ def put_multipart(parameters = {}, headers = {})
44
+ Http::PutMultipart.new(self, parameters, headers, @options).invoke
45
+ end
46
+ end
47
+
48
+ class Uri
49
+ include Multipart
50
+ end
51
+ end
52
+
data/lib/wrest/native.rb CHANGED
@@ -7,11 +7,12 @@
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
- module Wrest #:nodoc:
11
-
10
+ module Wrest
12
11
  # Contains all native protocol related classes such as
13
12
  # Get, Post, Request, Response etc. and uses the native
14
- # Ruby Net::native libraries.
13
+ # Ruby Net::HTTP libraries.
14
+ #
15
+ # Wrest::Http points to this module by default.
15
16
  module Native
16
17
  include Wrest::HttpShared
17
18
  end
@@ -0,0 +1,32 @@
1
+ # Copyright 2009 - 2010 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
+ require 'net/http/post/multipart'
10
+
11
+ module Wrest::Native
12
+ class PostMultipart < Request
13
+ def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
14
+ super(
15
+ wrest_uri,
16
+ Net::HTTP::Post::Multipart,
17
+ parameters,
18
+ nil,
19
+ headers,
20
+ options
21
+ )
22
+ end
23
+
24
+ def build_request(request_klass, uri, parameters, headers)
25
+ request_klass.new(uri.full_path, parameters, headers)
26
+ end
27
+
28
+ def do_request
29
+ @connection.request(@http_request)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ # Copyright 2009 - 2010 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
+ require 'net/http/post/multipart'
10
+
11
+ module Wrest::Native
12
+ class PutMultipart < Request
13
+ def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
14
+ super(
15
+ wrest_uri,
16
+ Net::HTTP::Put::Multipart,
17
+ parameters,
18
+ nil,
19
+ headers,
20
+ options
21
+ )
22
+ end
23
+
24
+ def build_request(request_klass, uri, parameters, headers)
25
+ request_klass.new(uri.full_path, parameters, headers)
26
+ end
27
+
28
+ def do_request
29
+ @connection.request(@http_request)
30
+ end
31
+ end
32
+ end
@@ -10,32 +10,31 @@
10
10
  module Wrest::Native
11
11
  # This represents a HTTP request. Typically you will never need to instantiate
12
12
  # one of these yourself - you can use one of the more conveient APIs via Wrest::Uri
13
- # or Wrest::Http::Get etc. instead.
13
+ # or Wrest::Native::Get etc. instead.
14
14
  class Request
15
15
  attr_reader :http_request, :uri, :body, :headers, :username, :password, :follow_redirects,
16
16
  :follow_redirects_limit, :follow_redirects_count, :timeout, :connection, :parameters
17
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. Defaulted to 60 by Uri#create_connection.
32
- # :connection => The HTTP Connection object to use. This is how a keep-alive connection can be
33
- # used for multiple requests.
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. Defaulted to 60 by Uri#create_connection.
32
+ # :connection => The HTTP Connection object to use. This is how a keep-alive connection can be
33
+ # used for multiple requests.
34
34
  def initialize(wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, options = {})
35
35
  @uri = wrest_uri
36
36
  @headers = headers.stringify_keys
37
37
  @parameters = parameters
38
- @http_request = http_request_klass.new(parameters.empty? ? wrest_uri.full_path : "#{wrest_uri.full_path}?#{parameters.to_query}", @headers)
39
38
  @body = body
40
39
  @options = options.clone
41
40
  @username = @options[:username]
@@ -45,14 +44,15 @@ module Wrest::Native
45
44
  @follow_redirects_limit = (@options[:follow_redirects_limit] ||= 5)
46
45
  @timeout = @options[:timeout]
47
46
  @connection = @options[:connection]
47
+ @http_request = self.build_request(http_request_klass, @uri, @parameters, @headers)
48
48
  end
49
49
 
50
50
  # Makes a request and returns a Wrest::Native::Response.
51
51
  # Data about the request is and logged to Wrest.logger
52
52
  # The log entry contains the following information:
53
53
  #
54
- # --> indicates a request
55
- # <-- indicates a response
54
+ # --> indicates a request
55
+ # <-- indicates a response
56
56
  #
57
57
  # The type of request is mentioned in caps, followed by a hash
58
58
  # uniquely uniquely identifying a particular request/response pair.
@@ -73,12 +73,20 @@ module Wrest::Native
73
73
  prefix = "#{http_request.method} #{http_request.hash} #{@connection.hash}"
74
74
 
75
75
  Wrest.logger.debug "--> (#{prefix}) #{@uri.protocol}://#{@uri.host}:#{@uri.port}#{@http_request.path}"
76
- time = Benchmark.realtime { response = Wrest::Native::Response.new( @connection.request(@http_request, @body) ) }
76
+ time = Benchmark.realtime { response = Wrest::Native::Response.new( do_request ) }
77
77
  Wrest.logger.debug "<-- (#{prefix}) %d %s (%d bytes %.2fs)" % [response.code, response.message, response.body ? response.body.length : 0, time]
78
78
 
79
79
  @follow_redirects ? response.follow(@options) : response
80
80
  rescue Timeout::Error => e
81
81
  raise Wrest::Exceptions::Timeout.new(e)
82
82
  end
83
+
84
+ def build_request(request_klass, uri, parameters, headers)
85
+ request_klass.new(parameters.empty? ? uri.full_path : "#{uri.full_path}?#{parameters.to_query}", headers)
86
+ end
87
+
88
+ def do_request
89
+ @connection.request(@http_request, @body)
90
+ end
83
91
  end
84
92
  end
data/lib/wrest/uri.rb CHANGED
@@ -104,8 +104,8 @@ module Wrest #:nodoc:
104
104
  end
105
105
 
106
106
  # Makes a POST request to this URI. This is a convenience API
107
- # that mimics a form being posted; some allegly RESTful APIs like FCBK
108
- # require this to be used.
107
+ # that mimics a form being posted; some allegly RESTful APIs like FCBK require
108
+ # this.
109
109
  #
110
110
  # Form encoding involves munging the parameters into a string and placing them
111
111
  # in the body, as well as setting the Content-Type header to
data/lib/wrest/version.rb CHANGED
@@ -12,7 +12,7 @@ module Wrest
12
12
  unless defined? MAJOR
13
13
  MAJOR = 0
14
14
  MINOR = 1
15
- TINY = 0
15
+ TINY = 1
16
16
 
17
17
  STRING = [MAJOR, MINOR, TINY].join('.')
18
18
 
data/wrest.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wrest}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Sidu Ponnappa"]
12
- s.date = %q{2009-12-21}
12
+ s.date = %q{2010-04-22}
13
13
  s.description = %q{Wrest is a HTTP and REST client library which allows you to quickly build well encapsulated, object oriented wrappers around any web service.}
14
14
  s.email = %q{ckponnappa@gmail.com}
15
15
  s.executables = ["wrest", "jwrest"]
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "bin/jwrest",
25
25
  "bin/wrest",
26
26
  "bin/wrest_shell.rb",
27
+ "examples/aws_simpledb.rb",
27
28
  "examples/delicious.rb",
28
29
  "examples/facebook.rb",
29
30
  "examples/keep_alive.rb",
@@ -64,13 +65,16 @@ Gem::Specification.new do |s|
64
65
  "lib/wrest/http_shared/headers.rb",
65
66
  "lib/wrest/http_shared/standard_headers.rb",
66
67
  "lib/wrest/http_shared/standard_tokens.rb",
68
+ "lib/wrest/multipart.rb",
67
69
  "lib/wrest/native.rb",
68
70
  "lib/wrest/native/connection_factory.rb",
69
71
  "lib/wrest/native/delete.rb",
70
72
  "lib/wrest/native/get.rb",
71
73
  "lib/wrest/native/options.rb",
72
74
  "lib/wrest/native/post.rb",
75
+ "lib/wrest/native/post_multipart.rb",
73
76
  "lib/wrest/native/put.rb",
77
+ "lib/wrest/native/put_multipart.rb",
74
78
  "lib/wrest/native/redirection.rb",
75
79
  "lib/wrest/native/request.rb",
76
80
  "lib/wrest/native/response.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sidu Ponnappa
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-21 00:00:00 +05:30
12
+ date: 2010-04-22 00:00:00 +05:30
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -49,6 +49,7 @@ files:
49
49
  - bin/jwrest
50
50
  - bin/wrest
51
51
  - bin/wrest_shell.rb
52
+ - examples/aws_simpledb.rb
52
53
  - examples/delicious.rb
53
54
  - examples/facebook.rb
54
55
  - examples/keep_alive.rb
@@ -89,13 +90,16 @@ files:
89
90
  - lib/wrest/http_shared/headers.rb
90
91
  - lib/wrest/http_shared/standard_headers.rb
91
92
  - lib/wrest/http_shared/standard_tokens.rb
93
+ - lib/wrest/multipart.rb
92
94
  - lib/wrest/native.rb
93
95
  - lib/wrest/native/connection_factory.rb
94
96
  - lib/wrest/native/delete.rb
95
97
  - lib/wrest/native/get.rb
96
98
  - lib/wrest/native/options.rb
97
99
  - lib/wrest/native/post.rb
100
+ - lib/wrest/native/post_multipart.rb
98
101
  - lib/wrest/native/put.rb
102
+ - lib/wrest/native/put_multipart.rb
99
103
  - lib/wrest/native/redirection.rb
100
104
  - lib/wrest/native/request.rb
101
105
  - lib/wrest/native/response.rb