Class: Stn::Request Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stn/http_request.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A wrapper around Net::HTTP to send HTTP requests to the ServiceTitan API and return their result or raise an error if the result is unexpected. The basic way to use HTTPRequest is by calling run on an instance.

Examples:

List the species of all breeds.

host = ''subdomain.servicetitan.com'
headers = ...
path = '/api/v1/resources/breeds/'
body = {securityToken: security_token}
response = Stn::HTTPRequest.new(path: path, body: body).run
response['Breeds'].map{|breed| breed['species']}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Request

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes an HTTPRequest object.

Parameters:

  • options (Hash) (defaults to: {})

    the options for the request.

Options Hash (options):

  • :host (String)

    The host of the request URI.

  • :path (String)

    The path of the request URI.

  • :headers (Hash) — default: {}

    The HTTP headers for the request.

  • :body (Hash)

    The body of the request.



25
26
27
28
29
30
# File 'lib/stn/http_request.rb', line 25

def initialize(options = {})
  @host = options[:host]
  @path = options[:path]
  @body = options[:body]
  @headers = options.fetch :headers, {}
end

Instance Method Details

#runHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends the request and returns the body parsed from the JSON response.

Returns:

  • (Hash)

    the body parsed from the JSON response.

Raises:

  • (Stn::ConnectionError)

    if the request fails.

  • (Stn::Error)

    if parsed body includes errors.



36
37
38
39
40
41
# File 'lib/stn/http_request.rb', line 36

def run
  return {} if response.body.empty?
  JSON(response.body).tap do |data|
    raise Error, data['error'] if data['error']
  end
end