Class: Bubbles::RestEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/bubbles/rest_environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scheme = 'https', host = 'api.foamfactory.com', port = 443, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestEnvironment

Construct a new instance of RestEnvironment.

Parameters:

  • scheme (String) (defaults to: 'https')

    The scheme to use for communicating with the host. Currently, http and https are supported.

  • host (String) (defaults to: 'api.foamfactory.com')

    The host to communicate with.

  • port (Integer) (defaults to: 443)

    The port on which the communication channel should operate.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to identify your client with the API. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header that will specify the API key. Defaults to “X-API-Key”.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bubbles/rest_environment.rb', line 14

def initialize(scheme='https', host='api.foamfactory.com', port=443, api_key=nil, api_key_name='X-API-Key')
  @scheme = scheme
  @port = port

  if @scheme == 'http' && @port == nil
    @port = 80
  elsif @scheme == 'https' && @port == nil
    @port = 443
  end

  @host = host
  @api_key = api_key
  @api_key_name = api_key_name
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/bubbles/rest_environment.rb', line 3

def api_key
  @api_key
end

#api_key_nameString

Retrieve the name of the API key to be used.

This will be the “key” portion of the key-value of the API key header.

Returns:

  • (String)

    The API key name, if set; “X-API-Key”, otherwise.



36
37
38
# File 'lib/bubbles/rest_environment.rb', line 36

def api_key_name
  @api_key_name
end

#hostObject

Returns the value of attribute host.



3
4
5
# File 'lib/bubbles/rest_environment.rb', line 3

def host
  @host
end

#portObject

Returns the value of attribute port.



3
4
5
# File 'lib/bubbles/rest_environment.rb', line 3

def port
  @port
end

Instance Method Details

#get_api_key_if_needed(endpoint) ⇒ String

Retrieve an API key from this RestEnvironment, but only if a specific Endpoint requires it.

If an Endpoint has api_key_required set to true, this method will return the API for the current RestEnvironment. If not, then it will return nil, rather than just blindly returning the API key for every possible retrieval, even if the Endpoint doesn’t require it.

Returns:

  • (String)

    The API key for this RestEnvironment, if the specified Endpoint requires it; nil, otherwise.



50
51
52
53
54
55
56
# File 'lib/bubbles/rest_environment.rb', line 50

def get_api_key_if_needed(endpoint)
  if endpoint.api_key_required?
    @api_key
  else
    nil
  end
end

#schemeSymbol

Retrieve the scheme of the current RestEnvironment, as a Symbol.

Returns:

  • (Symbol)

    The scheme of the current RestEnvironment, as a Symbol.



63
64
65
# File 'lib/bubbles/rest_environment.rb', line 63

def scheme
  @scheme.to_s
end