Class: ShortcutRuby::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shortcut, action:, params: {}) ⇒ Request

Prepares a fancy request object and ensures the inputs make as much sense as possible. It’s still totally possible to just provide a path that doesn’t match a real url though.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
# File 'lib/shortcut_ruby/request.rb', line 11

def initialize(shortcut, action:, params: {})
  raise ArgumentError unless validate_input(shortcut, action, params)

  self.params = params || {}
  self.uri = construct_uri(shortcut)
  self.action = action
  self.response_format = shortcut.response_format
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



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

def action
  @action
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#response_formatObject

Returns the value of attribute response_format.



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

def response_format
  @response_format
end

#uriObject

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#fetchObject

Executes the http(s) request and provides the response with some additional decoration in a Hash.



23
24
25
26
27
28
29
30
31
32
# File 'lib/shortcut_ruby/request.rb', line 23

def fetch
  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |https|
    req = Net::HTTP.const_get(action).new(uri)

    set_body(req)
    set_format_header(req)

    wrap_response(https.request(req))
  end
end