Class: Diplomat::RestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/diplomat/rest_client.rb

Overview

Base class for interacting with the Consul RESTful API

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_connection = nil) ⇒ RestClient

Initialize the fadaray connection

Parameters:

  • api_connection (Faraday::Connection, nil) (defaults to: nil)

    supply mock API Connection



8
9
10
# File 'lib/diplomat/rest_client.rb', line 8

def initialize(api_connection = nil)
  start_connection api_connection
end

Class Method Details

.access_method?(meth_id) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/diplomat/rest_client.rb', line 32

def access_method?(meth_id)
  @access_methods.include? meth_id
end

.method_missing(meth_id, *args) ⇒ Boolean

Allow certain methods to be accessed without defining “new”.

Parameters:

  • meth_id (Symbol)

    symbol defining method requested

  • *args

    Arguments list

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/diplomat/rest_client.rb', line 41

def method_missing(meth_id, *args)
  if access_method?(meth_id)
    new.send(meth_id, *args)
  else

    # See https://bugs.ruby-lang.org/issues/10969
    begin
      super
    rescue NameError => err
      raise NoMethodError, err
    end
  end
end

.respond_to?(meth_id, with_private = false) ⇒ Boolean

Make ‘respond_to?` aware of method short-cuts.

Parameters:

  • meth_id (Symbol)

    the tested method

Returns:

  • (Boolean)


59
60
61
# File 'lib/diplomat/rest_client.rb', line 59

def respond_to?(meth_id, with_private = false)
  access_method?(meth_id) || super
end

.respond_to_missing?(meth_id, with_private = false) ⇒ Boolean

Make ‘respond_to_missing` aware of method short-cuts. This is needed for #method to work on these, which is helpful for testing purposes.

Parameters:

  • meth_id (Symbol)

    the tested method

Returns:

  • (Boolean)


68
69
70
# File 'lib/diplomat/rest_client.rb', line 68

def respond_to_missing?(meth_id, with_private = false)
  access_method?(meth_id) || super
end

Instance Method Details

#concat_url(parts) ⇒ String

Assemble a url from an array of parts.

Parameters:

  • parts (Array)

    the url chunks to be assembled

Returns:

  • (String)

    the resultant url string



23
24
25
26
27
28
29
# File 'lib/diplomat/rest_client.rb', line 23

def concat_url(parts)
  if parts.length > 1
    parts.first + '?' + parts.drop(1).join('&')
  else
    parts.first
  end
end

#use_named_parameter(name, value) ⇒ Array

Format url parameters into strings correctly

Parameters:

  • name (String)

    the name of the parameter

  • value (String)

    the value of the parameter

Returns:

  • (Array)

    the resultant parameter string inside an array.



16
17
18
# File 'lib/diplomat/rest_client.rb', line 16

def use_named_parameter(name, value)
  value ? ["#{name}=#{value}"] : []
end