Class: Foobara::CommandConnectors::Http::Request

Inherits:
Foobara::CommandConnector::Request show all
Defined in:
foobara-http-command-connector-0.0.23/src/http/request.rb

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Attributes inherited from Foobara::CommandConnector::Request

#command, #command_class, #command_connector, #error, #response, #serializers

Instance Method Summary collapse

Methods inherited from Foobara::CommandConnector::Request

#error_collection, #outcome, #response_body, #result, #serializer, #success?

Methods included from TruncatedInspect

#inspect, truncating

Constructor Details

#initialize(path:, method: nil, headers: {}, query_string: "", body: "", scheme: nil, host: nil, port: nil, cookies: nil, remote_ip: nil, prefix: nil) ⇒ Request

Returns a new instance of Request.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 20

def initialize(
  path:,
  method: nil,
  headers: {},
  query_string: "",
  body: "",
  scheme: nil,
  host: nil,
  port: nil,
  cookies: nil,
  remote_ip: nil,
  prefix: nil
)
  self.path = path
  self.method = method
  self.headers = headers
  self.query_string = query_string
  self.body = body
  self.scheme = scheme
  self.host = host
  self.port = port
  self.cookies = cookies
  self.remote_ip = remote_ip
  self.prefix = prefix

  super()
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def body
  @body
end

#cookiesObject

Returns the value of attribute cookies.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def cookies
  @cookies
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def headers
  @headers
end

#hostObject

Returns the value of attribute host.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def host
  @host
end

#methodObject

Returns the value of attribute method.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def method
  @method
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def path
  @path
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def port
  @port
end

#prefixObject

Returns the value of attribute prefix.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def prefix
  @prefix
end

#query_stringObject

Returns the value of attribute query_string.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def query_string
  @query_string
end

#remote_ipObject

Returns the value of attribute remote_ip.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def remote_ip
  @remote_ip
end

#response_headersObject

Returns the value of attribute response_headers.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def response_headers
  @response_headers
end

#schemeObject

Returns the value of attribute scheme.



7
8
9
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 7

def scheme
  @scheme
end

Instance Method Details

#actionObject



83
84
85
86
87
88
89
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 83

def action
  unless defined?(@action)
    set_action_and_command_name
  end

  @action
end

#argumentObject



91
92
93
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 91

def argument
  prefixless_path.split("/")[2]
end

#full_command_nameObject



62
63
64
65
66
67
68
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 62

def full_command_name
  unless defined?(@full_command_name)
    set_action_and_command_name
  end

  @full_command_name
end

#inputsObject



58
59
60
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 58

def inputs
  @inputs ||= parsed_body.merge(parsed_query_string)
end

#parsed_bodyObject



70
71
72
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 70

def parsed_body
  body.empty? ? {} : JSON.parse(body)
end

#parsed_query_stringObject



74
75
76
77
78
79
80
81
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 74

def parsed_query_string
  if query_string.nil? || query_string.empty?
    {}
  else
    # TODO: override this in rack connector to use better rack utils
    CGI.parse(query_string).transform_values!(&:first)
  end
end

#prefixless_pathObject



101
102
103
104
105
106
107
108
109
110
111
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 101

def prefixless_path
  return @prefixless_path if defined?(@prefixless_path)

  prefix = self.prefix

  @prefixless_path = if prefix
                       path.gsub(/^#{prefix}\//, "/")
                     else
                       path
                     end
end

#set_action_and_command_nameObject



95
96
97
98
99
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 95

def set_action_and_command_name
  @action, *full_command_name = prefixless_path[1..].split("/")

  @full_command_name = full_command_name.join("::").gsub("/", "::")
end

#set_response_header(name, value) ⇒ Object



113
114
115
116
117
118
119
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 113

def set_response_header(name, value)
  name = name.to_s
  name = name.downcase

  self.response_headers ||= {}
  self.response_headers = response_headers.merge(name => value)
end

#urlObject



48
49
50
51
52
53
54
55
56
# File 'foobara-http-command-connector-0.0.23/src/http/request.rb', line 48

def url
  URI::Generic.build(
    scheme:,
    host:,
    port:,
    path:,
    query: query_string.nil? || query_string.empty? ? nil : query_string
  ).to_s
end