Sha256: 5e76922001f350df35482818e34ba6faa6f42cb3b8cda37fdc7cbda7b5385ce9

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

module FaaStRuby
  require "faastruby/regions"
  ROOT_DOMAIN = ENV['FAASTRUBY_ROOT_DOMAIN'] || 'faastruby.io'
  WORKSPACE_BASE_HOST = ENV['FAASTRUBY_WORKSPACE_BASE_HOST'] || 'faast.cloud'
  URL_PROTOCOL = ENV['FAASTRUBY_URL_PROTOCOL'] || 'https'
  class << self
    attr_accessor :configuration
  end

  def self.api_key
    configuration&.api_key || ENV['FAASTRUBY_API_KEY']
  end

  def self.api_secret
    configuration&.api_secret || ENV['FAASTRUBY_API_SECRET']
  end

  def self.configure
    self.configuration ||= Configuration.new
    yield(configuration)
  end

  def self.credentials
    {api_key: api_key, api_secret: api_secret}
  end

  def self.api_host
    ENV['FAASTRUBY_HOST'] || "#{URL_PROTOCOL}://api.#{region}.#{ROOT_DOMAIN}"
  end

  def self.workspace_host_for(workspace_name)
    "#{URL_PROTOCOL}://#{workspace_name}.#{region}.#{WORKSPACE_BASE_HOST}"
  end

  class Configuration
    attr_accessor :api_key, :api_secret
  end


  class BaseObject
    def initialize(params = {}, &block)
      @errors = []
      @api = API.new
      self.mass_assign(params) if params
      yield self if block_given?
    end

    def assign_attributes(params = {}, &block)
      self.mass_assign(params) if params
      yield self if block_given?
    end

    def attributes=(params)
      assign_attributes(params)
    end

    def call_api
      response = yield
      @status_code = response.code
      @errors += response.errors if response.errors.any?

      response
    end

    def mass_assign(attrs)
      attrs.each do |key, value|
        self.public_send("#{key}=", value)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
faastruby-0.5.30 lib/faastruby/base.rb
faastruby-0.5.29 lib/faastruby/base.rb
faastruby-0.5.28 lib/faastruby/base.rb
faastruby-0.5.27 lib/faastruby/base.rb
faastruby-0.5.26 lib/faastruby/base.rb