Class: ThinHTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/thin_http.rb

Overview

Synopsis

ThinHTTP is a light-weight and user friendly HTTP library. This class is useful for sending simple GET requests or uploading files via POST.

By default, it sends URL encoded data (application/x-www-form-urlencoded). MIME multipart encoded data (multipart/form-data) can be sent by utilizing the MIME library or the MultipartFormData class.

Features

  • Implements GET and POST requests.
  • Follows redirects using GET requests and set instance's host, port, and path attributes using the final destination URI.
  • Accepts either a params hash or an encoded string for POST and GET requests.
  • User-defined headers are sent with each request.
  • Sends and receives cookies automatically.
  • HTTP support only (HTTPS will be implemented when needed).

Design Goals

  • Extremely simple implementation and easy to use interface.
  • Only implement what is absolutely necessary.
  • Adhere to the RFCs.
  • Utilize third party libraries for added functionality when appropriate (i.e. MIME library for constructing multipart/form-data POSTs).
  • Build lightly on top of the standard Net::HTTP library.
  • Useful as a test tool in unit and integration testing (original design goal).

Examples

GET Request

th = ThinHTTP.new('rubyforge.org', 80)
th.set_header('User-Agent', th.class.to_s)
response = th.get('/')

POST Request (URL encoding)

th = ThinHTTP.new('example.com')
response = th.post('/submit_form', :param1 => 'val1', :param2 => 'val2')

POST Request (multipart/form-data encoding using MIME library)

fd = MIME::MultipartMedia::FormData.new
fd.add_entity(MIME::TextMedia.new('Clint'), 'first_name')
fd.add_entity(MIME::DiscreteMediaFactory.create('/tmp/pic.jpg'), 'portrait')

th = ThinHTTP.new('example.com')
response = th.post('/upload_file', fd.body, fd.content_type)

More Examples

  • Check the MultipartFormData class examples
  • Check the ThinHTTPTest class source code

-- TODO

  • May want to create a Response object to encapsulate the HTTPResponse returned from the request methods. See SOAP::Response.
  • May want to decompose the HTTP headers. ++

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 2000, http_headers = {}) ⇒ ThinHTTP

Assign initial connection params for host, port, and http_headers. No connection is established until an HTTP method is invoked.



86
87
88
89
90
91
92
93
# File 'lib/thin_http.rb', line 86

def initialize host = 'localhost', port = 2000, http_headers = {}
  @host = host
  @port = port
  @path = ''
  self.cookies = CookieCollection.new
  self.request_headers = http_headers
  self.response_headers = Hash.new
end

Instance Attribute Details

#cookies ⇒ Object

Returns the value of attribute cookies.



78
79
80
# File 'lib/thin_http.rb', line 78

def cookies
  @cookies
end

#host ⇒ Object

Returns the value of attribute host.



76
77
78
# File 'lib/thin_http.rb', line 76

def host
  @host
end

#path ⇒ Object

Returns the value of attribute path.



77
78
79
# File 'lib/thin_http.rb', line 77

def path
  @path
end

#port ⇒ Object

Returns the value of attribute port.



76
77
78
# File 'lib/thin_http.rb', line 76

def port
  @port
end

#request_headers ⇒ Object

Returns the value of attribute request_headers.



79
80
81
# File 'lib/thin_http.rb', line 79

def request_headers
  @request_headers
end

#response_headers ⇒ Object

Returns the value of attribute response_headers.



80
81
82
# File 'lib/thin_http.rb', line 80

def response_headers
  @response_headers
end

Instance Method Details

#get(path, params = nil) ⇒ Object

Send params to path as a GET request and return the response body.

params may be a URL encoded String or a Hash.



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/thin_http.rb', line 136

def get path, params = nil
  url_path =
    case params
    when Hash    ; path + '?' + url_encode(params)
    when String  ; path + '?' + params
    when NilClass; path
    else raise 'cannot process params'
    end

  send_request Net::HTTP::Get.new(url_path)
end

#post(path, params, content_type = 'application/x-www-form-urlencoded') ⇒ Object

Send params to path as a POST request and return the response body.

params may be a String or a Hash. If params is a String, it is considered encoded data and content_type must be set accordingly. Otherwise, the params Hash is URL encoded.



155
156
157
158
159
160
161
# File 'lib/thin_http.rb', line 155

def post path, params, content_type = 'application/x-www-form-urlencoded'
  post_request = Net::HTTP::Post.new(path)
  post_request.content_type = content_type
  post_request.body = params.is_a?(Hash) ? url_encode(params) : params.to_s

  send_request post_request
end

#set_header(name, value) ⇒ Object

Set the name header and its value in each request.



120
121
122
# File 'lib/thin_http.rb', line 120

def set_header name, value
  request_headers.store(name, value)
end

#unset_header(name) ⇒ Object

Delete the name header, thus not setting name in subsequent requests.



127
128
129
# File 'lib/thin_http.rb', line 127

def unset_header name
  request_headers.delete(name)
end