Sha256: 90009f32d940b1dd90f682d092cb319322431ba5a11d9200a1ac271b3230bab0

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require "json"
require "uri"

module Fling
  # Configuration for the local Tahoe cluster
  class Config
    CONFIG_KEYS = %w(introducer convergence salt dropcap)
    attr_reader(*CONFIG_KEYS)

    # Load configuration from a JSON file
    def self.load_json(file)
      from_json File.read(file)
    end

    # Parse configuration from JSON
    def self.from_json(json)
      new(JSON.parse(json))
    end

    def initialize(options = {})
      CONFIG_KEYS.each do |key|
        fail ArgumentError, "missing key: #{key}" unless options[key]
        instance_variable_set("@#{key}", options[key])
      end

      fail ConfigError, "bad introducer: #{@introducer}" if URI(@introducer).scheme != "pb"
      fail ConfigError, "bad dropcap: #{@dropcap}" unless @dropcap.start_with?("URI:DIR2:")

      %w(convergence salt).each do |key|
        b32_value = options[key]

        begin
          value = Encoding.decode(b32_value)
        rescue
          raise ConfigError, "bad #{key} (base32 error): #{b32_value}"
        end

        fail ConfigError, "bad #{key} (wrong size): #{b32_value}" if value.size != 32
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fling-0.0.3 lib/fling/config.rb