lib/buildbox/configuration.rb in buildbox-0.0.4 vs lib/buildbox/configuration.rb in buildbox-0.1

- old
+ new

@@ -1,62 +1,38 @@ +require 'rubygems' +require 'hashie/dash' +require 'json' + module Buildbox - class Configuration - def self.load(*args) - new(*args).tap &:reload - end + class Configuration < Hashie::Dash + property :worker_access_token, :default => nil + property :api_endpoint, :default => "http://api.buildbox.dev/v1" - 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) + def update(attributes) + attributes.each_pair { |key, value| self[key] = value } save end def save - File.open(path, 'w+') do |file| - file.write(to_json) - end - Buildbox.logger.debug "Configuration saved to `#{path}`" + File.open(path, 'w+') { |file| file.write(pretty_json) } end def reload - json = if path.exist? - read - else - save && read - end - - json.each_pair do |key, value| - self.public_send("#{key}=", value) + if path.exist? + read_and_load + else + save && read_and_load 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) + def pretty_json + JSON.pretty_generate(self) end - def read - Buildbox.logger.debug "Reading configuration `#{path}`" - - JSON.parse(path.read) + def read_and_load + merge! JSON.parse(path.read) end def path Buildbox.root_path.join("configuration.json") end