Class: Mode::Sdk::Client::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/mode/sdk/client/request.rb

Overview

The Request class wraps a Net::HTTPRequest object for the Mode API

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_request, body = nil, options = {}) ⇒ Mode::Sdk::Client::Request

Construct a new Request instance

Options Hash (options):

  • :expect (Array<Integer>)

    an array of expected response codes

  • :content_type (String)

    the content type of the request

  • :content_length (Integer)

    the content length of the request


26
27
28
29
30
# File 'lib/mode/sdk/client/request.rb', line 26

def initialize(http_request, body = nil, options = {})
  @http_request = http_request
  @body         = body
  @options      = options
end

Class Method Details

.hostString

The Mode API host to use for requests


119
120
121
# File 'lib/mode/sdk/client/request.rb', line 119

def host
  Mode::Sdk.config.host
end

.httpNet::HTTP

The Net::HTTP instance used to make requests


107
108
109
110
111
112
113
# File 'lib/mode/sdk/client/request.rb', line 107

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

  @http = Net::HTTP.new(uri.host, uri.port)
  @http.use_ssl = host =~ /\Ahttps/
  @http
end

Instance Method Details

#responseMode::Sdk::Client::Response

Result of the HTTP request


36
37
38
39
40
41
42
43
# File 'lib/mode/sdk/client/request.rb', line 36

def response
  http_response = self.class.http.request(build_http_request)

  response = Mode::Sdk::Client::Response.new(
    http_response, expected_codes)

  response.tap(&:validate!)
end