Class: Http::Client
- Inherits:
-
Object
- Object
- Http::Client
- Defined in:
- lib/http/client.rb
Overview
We all know what HTTP clients are, right?
Instance Method Summary collapse
-
#connect(uri, options = {}) ⇒ Object
Convert to a transparent TCP/IP tunnel.
-
#delete(uri, options = {}) ⇒ Object
Delete a resource.
-
#get(uri, options = {}) ⇒ Object
Get a resource.
-
#head(uri, options = {}) ⇒ Object
Request a get sans response body.
-
#initialize(uri, options = {}) ⇒ Client
constructor
I swear I’ll document that nebulous options hash.
-
#options(uri, options = {}) ⇒ Object
Return the methods supported on the given URI.
-
#patch(uri, options = {}) ⇒ Object
Apply partial modifications to a resource.
-
#post(uri, options = {}) ⇒ Object
Post to a resource.
-
#put(uri, options = {}) ⇒ Object
Put to a resource.
-
#request(verb, options = {}) ⇒ Object
Make an HTTP request.
-
#trace(uri, options = {}) ⇒ Object
Echo the request back to the client.
Constructor Details
#initialize(uri, options = {}) ⇒ Client
I swear I’ll document that nebulous options hash
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/http/client.rb', line 5 def initialize(uri, = {}) if uri.is_a? URI @uri = uri else # Why the FUCK can't Net::HTTP do this? @uri = URI(uri) end @options = {:response => :object}.merge() end |
Instance Method Details
#connect(uri, options = {}) ⇒ Object
Convert to a transparent TCP/IP tunnel
52 53 54 |
# File 'lib/http/client.rb', line 52 def connect(uri, = {}) request :connect, end |
#delete(uri, options = {}) ⇒ Object
Delete a resource
37 38 39 |
# File 'lib/http/client.rb', line 37 def delete(uri, = {}) request :delete, end |
#get(uri, options = {}) ⇒ Object
Get a resource
22 23 24 |
# File 'lib/http/client.rb', line 22 def get(uri, = {}) request :get, end |
#head(uri, options = {}) ⇒ Object
Request a get sans response body
17 18 19 |
# File 'lib/http/client.rb', line 17 def head(uri, = {}) request :head, end |
#options(uri, options = {}) ⇒ Object
Return the methods supported on the given URI
47 48 49 |
# File 'lib/http/client.rb', line 47 def (uri, = {}) request :options, end |
#patch(uri, options = {}) ⇒ Object
Apply partial modifications to a resource
57 58 59 |
# File 'lib/http/client.rb', line 57 def patch(uri, = {}) request :patch, end |
#post(uri, options = {}) ⇒ Object
Post to a resource
27 28 29 |
# File 'lib/http/client.rb', line 27 def post(uri, = {}) request :post, end |
#put(uri, options = {}) ⇒ Object
Put to a resource
32 33 34 |
# File 'lib/http/client.rb', line 32 def put(uri, = {}) request :put, end |
#request(verb, options = {}) ⇒ Object
Make an HTTP request
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/http/client.rb', line 62 def request(verb, = {}) # Red, green, refactor tomorrow :/ = @options.merge() raw_headers = [:headers] || {} # Stringify keys :/ headers = {} raw_headers.each { |k,v| headers[k.to_s] = v } http = Net::HTTP.new(@uri.host, @uri.port) # Why the FUCK can't Net::HTTP do this either?! http.use_ssl = true if @uri.is_a? URI::HTTPS request_class = Net::HTTP.const_get(verb.to_s.capitalize) request = request_class.new(@uri.request_uri, headers) request.set_form_data([:form]) if [:form] net_http_response = http.request(request) response = Http::Response.new net_http_response.each_header do |header, value| response[header] = value end response.status = Integer(net_http_response.code) # WTF again Net::HTTP response.body = net_http_response.body case [:response] when :object response when :parsed_body response.parse_body when :body response.body else raise ArgumentError, "invalid response type: #{[:response]}" end end |
#trace(uri, options = {}) ⇒ Object
Echo the request back to the client
42 43 44 |
# File 'lib/http/client.rb', line 42 def trace(uri, = {}) request :trace, end |