Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

splitted the send method and corrected body creation

  • Loading branch information...
commit 5f911324eff8875d991b4a82c6c79d1df6a33d7d 1 parent 9d752c9
@hukl authored
Showing with 26 additions and 8 deletions.
  1. +18 −6 lib/rig/http.rb
  2. +8 −2 lib/rig/http_body.rb
View
24 lib/rig/http.rb
@@ -13,7 +13,7 @@ module Rig
HTTPMethods = %w(GET POST PUT DELETE)
class HTTP
- attr_reader :options, :header, :body
+ attr_reader :options, :header, :body, :tcp_socket, :response
def initialize *options
@options = normalize_options( options )
@@ -26,15 +26,27 @@ def initialize *options
@header = HTTPHeader.new( @options )
end
+ def send_header
+ @tcp_socket ||= TCPSocket.new( @options[:host], @options[:port] )
+ @tcp_socket.write( @header.to_s )
+ end
+
+ def send_body
+ begin
+ @tcp_socket ||= TCPSocket.new( @options[:host], @options[:port] )
+ @tcp_socket.write( @body.to_s )
+ @response = tcp_socket.read
+ ensure
+ @tcp_socket.close
+ end
+ end
+
def send
begin
- tcp_socket = TCPSocket.new( @options[:host], @options[:port] )
- tcp_socket.write( @header.to_s + @body.to_s )
- response = tcp_socket.read
+ send_header
+ send_body
rescue => exception
puts exception.message
- ensure
- tcp_socket.close
end
HTTPResponse.new( response ) || exception.message
View
10 lib/rig/http_body.rb
@@ -35,7 +35,9 @@ def boundary
def multipart?
if defined? @multipart
@multipart
- elsif @options
+ elsif @options.is_a?( String )
+ @multipart = false
+ elsif @options.is_a?( Hash )
@multipart = @options.values.any? do |element|
element.respond_to?( :read )
end
@@ -45,7 +47,11 @@ def multipart?
end
def create_simple_body
- push @options.map {|key, value| "#{key}=#{value}"}.join("&")
+ if @options.is_a?( String )
+ push @options
+ elsif @options.is_a?( Hash )
+ push @options.map {|key, value| "#{key}=#{value}"}.join("&")
+ end
end
def create_multipart_body
Please sign in to comment.
Something went wrong with that request. Please try again.