class BookableCloud::Cli

Public Instance Methods

check() click to toggle source
# File lib/bookable_cloud/cli.rb, line 14
def check
  check_configuration!
end
fetch() click to toggle source
# File lib/bookable_cloud/cli.rb, line 19
def fetch
  check_configuration!

  assets = api_client.list_assets

  assets.each do |key|
    fetch_asset(key)
    log "Fetched: #{key}"
  end
  # say("Done.", :green) unless options['quiet']
end
watch() click to toggle source
# File lib/bookable_cloud/cli.rb, line 32
def watch
  check_configuration!

  log "Watching directory: #{Dir.pwd}"
  Listen.to(Dir.pwd) do |modified, added, removed|
    modified.each do |filename|
      filename = filename.gsub("#{Dir.pwd}/", '')

      next if File.directory?(filename)

      upload_asset filename
    end
  end.start
  sleep
end

Private Instance Methods

api_client() click to toggle source
# File lib/bookable_cloud/cli.rb, line 111
def api_client
  @api_client ||= ApiClient.new(config)
end
check_configuration!() click to toggle source
# File lib/bookable_cloud/cli.rb, line 50
def check_configuration!
  is_success = api_client.get_theme.is_a?(Net::HTTPSuccess)

  if is_success
    log "Theme works", color: :green
  else
    log "Theme doesnt work", color: :red
    exit(1)
  end
end
config() click to toggle source
# File lib/bookable_cloud/cli.rb, line 115
def config
  @config ||= YAML.load_file 'config.yml'
end
fetch_asset(key) click to toggle source
# File lib/bookable_cloud/cli.rb, line 68
    def fetch_asset(key)
      asset = api_client.get_asset(key)
#      puts asset
#      puts asset

      if asset['value']
        # # For CRLF line endings
        content = asset['value'].gsub("\r", "")
        format = "w"
      elsif asset['attachment']
        content = Base64.decode64(asset['attachment'])
        format = "w+b"
      end

      FileUtils.mkdir_p(File.dirname(key))
      File.open(key, format) {|f| f.write content} if content
    end
log(text, color: nil) click to toggle source
# File lib/bookable_cloud/cli.rb, line 86
def log(text, color: nil)
  text = case color
  when :black
    "\033[30m#{text}\033[0m"
  when :red
    "\033[31m#{text}\033[0m"
  when :green
    "\033[32m#{text}\033[0m"
  when :brown
    "\033[33m#{text}\033[0m"
  when :brown
    "\033[34m#{text}\033[0m"
  when :magenta
    "\033[35m#{text}\033[0m"
  when :cyan
    "\033[36m#{text}\033[0m"
  when :gray
    "\033[37m#{text}\033[0m"
  else
    text
  end

  puts text
end
upload_asset(key) click to toggle source
# File lib/bookable_cloud/cli.rb, line 61
def upload_asset(key)
  log "Uploading #{key}", color: :green
  content = File.read(key)

  api_client.update_asset key, content
end