没有将String隐式转换为Integer base62编码轨道

时间:2017-04-13 20:58:34

标签: ruby uuid base62

var gulp        = require('gulp');
var browserSync = require('browser-sync');
var sass        = require('gulp-sass');
var prefix      = require('gulp-autoprefixer');
var cp          = require('child_process');
var cleanCSS    = require('gulp-clean-css');
var uglify      = require('gulp-uglify');
var pump        = require('pump');
var imagemin    = require('gulp-imagemin');
var imageresize = require('gulp-image-resize');
var rename      = require("gulp-rename");

var jekyll   = process.platform === 'win32' ? 'jekyll.bat' : 'jekyll';
var messages = {
    jekyllBuild: '<span style="color: grey">Running:</span> $ jekyll build'
};

/**
 * Build the Jekyll Site
 */
gulp.task('jekyll-build', function (done) {
    browserSync.notify(messages.jekyllBuild);
    return cp.spawn( jekyll , ['build'], {stdio: 'inherit'})
        .on('close', done);
});

/**
 * Rebuild Jekyll & do page reload
 */
gulp.task('jekyll-rebuild', ['jekyll-build'], function () {
    browserSync.reload();
});

/**
 * Wait for jekyll-build, then launch the Server
 */
gulp.task('browser-sync', ['sass', 'jekyll-build'], function() {
    browserSync({
        server: {
            baseDir: '_site'
        }
    });
});

/**
 * Compile files from _scss into both _site/css (for live injecting) and site (for future jekyll builds)
 */
gulp.task('sass', function () {
    return gulp.src('_scss/main.scss')
        .pipe(sass({
            includePaths: ['scss'],
            onError: browserSync.notify
        }))
        .pipe(prefix(['last 15 versions', '> 1%', 'ie 8', 'ie 7'], { cascade: true }))
        .pipe(cleanCSS({compatibility: 'ie8'}))
        .pipe(gulp.dest('_site/css'))
        .pipe(browserSync.reload({stream:true}))
        .pipe(gulp.dest('css'));
});

/**
 * Minify javascript files (must be run separatly after gulp)
 */
gulp.task('compress', function(cb){
  pump([
        gulp.src('js/*.js'),
        uglify(),
        gulp.dest('_site/js')
    ],
    cb
  );
});

/**
 * Compress image files
 */

   var resizeImageTasks = [];
   [400,600,900,1200,1800].forEach(function(size) {
     var resizeImageTask = 'resize_' + size;
     gulp.task(resizeImageTask, function() {
         gulp.src('unsized-img/**/*.{jpg,png,gif}')
             .pipe(imageresize({
               width: size,
               upscale: false,
             }))
             .pipe(rename(function (path) { path.basename += size; }))
             .pipe(gulp.dest('img'));
           gulp.src('img/**/*.{svg,ico}')
            .pipe(gulp.dest('img'));
     });
     resizeImageTasks.push(resizeImageTask);
   });

gulp.task('resize', resizeImageTasks);

gulp.task('optimize', function() {
    gulp.src('img/**/*.{jpg,png,gif}')
        .pipe(imageresize({
          quality: 0.8,
          samplingFactor: [2, 2],
          noProfile: true,
          interlace: true,
        }))
        .pipe(imagemin())
        .pipe(gulp.dest('_site/img'));
      gulp.src('img/**/*.{svg,ico}')
       .pipe(gulp.dest('_site/img'));
});
/**
 * Watch scss files for changes & recompile
 * Watch html/md files, run jekyll & reload BrowserSync
 */
gulp.task('watch', function () {
    gulp.watch('_scss/*.scss', ['sass']);
    gulp.watch(['*.html', '_layouts/*.html', '_posts/*', 'work/*.html', 'services/*.html', 'culture/*.html', 'blog/*.html', 'contact/*.html', 'agency/*.html', 'blog/**/*.md','work/**/*.md'], ['jekyll-rebuild']);
});

/**
 * Default task, running just `gulp` will compile the sass,
 * compile the jekyll site, launch BrowserSync & watch files.
 */
gulp.task('default', ['browser-sync', 'watch']);

没有将String隐式转换为整数

line = uuid = Digest::SHA256.digest(SecureRandom.uuid) id = Base62.encode(uuid)

2 个答案:

答案 0 :(得分:0)

我认为您的代码无法正常运行。这就是原因:

Base62.encode(num)获取一个base10数字并将其转换为base62字符串。这对你来说是一个问题:

Digest::SHA256.digest(SecureRandom.uuid)
# => "\e\x1F\xD6yby\x02o\f)\xA2\x91\xD4\xFB\x85jd\xE0\xF7\xECtd\x8E\xA6\x9Ez\x99\xD8>\x04\nT"

返回一个字符串。

如果您查看base62-rb gem中的代码以及方法上方的注释,您可以看到它将字符串与整数进行比较,这是我尝试时遇到的错误复制一遍:

ArgumentError: comparison of String with 0 failed

这里是来自gem的方法:

# From base62-rb.rb line 8-20:

  # Encodes base10 (decimal) number to base62 string.
  def self.encode(num)
    return "0" if num == 0
    return nil if num < 0

    str = ""
    while num > 0
      # prepend base62 charaters
      str = KEYS[num % BASE] + str
      num = num / BASE
    end
    str
  end

所有这一切当然都取决于您使用的是base62-rb gem。那么,也许你可以给我们一些背景知识,让我们知道你曾尝试过什么?

答案 1 :(得分:0)

  

将字符串传递给base62

uuid = SecureRandom.uuid.gsub("-", "").hex
@id = uuid.base62_encode