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, configuration: nil) ⇒ RestClient

Initialize the fadaray connection

Parameters:

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

    supply mock API Connection

  • configuration (Diplomat::Configuration) (defaults to: nil)

    a dedicated config to use



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

def initialize(api_connection = nil, configuration: nil)
  @configuration = configuration
  start_connection api_connection
end

Class Method Details

.access_method?(meth_id) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/diplomat/rest_client.rb', line 46

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)


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/diplomat/rest_client.rb', line 55

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 => e
      raise NoMethodError, e
    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)


73
74
75
# File 'lib/diplomat/rest_client.rb', line 73

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)


82
83
84
# File 'lib/diplomat/rest_client.rb', line 82

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



36
37
38
39
40
41
42
43
# File 'lib/diplomat/rest_client.rb', line 36

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

#configurationDiplomat::Configuration

Get client configuration or global one if not specified via initialize.

Returns:



21
22
23
# File 'lib/diplomat/rest_client.rb', line 21

def configuration
  @configuration || ::Diplomat.configuration
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.



29
30
31
# File 'lib/diplomat/rest_client.rb', line 29

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