Module: Request

Defined in:
lib/http_transport_provider/request.rb

Class Method Summary collapse

Class Method Details

.build(uri, config, params = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/http_transport_provider/request.rb', line 5

def self.build(uri, config, params = {})
  verb = config['verb']
  if verb.upcase == 'GET'
    if params.empty? == false
      uri.query = URI.encode_www_form(params)
      path = "#{uri.path}#{'?' + uri.query if uri.query}"
    else
      if uri.path == ''
        path = '/'
      else
        path = uri.path
      end
    end

    request = Net::HTTP::Get.new path
  elsif verb.upcase == 'POST'
    if uri.path != ''
      path = uri.path
    else
      path = '/'
    end

    request = Net::HTTP::Post.new(path)
    if params.empty? == false
      request.set_form_data(params)
    end
  end

  if config['credentials'].nil? == false
    request.basic_auth(config['credentials']['username'], config['credentials']['password'])
  end

  #TODO: check if return object is valid net/http request
  request
end