Class: HTTP::Requestor

Inherits:
Object
  • Object
show all
Defined in:
lib/http_requestor.rb,
lib/http_requestor_multipart.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain, options = {}) ⇒ Requestor

Instance Methods ===================

9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/http_requestor.rb', line 9

def initialize(domain, options={})
  domain = "http://#{domain}" if domain.match(/^(http|https):\/\//).nil?
  
  @defaults = {:domain => domain}
  @uri = URI.parse(@defaults[:domain])
  if @uri.scheme == "http"
    @http = Net::HTTP.new(@uri.host, @uri.port)
  else
    @http = Net::HTTP.new(@uri.host, @uri.port)
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
end

Class Method Details

.multipart_request(domain, request_type, data = {}, headers = {}, options = {}) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/http_requestor_multipart.rb', line 29

def self.multipart_request(domain, request_type, data={}, headers={}, options={})
  request_type.to_s.upcase!
  request_type = "POST" unless ["POST", "PUT"].include?(request_type)
  req = self.new(domain, options)

  if request_type == "POST"
    return req.post_multipart("", data, headers)
  elsif request_type == "PUT"
    return req.put_multipart("", data, headers)
  end
end

.request(domain, request_type, request_path, data = {}, headers = {}, options = {}) ⇒ Object

Class Methods ===================

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/http_requestor.rb', line 118

def self.request(domain, request_type, request_path, data={}, headers={}, options={})
  request_type.to_s.upcase!
  request_type = valid_request_types[0] unless valid_request_types.include?(request_type)
  req = self.new(domain, options)
  if request_type == "GET"
    return req.get(request_path, data, headers)
  elsif request_type == "POST"
    return req.post(request_path, data, headers)
  elsif request_type == "PUT"
    return req.put(request_path, data, headers)
  elsif request_type == "DELETE"
    return req.delete(request_path, data, headers)
  elsif request_type == "OPTIONS"
    return req.options(request_path, data, headers)
  elsif request_type == "PATCH"
    return req.patch(request_path, data, headers)
  elsif request_type == "MOVE"
    return req.move(request_path, data, headers)
  elsif request_type == "HEAD"
    return req.head(request_path, data, headers)
  elsif request_type == "TRACE"
    return req.trace(request_path, data, headers)
  end
end

.request_with_url(url, request_type, data = {}, headers = {}) ⇒ Object


143
144
145
146
# File 'lib/http_requestor.rb', line 143

def self.request_with_url(url, request_type, data={}, headers={})
  uri = URI.parse(url)
  return self.request("#{uri.scheme}://#{uri.host}:#{uri.port}", request_type, uri.request_uri, data, headers)
end

.send_basic_auth_request(url, username, password) ⇒ Object


148
149
150
151
152
153
154
155
# File 'lib/http_requestor.rb', line 148

def self.send_basic_auth_request(url, username, password)
  uri = URI.parse(url)
  
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.request_uri)
  request.basic_auth(username, password)
  http.request(request)
end

.valid_request_typesObject


157
158
159
# File 'lib/http_requestor.rb', line 157

def self.valid_request_types
  ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH", "MOVE", "HEAD", "TRACE"]
end

Instance Method Details

#data_to_query(data) ⇒ Object


113
114
115
# File 'lib/http_requestor.rb', line 113

def data_to_query(data)
  @defaults[:data] = (data.nil? || data.empty?) ? "" : data.to_query
end

#delete(path, data = {}, headers = nil) ⇒ Object


53
54
55
56
57
58
59
60
61
# File 'lib/http_requestor.rb', line 53

def delete(path,data={},headers=nil)
  data_to_query(data)
  if headers == nil
    response = @http.send_request('DELETE', path, @defaults[:data])
  else
    response = @http.send_request('DELETE', path, @defaults[:data], headers)
  end
  response
end

#get(path, data = {}, headers = nil) ⇒ Object


23
24
25
26
27
28
29
30
31
# File 'lib/http_requestor.rb', line 23

def get(path,data={},headers=nil)
  data_to_query(data)
  if headers == nil
    response = @http.send_request('GET', path, @defaults[:data])
  else
    response = @http.send_request('GET', path, @defaults[:data], headers)
  end
  response
