Sha256: 3741448f47790fbacdbd3c0163970822fda141a51ffc17dac21f76937edf6b6e

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

module Buildbox
  class Configuration
    def self.load(*args)
      new(*args).tap &:reload
    end

    require 'json'

    attr_accessor :worker_uuid
    attr_accessor :api_key
    attr_accessor :endpoint
    attr_accessor :use_ssl
    attr_accessor :api_version

    def initialize
      @use_ssl       = true
      @endpoint      = 'api.buildbox.io'
      @api_version   = 1
    end

    def update(key, value)
      self.public_send("#{key}=", value)
      save
    end

    def save
      File.open(path, 'w+') do |file|
        file.write(to_json)
      end
      Buildbox.logger.debug "Configuration saved to `#{path}`"
    end

    def reload
      json = if path.exist?
               read
             else
               save && read
             end

      json.each_pair do |key, value|
        self.public_send("#{key}=", value)
      end
    end

    private

    def to_json
      JSON.pretty_generate(:endpoint    => endpoint,
                           :use_ssl     => use_ssl,
                           :api_version => api_version,
                           :api_key     => api_key,
                           :worker_uuid => worker_uuid)
    end

    def read
      Buildbox.logger.debug "Reading configuration `#{path}`"

      JSON.parse(path.read)
    end

    def path
      Buildbox.root_path.join("configuration.json")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
buildbox-0.0.4 lib/buildbox/configuration.rb
buildbox-0.0.3 lib/buildbox/configuration.rb