Module: Ey::Core::Client::Shared

Included in:
Mock, Real
Defined in:
lib/ey-core/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#authenticationObject (readonly)

Returns the value of attribute authentication.



357
358
359
# File 'lib/ey-core/client.rb', line 357

def authentication
  @authentication
end

#cacheObject (readonly)

Returns the value of attribute cache.



357
358
359
# File 'lib/ey-core/client.rb', line 357

def cache
  @cache
end

#urlObject (readonly)

Returns the value of attribute url.



357
358
359
# File 'lib/ey-core/client.rb', line 357

def url
  @url
end

Instance Method Details

#metadataObject

Deprecated.

will be removed in 3.x

See Also:

  • Ey::Core::Client::Shared.{{#services}


462
463
464
# File 'lib/ey-core/client.rb', line 462

def 
  services.get("core")
end

#require_argument(_params, *_requirements) ⇒ Object



437
438
439
440
441
442
443
444
# File 'lib/ey-core/client.rb', line 437

def require_argument(_params, *_requirements)
  params       = Cistern::Hash.stringify_keys(_params)
  requirements = _requirements.map(&:to_s)

  values = params.values_at(*requirements)
  values.all?(&:nil?) && raise(ArgumentError.new("argument is missing or the value is empty: #{requirement}"))
  values.size == 1 ? values.first : values
end

#require_argument!(_params, *_requirements) ⇒ Object



446
447
448
449
450
451
452
453
454
# File 'lib/ey-core/client.rb', line 446

def require_argument!(_params, *_requirements)
  params       = Cistern::Hash.stringify_keys(_params)
  requirements = _requirements.map(&:to_s)

  values = params.values_at(*requirements)
  values.all?(&:nil?) && raise(ArgumentError.new("argument is missing or the value is empty: #{requirement}"))
  requirements.each { |r| params.delete(r) }
  values.size == 1 ? values.first : values
end

#require_arguments(_params, *_requirements) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/ey-core/client.rb', line 424

def require_arguments(_params, *_requirements)
  params       = Cistern::Hash.stringify_keys(_params)
  requirements = _requirements.map(&:to_s)

  values = requirements.map do |requirement|
    if params[requirement].nil?
      raise ArgumentError, "argument is missing or the value is empty: #{requirement}"
    end
    params[requirement]
  end
  [params, *values]
end

#require_parameters(_params, *_requirements) ⇒ Object

strong parameter emulation



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/ey-core/client.rb', line 405

def require_parameters(_params, *_requirements)
  params       = Cistern::Hash.stringify_keys(_params)
  requirements = _requirements.map(&:to_s)

  requirements.each do |requirement|
    unless !params[requirement].nil?
      if self.class == Ey::Core::Client::Real
        raise ArgumentError, "param is missing or the value is empty: #{requirement}"
      else
        response(
          :status => 400,
          :body   => "param is missing or the value is empty: #{requirement}")
      end
    end
  end
  values = params.values_at(*requirements)
  values.size == 1 ? values.first : values
end

#setup(options) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/ey-core/client.rb', line 359

def setup(options)
  token_dotfile = begin
                    if options[:config_file]
                      YAML.load_file(options[:config_file]) || {} # if the file is empty, yaml returns false
                    else
                      YAML.load_file(File.expand_path("~/.ey-core"))
                    end
                  rescue Errno::ENOENT
                    {}
                  end

  resolved_url = options[:url] || ENV["CORE_URL"] || "https://api.engineyard.com/"
  @url   = File.join(resolved_url, "/") # trailing slash # consistency # hash tag you're welcome
  @cache = if options[:cache] == true
             Ey::Core::MemoryCache
           else options[:cache]
           end

  @authentication = nil
  @token = if ENV["CORE_TOKEN"]
             ENV["CORE_TOKEN"]
           elsif options.has_key?(:token) && options[:token].nil?
             @authentication = :none
           else
             options[:token] || token_dotfile[@url] || token_dotfile[@url.gsub(/\/$/, "")] # matching with or without trailing slash
           end

  # For HMAC
  @auth_id  = options[:auth_id]
  @auth_key = options[:auth_key]

  unless @authentication == :none
    if !@auth_id && !@auth_key && !@token
      raise "Missing token. Use Ey::Core::Client.new(token: mytoken) or add \"'#{@url}': mytoken\" to your ~/.ey-core file (see: https://cloud.engineyard.com/cli)" unless @token
    elsif options[:token] || (@token && !@auth_id) # token was explicitly provided
      @authentication = :token
    else
      @authentication = :hmac
      @token = nil
    end
  end

  @logger = options[:logger] || Logger.new(nil)
end

#url_for(*args) ⇒ Object



456
457
458
# File 'lib/ey-core/client.rb', line 456

def url_for(*args)
  File.join(@url.to_s, *args.map(&:to_s))
end