end

#head(path, data = {}, headers = nil) ⇒ Object


93
94
95
96
97
98
99
100
101
# File 'lib/http_requestor.rb', line 93

def head(path,data={},headers=nil)
  data_to_query(data)
  if headers == nil
    response = @http.send_request('HEAD', path, @defaults[:data])
  else
    response = @http.send_request('HEAD', path, @defaults[:data], headers)
  end
  response
end

#move(path, data = {}, headers = nil) ⇒ Object


83
84
85
86
87
88
89
90
91
# File 'lib/http_requestor.rb', line 83

def move(path,data={},headers=nil)
  data_to_query(data)
  if headers == nil
    response = @http.send_request('MOVE', path, @defaults[:data])
  else
    response = @http.send_request('MOVE', path, @defaults[:data], headers)
  end
  response
end

#options(path, data = {}, headers = nil) ⇒ Object


63
64
65
66
67
68
69
70
71
# File 'lib/http_requestor.rb', line 63

def options(path,data={},headers=nil)
  data_to_query(data)
  if headers == nil
    response = @http.send_request('OPTIONS', path, @defaults[:data])
  else
    response = @http.send_request('OPTIONS', path, @defaults[:data], headers)
  end
  response
end

#patch(path, data = {}, headers = nil) ⇒ Object


73
74
75
76
77
78
79
80
81
# File 'lib/http_requestor.rb', line 73

def patch(path,data={},headers=nil)
  data_to_query(data)
  if headers == nil
    response = @http.send_request('PATCH', path, @defaults[:data])
  else
    response = @http.send_request('PATCH', path, @defaults[:data], headers)
  end
  response
end

#post(path, data = {}, headers = nil) ⇒ Object


33
34
35
36
37
38
39
40
41
# File 'lib/http_requestor.rb', line 33

def post(path,data={},headers=nil)
  data_to_query(data)
  if headers == nil
    response = @http.send_request('POST', path, @defaults[:data])
  else
    response = @http.send_request('POST', path, @defaults[:data], headers)
  end
  response
end

#post_multipart(path = "", data = {}, headers = nil) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/http_requestor_multipart.rb', line 3

def post_multipart(path="", data={},headers=nil)
  @uri = URI.parse(@defaults[:domain] + path) unless path.empty?
  build_data(data)
  req = Net::HTTP::Post.new(@uri.request_uri)
  req.initialize_http_header(headers) unless headers.nil?
  req.body = @post_body
  req["Content-Type"] = "multipart/form-data; boundary=#{@boundary}"
  res = @http.request(req)
  @post_body = ""
  @boundary = ""
  res
end

#put(path, data = {}, headers = nil) ⇒ Object


43
44
45
46
47
48
49
50
51
# File 'lib/http_requestor.rb', line 43

def put(path,data={},headers=nil)
  data_to_query(data)
  if headers == nil
    response = @http.send_request('PUT', path, @defaults[:data])
  else
    response = @http.send_request('PUT', path, @defaults[:data], headers)
  end
  response
end

#put_multipart(path = "", data = {}, headers = nil) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/http_requestor_multipart.rb', line 16

def put_multipart(path="", data={},headers=nil)
  @uri = URI.parse(@defaults[:domain] + path) unless path.empty?
  build_data(data)
  req = Net::HTTP::Put.new(@uri.request_uri)
  req.initialize_http_header(headers) unless headers.nil?
  req.body = @post_body
  req["Content-Type"] = "multipart/form-data; boundary=#{@boundary}"
  res = @http.request(req)
  @post_body = ""
  @boundary = ""
  res
end

#trace(path, data = {}, headers = nil) ⇒ Object


103
104
105
106
107
108
109
110
111
# File 'lib/http_requestor.rb', line 103

def trace(path,data={},headers=nil)
  data_to_query(data)
  if headers == nil
    response = @http.send_request('TRACE', path, @defaults[:data])
  else
    response = @http.send_request('TRACE', path, @defaults[:data], headers)
  end
  response
end