Sha256: 5ab1ed1893e2ae1c99da32b4fe60c10d550b4cc71c471c1e5251c0e3b6371649

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module Shelly
  class Cloudfile < Model
    attr_accessor :content

    def self.present?
      File.exists?(File.join(Dir.pwd, "Cloudfile"))
    end

    def initialize
      open if File.exists?(path)
    end

    def path
      File.join(Dir.pwd, "Cloudfile")
    end

    def open
      @content = YAML.load(File.open(path))
    end

    def write(hash)
      @content = hash
      File.open(path, "w") do |f|
        f.write(yaml(hash))
      end
    end

    def clouds
      @content.keys.sort
    end

    def yaml(hash)
      string = hash.deep_stringify_keys.to_yaml
      # FIXME: check if it possible to remove sub("---", "") by passing options to_yaml
      string.sub("---","").split("\n").map(&:rstrip).join("\n").strip
    end

    def fetch_users
      response = shelly.apps_users(clouds)
      response.inject({}) do |result, app|
        result[app['code_name']] = app['users'].map do |user|
          user['name'] ? "#{user['email']} (#{user['name']})" : user['email']
        end
        result
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shelly-0.0.21.pre2 lib/shelly/cloudfile.rb
shelly-0.0.21 lib/shelly/cloudfile.rb