Class: Tlopo::Request

Inherits:
Object
  • Object
show all
Extended by:
Setters
Defined in:
lib/tlopo/request.rb,
lib/tlopo/request/version.rb

Constant Summary collapse

VERSION =
'0.1.1'

Instance Method Summary collapse

Methods included from Setters

make_setter

Constructor Details

#initializeRequest



25
26
27
28
29
# File 'lib/tlopo/request.rb', line 25

def initialize
  @method = 'get'
  @insecure = false
  @http_trace = !ENV['HTTP_TRACE'].nil? && ENV['HTTP_TRACE'].downcase == 'true'
end

Instance Method Details

#run(&block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tlopo/request.rb', line 31

def run(&block)
  instance_eval(&block) if block_given?
  uri = URI(@url)
  is_https = uri.scheme == 'https'

  uri.path = '/' if uri.path.empty?

  path = uri.path

  unless uri.query.nil?
    path = uri.query.empty? ? uri.path : "#{uri.path}?#{uri.query}"
  end

  req = Object.const_get("Net::HTTP::#{@method.capitalize}").new(path)
  req.body = @payload

  @headers&.each { |k, v| req[k] = v }

  opts = {}

  opts[:use_ssl] = is_https
  opts[:verify_mode] = OpenSSL::SSL::VERIFY_NONE if is_https && @insecure

  Net::HTTP.start(uri.host, uri.port, opts) do |http|
    r = http.request(req)
    msg = "HTTP_TRACE - code: #{r.code} method: #{@method}, url: #{@url},"
    msg += " headers: #{@headers}, payload: #{@payload}"
    LOGGER.debug msg if @http_trace
    return r
  end
end