Sha256: d0e974e731089da8db40f83aa10ed757f5439c263f9002936fdebbe136ab74de

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require "yaml"

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']
          user['email']
        end
        result
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shelly-0.0.23 lib/shelly/cloudfile.rb