Class: Karaden::Net::Requestor

Inherits:
RequestorInterface show all
Defined in:
lib/karaden/net/requestor.rb

Constant Summary collapse

DEFAULT_USER_AGENT =
'Karaden/Ruby/'.freeze

Instance Method Summary collapse

Instance Method Details

#send(method, path, content_type = nil, params = nil, data = nil, request_options = nil, is_no_contents = false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/karaden/net/requestor.rb', line 6

def send(method, path, content_type = nil, params = nil, data = nil, request_options = nil, is_no_contents = false)
  request_options = Karaden::RequestOptions.new if request_options.nil?
  options = Karaden::Config.as_request_options.merge(request_options).validate
  headers = {
    'User-Agent': build_user_agent(options),
    'Karaden-Client-User-Agent': build_client_user_agent,
    'Karaden-Version' => options.api_version,
    'Content-Type': content_type,
    'Authorization': build_authorization(options),
  }

  uri = URI.parse(build_http_url(path, params, options))
  http = ::Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  http.open_timeout = options.connection_timeout
  http.read_timeout = options.read_timeout

  data = URI.encode_www_form(data) unless data.nil?
  response = http.send_request(method, uri.request_uri, data, headers)

  !is_no_contents ? Karaden::Net::Response.new(response, options) : Karaden::Net::NoContentsResponse.new(response, options)
end