Class: NgrokAPI::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ngrokapi/http_client.rb

Overview

Low-level api client for communicating with Ngrok’s HTTP API using HTTP. You should not have to use this class directly, but use the individual clients to make your API calls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: 'https://api.ngrok.com') ⇒ HttpClient

Returns a new instance of HttpClient.



13
14
15
16
# File 'lib/ngrokapi/http_client.rb', line 13

def initialize(api_key:, base_url: 'https://api.ngrok.com')
  @api_key = api_key
  @base_url = base_url
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/ngrokapi/http_client.rb', line 10

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



10
11
12
# File 'lib/ngrokapi/http_client.rb', line 10

def base_url
  @base_url
end

Instance Method Details

#delete(path, danger: false) ⇒ nil

Make a DELETE request to a given URI

Parameters:

  • path (string)

    URL resource path.

  • danger (boolean) (defaults to: false)

    determine if we should throw an exception on 404 or not

Returns:

  • (nil)


24
25
26
27
28
# File 'lib/ngrokapi/http_client.rb', line 24

def delete(path, danger: false)
  uri = get_uri(path)
  req = Net::HTTP::Delete.new(uri, headers)
  json_do(uri, req, danger: danger)
end

#get(path, danger: false, data: {}) ⇒ json

Make a GET request to a given URI with optional data

Parameters:

  • path (string)

    URL resource path

  • danger (boolean) (defaults to: false)

    determine if we should throw an exception on 404 or not

  • data (hash) (defaults to: {})

    hash which will be converted to query parameters or form data

Returns:

  • (json)

    response body



37
38
39
40
41
# File 'lib/ngrokapi/http_client.rb', line 37

def get(path, danger: false, data: {})
  uri = get_uri(path, data: data)
  req = Net::HTTP::Get.new(uri, headers)
  json_do(uri, req, danger: danger)
end

#list(danger: false, before_id: nil, limit: nil, path: nil, url: nil) ⇒ json

Make a GET request to list resources

Parameters:

  • danger (boolean) (defaults to: false)

    determine if we should throw an exception on 404 or not

  • before_id (string) (defaults to: nil)

    URL resource path

  • limit (integer) (defaults to: nil)

    URL resource path

  • path (string) (defaults to: nil)

    resource path, mutually exclusive with url

  • url (string) (defaults to: nil)

    Full URL of the resource, mutually exclusive with path

  • url (string) (defaults to: nil)

    Full URL of the resource, mutually exclusive with path

Returns:

  • (json)

    response body



53
54
55
56
57
58
59
60
61
62
# File 'lib/ngrokapi/http_client.rb', line 53

def list(danger: false, before_id: nil, limit: nil, path: nil, url: nil)
  if url
    get(url)
  else
    data = {}
    data[:before_id] = before_id if before_id
    data[:limit] = limit if limit
    get(path, danger: danger, data: data)
  end
end

#patch(path, danger: false, data: {}) ⇒ json

Make a PATCH request to a given URI with optional data

Parameters:

  • path (string)

    URL resource path

  • danger (boolean) (defaults to: false)

    determine if we should throw an exception on 404 or not

  • data (hash) (defaults to: {})

    hash which will be converted to query parameters or form data

Returns:

  • (json)

    response body



71
72
73
74
75
# File 'lib/ngrokapi/http_client.rb', line 71

def patch(path, danger: false, data: {})
  uri = get_uri(path)
  req = Net::HTTP::Patch.new(uri, headers_with_json)
  json_do(uri, req, danger: danger, data: deep_to_h(data).to_json)
end

#post(path, danger: false, data: {}) ⇒ json

Make a POST request to a given URI with optional data

Parameters:

  • path (string)

    URL resource path

  • danger (boolean) (defaults to: false)

    determine if we should throw an exception on 404 or not

  • data (hash) (defaults to: {})

    hash which will be converted to query parameters or form data

Returns:

  • (json)

    response body



84
85
86
87
88
# File 'lib/ngrokapi/http_client.rb', line 84

def post(path, danger: false, data: {})
  uri = get_uri(path)
  req = Net::HTTP::Post.new(uri, headers_with_json)
  json_do(uri, req, danger: danger, data: deep_to_h(data).to_json)
end

#put(path, danger: false, data: {}) ⇒ json

Make a PUT request to a given URI with optional data

Parameters:

  • path (string)

    URL resource path

  • danger (boolean) (defaults to: false)

    determine if we should throw an exception on 404 or not

  • data (hash) (defaults to: {})

    hash which will be converted to query parameters or form data

Returns:

  • (json)

    response body



97
98
99
100
101
# File 'lib/ngrokapi/http_client.rb', line 97

def put(path, danger: false, data: {})
  uri = get_uri(path)
  req = Net::HTTP::Put.new(uri, headers_with_json)
  json_do(uri, req, danger: danger, data: deep_to_h(data).to_json)
end