Class: Http::Client

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

Overview

We all know what HTTP clients are, right?

Instance Method Summary collapse

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, options = {})
  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(options)
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, options = {})
  request :connect, options
end

#delete(uri, options = {}) ⇒ Object

Delete a resource



37
38
39
# File 'lib/http/client.rb', line 37

def delete(uri, options = {})
  request :delete, options
end

#get(uri, options = {}) ⇒ Object

Get a resource



22
23
24
# File 'lib/http/client.rb', line 22

def get(uri, options = {})
  request :get, options
end

#head(uri, options = {}) ⇒ Object

Request a get sans response body



17
18
19
# File 'lib/http/client.rb', line 17

def head(uri, options = {})
  request :head, options
end

#options(uri, options = {}) ⇒ Object

Return the methods supported on the given URI



47
48
49
# File 'lib/http/client.rb', line 47

def options(uri, options = {})
  request :options, 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, options = {})
  request :patch, options
end

#post(uri, options = {}) ⇒ Object

Post to a resource



27
28
29
# File 'lib/http/client.rb', line 27

def post(uri, options = {})
  request :post, options
end

#put(uri, options = {}) ⇒ Object

Put to a resource



32
33
34
# File 'lib/http/client.rb', line 32

def put(uri, options = {})
  request :put, options
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, options = {})
  # Red, green, refactor tomorrow :/
  options = @options.merge(options)
  raw_headers = options[: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(options[:form]) if options[: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 options[:response]
  when :object
    response
  when :parsed_body
    response.parse_body
  when :body
    response.body
  else raise ArgumentError, "invalid response type: #{options[: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, options = {})
  request :trace, options
end