class StackMaster::ParameterResolvers::Http

Constants

Misconfigured
NotResolved

Public Class Methods

new(_config, _stack_definition) click to toggle source
# File lib/stack_master/parameter_resolvers/http.rb, line 17
def initialize(_config, _stack_definition); end

Public Instance Methods

resolve(args) click to toggle source
# File lib/stack_master/parameter_resolvers/http.rb, line 19
def resolve(args)
  usage unless args.is_a? Hash
  url = args.fetch('url') { usage("'url' not provided") }
  strategy = build_strategy(args.fetch('strategy') { usage("'strategy' not provided") })
  http(url, strategy)
rescue Faraday::Error
  raise NotResolved, "Unable to resolve HTTP parameters from #{url}"
end

Private Instance Methods

build_strategy(strategy) click to toggle source
# File lib/stack_master/parameter_resolvers/http.rb, line 46
def build_strategy(strategy)
  class_name = "StackMaster::HttpParameterResolver::Strategy::"\
               "#{strategy.classify}"
  class_name.constantize.new
rescue NameError
  usage("The strategy #{strategy.inspect} is not supported")
end
connection(url, strategy) click to toggle source
# File lib/stack_master/parameter_resolvers/http.rb, line 59
def connection(url, strategy)
  Faraday.new(
    url: url,
    params: {},
    headers: { 'Accept' => strategy.accept }
  ) do |connection|
    connection.response :raise_error
    connection.adapter :net_http
  end
end
http(url, strategy) click to toggle source
# File lib/stack_master/parameter_resolvers/http.rb, line 54
def http(url, strategy)
  response = connection(url, strategy).get
  strategy.parse(response)
end
usage(message = 'Misconfigured HTTP parameter resolver') click to toggle source
# File lib/stack_master/parameter_resolvers/http.rb, line 30
      def usage(message = 'Misconfigured HTTP parameter resolver')
        raise Misconfigured, <<~MESSAGE
          #{message}

          Please configure according to the following pattern:

          <my-parameter-name>:
            http:
              url: <my-http-url>
              strategy: <response-parsing-strategy>

          Where <response-parsing-strategy> is one of the supported strategies:
          - one_per_line
        MESSAGE
      end