Class: BOTR::HTTPBackend

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

Instance Method Summary collapse

Instance Method Details

#get(path, params = {}) ⇒ Object

GET request.



8
9
10
11
12
13
14
15
16
17
# File 'lib/botr/http/http_backend.rb', line 8

def get(path, params = {})
  uri = URI(path)
  uri.query = URI.encode_www_form(params)
  
  res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request_get uri
  end
  
  respond(res)
end

#post(path, params = {}, data_path = "") ⇒ Object

POST request with optional multipart/form-data.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/botr/http/http_backend.rb', line 20

def post(path, params = {}, data_path = "")
  uri = URI(path)
  uri.query = URI.encode_www_form(params)

  res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    req = Net::HTTP::Post.new(uri.request_uri)

    if data_path.empty?
      req.set_form_data(params)
    else
      boundary = rand(1000000).to_s
      
      req.body_stream = BOTR::Multipart.new(data_path, boundary)
      req["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
      req["Content-Length"] = req.body_stream.size.to_s    
    end
    
    http.request req
  end

  respond(res)     
end