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

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

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Attributes inherited from Foobara::CommandConnector::Request

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

Instance Method Summary collapse

Methods inherited from Foobara::CommandConnector::Request

#authenticate, #authenticator, #error_collection, #mutate_request, #outcome, #relevant_entity_classes, #response_body, #result, #serializer, #success?

Methods included from NestedTransactionable

#auto_detect_current_transactions, #commit_transaction, #commit_transaction_if_open, #open_transaction, #opened_transactions, #relevant_entity_classes, relevant_entity_classes_for_type, #relevant_entity_classes_for_type, #rollback_transaction, #transactions, #use_transaction, with_needed_transactions_for_type

Methods included from Foobara::Concern

foobara_class_methods_module_for, foobara_concern?, included

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
47
48
49
50
# File 'foobara-http-command-connector-0.0.26/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
)
  if cookies
    cookies = cookies.transform_keys(&:to_s)
  end

  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.26/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.26/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.26/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.26/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.26/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.26/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.26/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.26/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.26/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.26/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.26/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.26/src/http/request.rb', line 7

def scheme
  @scheme
end

Instance Method Details

#actionObject



87
88
89
90
91
92
93
# File 'foobara-http-command-connector-0.0.26/src/http/request.rb', line 87

def action
  unless defined?(@action)
    set_action_and_command_name
  end

  @action
end

#argumentObject



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

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

#full_command_nameObject



66
67
68
69
70
71
72
# File 'foobara-http-command-connector-0.0.26/src/http/request.rb', line 66

def full_command_name
  unless defined?(@full_command_name)
    set_action_and_command_name
  end

  @full_command_name
end

#inputsObject



62
63
64
# File 'foobara-http-command-connector-0.0.26/src/http/request.rb', line 62

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

#parsed_bodyObject



74
75
76
# File 'foobara-http-command-connector-0.0.26/src/http/request.rb', line 74

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

#parsed_query_stringObject



78
79
80
81
82
83
84
85
# File 'foobara-http-command-connector-0.0.26/src/http/request.rb', line 78

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



105
106
107
108
109
110
111
112
113
114
115
# File 'foobara-http-command-connector-0.0.26/src/http/request.rb', line 105

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



99
100
101
102
103
# File 'foobara-http-command-connector-0.0.26/src/http/request.rb', line 99

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

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

#urlObject



52
53
54
55
56
57
58
59
60
# File 'foobara-http-command-connector-0.0.26/src/http/request.rb', line 52

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