Class: Vra::Http::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Request

Returns a new instance of Request.



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

def initialize(params)
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



20
21
22
# File 'lib/vra/http.rb', line 20

def params
  @params
end

Instance Method Details

#callObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vra/http.rb', line 38

def call
  uri = URI(params[:url]) || raise(":url required")

  ssl_params = { use_ssl: uri.scheme == "https" }
  ssl_params[:verify_mode] = OpenSSL::SSL::VERIFY_NONE unless verify_ssl?

  Net::HTTP.start(uri.host, uri.port, ssl_params) do |http|
    request = http_request(params[:method], uri)
    request.initialize_http_header(params[:headers] || {})
    request.body = params[:payload] || ""

    Response.new(http.request(request))
  end
end

#http_request(method, uri) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vra/http.rb', line 53

def http_request(method, uri)
  type = {
    get: Net::HTTP::Get,
    head: Net::HTTP::Head,
    post: Net::HTTP::Post,
  }.fetch(method, nil)

  raise "Unknown HTTP method #{method}!" unless type

  type.new(uri)
end

#redirect_to(location) ⇒ Object



30
31
32
# File 'lib/vra/http.rb', line 30

def redirect_to(location)
  new(url: location)
end

#redirectable?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/vra/http.rb', line 26

def redirectable?
  [:get, :head].include?(params[:method])
end

#see_other(location) ⇒ Object



34
35
36
# File 'lib/vra/http.rb', line 34

def see_other(location)
  redirect_to(location).new(method: :get)
end