Class: Relax2::Request

Inherits:
Struct
  • Object
show all
Defined in:
lib/relax2/request.rb

Overview

Stores abstract request intents for building Net::HTTP::Request

Constant Summary collapse

SUPPORTED_METHODS =
%w[GET POST PUT PATCH DELETE].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Returns a new instance of Request.



24
25
26
27
# File 'lib/relax2/request.rb', line 24

def initialize(...)
  super
  freeze
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



5
6
7
# File 'lib/relax2/request.rb', line 5

def body
  @body
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



5
6
7
# File 'lib/relax2/request.rb', line 5

def headers
  @headers
end

#http_methodObject

Returns the value of attribute http_method

Returns:

  • (Object)

    the current value of http_method



5
6
7
# File 'lib/relax2/request.rb', line 5

def http_method
  @http_method
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



5
6
7
# File 'lib/relax2/request.rb', line 5

def path
  @path
end

#query_parametersObject

Returns the value of attribute query_parameters

Returns:

  • (Object)

    the current value of query_parameters



5
6
7
# File 'lib/relax2/request.rb', line 5

def query_parameters
  @query_parameters
end

Class Method Details

.from(args:, body: nil) ⇒ Object

Parameters:

  • args (Array<String>)

    assumed to be CLI args. [‘GET’, ‘/hoge’, ‘Authorization:’, ‘Bearer’ ‘mytopsecret’]

  • body (String|nil) (defaults to: nil)

    assumed to be CLI PIPE input.

Raises:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/relax2/request.rb', line 8

def self.from(args:, body: nil)
  arg = FluentArg.new(args)

  raise InvalidArgError, 'STDIN is not acceptable when @body is specified' if arg.magic_parameters[:body] && body

  body = arg.magic_parameters[:body] ? IO.read(arg.magic_parameters[:body]) : body

  new(
    http_method: arg.http_method,
    path: arg.path,
    query_parameters: arg.query_parameters.map(&:to_a),
    headers: arg.headers.map(&:to_a),
    body: body,
  )
end