Sha256: 817cf7d92934f04443c8bf73e218f03aa2a1649f6baf35ee424c7012fe6a5a30

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require 'addressable/uri'
require 'active_support/core_ext/hash/indifferent_access'

class Frenetic
  class Configuration

    @@defaults = {
      headers:   {
        accept:     'application/hal+json',
        user_agent: "Frenetic v#{Frenetic::VERSION}; #{Socket.gethostname}"
      }
    }

    def initialize( cfg = {} )
      @_cfg = cfg.symbolize_keys
    end

    def adapter
      @_cfg[:adapter] || Faraday.default_adapter
    end

    def api_token
      @_cfg[:api_token]
    end

    def attributes
      {
        adapter:   adapter,
        api_token: api_token,
        cache:     cache,
        default_root_cache_age: default_root_cache_age,
        headers:   headers,
        password:  password,
        url:       url,
        username:  username
      }
    end

    def cache
      if @_cfg[:cache] == :rack
        {
          metastore:     'file:tmp/rack/meta',
          entitystore:   'file:tmp/rack/body',
          ignore_headers: %w{Authorization Set-Cookie X-Content-Digest}
        }
      else
        {}
      end
    end

    def default_root_cache_age
      @_cfg[:default_root_cache_age]
    end

    def headers
      @@defaults[:headers].merge( @_cfg[:headers] || {} ).tap do |h|
        if @_cfg[:headers] && @_cfg[:headers][:user_agent]
          if h[:user_agent] != @@defaults[:headers][:user_agent]
            h[:user_agent] = "#{h[:user_agent]} (#{@@defaults[:headers][:user_agent]})"
          end
        end
      end
    end

    def password
      @_cfg[:password] || @_cfg[:api_key]
    end

    def url
      Addressable::URI.parse @_cfg[:url]
    end

    def username
      @_cfg[:username] || @_cfg[:app_id]
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
frenetic-0.0.20.alpha.3 lib/frenetic/configuration.rb