{"id":3302,"date":"2018-07-11T10:00:30","date_gmt":"2018-07-11T01:00:30","guid":{"rendered":"https:\/\/blog.timecrowd.net\/?p=3302"},"modified":"2018-07-06T14:23:31","modified_gmt":"2018-07-06T05:23:31","slug":"generate-shorten-url-by-rails","status":"publish","type":"post","link":"https:\/\/blog.timecrowd.net\/generate-shorten-url-by-rails\/","title":{"rendered":"Rails\u3067\u77ed\u7e2eURL\u3092\u767a\u884c\u3059\u308b"},"content":{"rendered":"<p>TimeCrowd\u3067\u306f\u62db\u5f85URL\u3092\u767a\u884c\u3057\u3066\u30e1\u30f3\u30d0\u30fc\u3092\u62db\u5f85\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002\u305d\u306e\u969b\u306b\u76f8\u624b\u306b\u62db\u5f85URL\u3092\u9023\u7d61\u3059\u308b\u306e\u3067\u3059\u304c\u3001\u3042\u307e\u308a\u306b\u9577\u3044\u306e\u3067\u6587\u7ae0\u5185\u306b\u57cb\u3081\u8fbc\u3080\u3068\u30a4\u30f3\u30d1\u30af\u30c8\u304c\u5f37\u3059\u304e\u308b\u3068\u3044\u3046\u8981\u671b\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u3053\u3067\u4eca\u56de\u306f\u62db\u5f85URL\u306b\u5bfe\u3057\u3066\u77ed\u7e2eURL\u3092\u4f7f\u7528\u3057\u3066\u898b\u3084\u3059\u3044\u5f62\u306b\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u7121\u4e8b\u306b\u53f0\u6e7e\u3067\u30c9\u30e9\u30b4\u30f3\u30dc\u30fc\u30c8\u306e\u8a66\u5408\u3092\u7d42\u3048\u3066\u5e30\u56fd\u3057\u305f\u65c5\u3057\u3066\u304d\u305f\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u4e09\u5b85\u3067\u3059\u3002<\/p>\n<div id=\"toc_container\" class=\"toc_white no_bullets\"><p class=\"toc_title\">\u76ee\u6b21<\/p><ul class=\"toc_list\"><li><a href=\"#Shorten\"><span class=\"toc_number toc_depth_1\">1<\/span> Shorten\u306e\u5c0e\u5165<\/a><\/li><li><a href=\"#URL\"><span class=\"toc_number toc_depth_1\">2<\/span> \u77ed\u7e2eURL\u3092\u4f7f\u3063\u3066\u30a2\u30af\u30bb\u30b9<\/a><\/li><li><a href=\"#i\"><span class=\"toc_number toc_depth_1\">3<\/span> \u53c2\u8003\u8a18\u4e8b<\/a><\/li><\/ul><\/div>\n<h2><span id=\"Shorten\">Shorten\u306e\u5c0e\u5165<\/span><\/h2>\n<p>\u4eca\u56de\u306f<a href=\"https:\/\/github.com\/jpmcgrath\/shortener\" target=\"_blank\" rel=\"noopener\">Shorten<\/a>\u3068\u3044\u3046gem\u3092\u5229\u7528\u3057\u3066\u6a5f\u80fd\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/jpmcgrath\/shortener#installation\" target=\"_blank\" rel=\"noopener\">Installation<\/a>\u3092\u53c2\u7167\u3057\u306a\u304c\u3089\u9032\u3081\u307e\u3059\u3002<\/p>\n<pre><code># Gemfile\r\ngem 'shortener'\r\n<\/code><\/pre>\n<pre><code>bin\/rails generate shortener\r\nbin\/rake db:migrate\r\nRunning via Spring preloader in process 16007\r\n== 20180706014530 CreateShortenedUrlsTable: migrating =========================\r\n-- create_table(:shortened_urls)\r\n   -&gt; 0.0302s\r\n-- add_index(:shortened_urls, :unique_key, {:unique=&gt;true})\r\n   -&gt; 0.0293s\r\n-- add_index(:shortened_urls, :url, {:length=&gt;2083})\r\nrake aborted!\r\nStandardError: An error has occurred, all later migrations canceled:\r\n\r\nMysql2::Error: Specified key was too long; max key length is 3072 bytes: CREATE  INDEX `index_shortened_urls_on_url`  ON `shortened_urls` (`url`(2083))\r\n\/Users\/kotamiyake\/code\/ruffnote\/timecrowd\/db\/migrate\/20180706014530_create_shortened_urls_table.rb:29:in `change'\r\n-e:1:in `'\r\nActiveRecord::StatementInvalid: Mysql2::Error: Specified key was too long; max key length is 3072 bytes: CREATE  INDEX `index_shortened_urls_on_url`  ON `shortened_urls` (`url`(2083))\r\n\/Users\/kotamiyake\/code\/ruffnote\/timecrowd\/db\/migrate\/20180706014530_create_shortened_urls_table.rb:29:in `change'\r\n-e:1:in `'\r\nMysql2::Error: Specified key was too long; max key length is 3072 bytes\r\n\/Users\/kotamiyake\/code\/ruffnote\/timecrowd\/db\/migrate\/20180706014530_create_shortened_urls_table.rb:29:in `change'\r\n-e:1:in `'\r\nTasks: TOP =&gt; db:migrate\r\n(See full trace by running task with --trace)<\/code><\/pre>\n<p>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3067\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u3069\u3046\u3084\u3089\u30ab\u30e9\u30e0\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u6700\u5927\u30ad\u30fc\u9577\u304c\u9577\u3059\u304e\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>MySQL\u3067\u306f\u57fa\u672c\u7684\u306b\u306f\u5358\u4e00\u30ab\u30e9\u30e0\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u6700\u5927\u30ad\u30fc\u9577\u306f767\u30d0\u30a4\u30c8\u307e\u3067\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002\u7279\u5b9a\u306e\u6761\u4ef6\u3067\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u6700\u5927\u30ad\u30fc\u9577\u30923072\u30d0\u30a4\u30c8\u307e\u3067\u62e1\u5f35\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>TimeCrowd\u3067\u306f\u6700\u5927\u30ad\u30fc\u95773072\u30d0\u30a4\u30c8\u307e\u3067\u62e1\u5f35\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u305d\u308c\u3067\u3082\u8db3\u308a\u306a\u304b\u3063\u305f\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30d5\u30a1\u30a4\u30eb\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>class CreateShortenedUrlsTable &lt; ActiveRecord::Migration[4.2]\r\n  def change\r\n    create_table :shortened_urls do |t|\r\n      # we can link this to a user for interesting things\r\n      t.integer :owner_id\r\n      t.string :owner_type, limit: 20\r\n\r\n      # the real url that we will redirect to\r\n      t.text :url, null: false, length: 2083\r\n\r\n      # the unique key\r\n      t.string :unique_key, limit: 10, null: false\r\n\r\n      # a category to help categorize shortened urls\r\n      t.string :category\r\n\r\n      # how many times the link has been clicked\r\n      t.integer :use_count, null: false, default: 0\r\n\r\n      # valid until date for expirable urls\r\n      t.datetime :expires_at\r\n\r\n      t.timestamps\r\n    end\r\n\r\n    # we will lookup the links in the db by key, urls and owners.\r\n    # also make sure the unique keys are actually unique\r\n    add_index :shortened_urls, :unique_key, unique: true\r\n    add_index :shortened_urls, :url, length: 2083\r\n    add_index :shortened_urls, [:owner_id, :owner_type]\r\n    add_index :shortened_urls, :category\r\n  end\r\nend<\/code><\/pre>\n<p>url\u306elength\u304c\u30ab\u30e9\u30e0\u9577\u3001\u30ad\u30fc\u9577\u5171\u306b2083\u6587\u5b57\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>TimeCrowd\u3067\u306f\u6587\u5b57\u30b3\u30fc\u30c9\u306butf8mb4\u3092\u63a1\u7528\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u30ab\u30e9\u30e0\u9577\u3001\u30ad\u30fc\u9577\u3092768\u6587\u5b57\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u5e78\u3044\u62db\u5f85URL\u306e\u9577\u3055\u306f768\u6587\u5b57\u3092\u8d85\u3048\u306a\u3044\u306e\u3067768\u6587\u5b57\u306b\u5909\u66f4\u3057\u3066\u3001\u518d\u5ea6\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre><code>bin\/rake db:migrate\r\nRunning via Spring preloader in process 37580\r\n== 20180706014530 CreateShortenedUrlsTable: migrating =========================\r\n-- create_table(:shortened_urls)\r\n   -&gt; 0.0339s\r\n-- add_index(:shortened_urls, :unique_key, {:unique=&gt;true})\r\n   -&gt; 0.0286s\r\n-- add_index(:shortened_urls, :url, {:length=&gt;768})\r\n   -&gt; 0.0217s\r\n-- add_index(:shortened_urls, [:owner_id, :owner_type])\r\n   -&gt; 0.0275s\r\n-- add_index(:shortened_urls, :category)\r\n   -&gt; 0.0278s\r\n== 20180706014530 CreateShortenedUrlsTable: migrated (0.1404s) ================<\/code><\/pre>\n<p>\u7121\u4e8b\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306b\u6210\u529f\u3057\u307e\u3057\u305f\u3002<\/p>\n<h2><span id=\"URL\">\u77ed\u7e2eURL\u3092\u4f7f\u3063\u3066\u30a2\u30af\u30bb\u30b9<\/span><\/h2>\n<p>\u307e\u305a\u306f\u77ed\u7e2eURL\u304b\u3089\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3059\u308b\u305f\u3081\u306e\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre><code>get '\/s\/:id' =&gt; 'shortener\/shortened_urls#show'<\/code><\/pre>\n<p>\u4f8b\u3067\u306f\u30eb\u30fc\u30c8\u76f4\u4e0b\u306b\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u77ed\u7e2eURL\u3068\u3044\u3046\u3053\u3068\u3067namespace\u3068\u3057\u3066\/s\/\u3067\u533a\u5207\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u308c\u3067\u306f\u6b21\u306b\u77ed\u7e2eURL\u3092\u767a\u884c\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre><code>pry(main)&gt; shortened_url = Shortener::ShortenedUrl.generate(\"https:\/\/timecrowd.net\")\r\npry(main)&gt; shortened_url.unique_key\r\n=&gt; \"5wr8x\"<\/code><\/pre>\n<p>\u3067\u306f\u958b\u767a\u74b0\u5883\u3092\u4f7f\u3063\u3066\u77ed\u7e2eURL\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>http:\/\/localhost:3000\/s\/5wr8x<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-3307\" src=\"https:\/\/blog.timecrowd.net\/wp-content\/uploads\/2018\/07\/screenshot-timecrowd.net-2018.07.06-12-29-08.png\" alt=\"TimeCrowd\u30c8\u30c3\u30d7\u30da\u30fc\u30b8\u753b\u50cf\" width=\"1440\" height=\"803\" srcset=\"https:\/\/blog.timecrowd.net\/wp-content\/uploads\/2018\/07\/screenshot-timecrowd.net-2018.07.06-12-29-08.png 1440w, https:\/\/blog.timecrowd.net\/wp-content\/uploads\/2018\/07\/screenshot-timecrowd.net-2018.07.06-12-29-08-768x428.png 768w\" sizes=\"(max-width: 1440px) 100vw, 1440px\" \/><\/p>\n<p>\u6b63\u5e38\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3059\u308b\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u753b\u9762\u3067\u8868\u793a\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-3310\" src=\"https:\/\/blog.timecrowd.net\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-06-at-12.40.39.png\" alt=\"\u77ed\u7e2eURL\u753b\u50cf\" width=\"1132\" height=\"622\" srcset=\"https:\/\/blog.timecrowd.net\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-06-at-12.40.39.png 1132w, https:\/\/blog.timecrowd.net\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-06-at-12.40.39-768x422.png 768w\" sizes=\"(max-width: 1132px) 100vw, 1132px\" \/><\/p>\n<p>\u3060\u3044\u3076\u30b9\u30c3\u30ad\u30ea\u3057\u307e\u3057\u305f\u306d\uff01<\/p>\n<p>\u3053\u308c\u3067\u4eca\u307e\u3067\u306e\u9577\u3005\u3068\u3057\u305fURL\u3092\u30c1\u30e3\u30c3\u30c8\u3084\u30e1\u30fc\u30eb\u306b\u6dfb\u4ed8\u3059\u308b\u5fc5\u8981\u306f\u306a\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u3060\u3053\u3061\u3089\u306e\u6a5f\u80fd\u306f\u30ea\u30ea\u30fc\u30b9\u3057\u3066\u3044\u307e\u305b\u3093\u304c\u3001\u8fd1\u3005\u30ea\u30ea\u30fc\u30b9\u3067\u304d\u308b\u3088\u3046\u92ed\u610f\u958b\u767a\u4e2d\u3067\u3059\u306e\u3067\u4e5e\u3046\u3054\u671f\u5f85\u304f\u3060\u3055\u3044\uff01<\/p>\n<h2><span id=\"i\">\u53c2\u8003\u8a18\u4e8b<\/span><\/h2>\n<p>\u7b2c32\u56de\u3000InnoDB\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u6700\u5927\u30ad\u30fc\u9577\u306b\u3064\u3044\u3066\uff1aMySQL\u9053\u666e\u8acb\u4fbf\u308a\uff5cgihyo.jp \u2026 \u6280\u8853\u8a55\u8ad6\u793e<br \/>\n<a href=\"http:\/\/gihyo.jp\/dev\/serial\/01\/mysql-road-construction-news\/0032\" target=\"_blank\" rel=\"noopener\">http:\/\/gihyo.jp\/dev\/serial\/01\/mysql-road-construction-news\/0032<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"TimeCrowd\u3067\u306f\u62db\u5f85URL\u3092\u767a\u884c\u3057\u3066\u30e1\u30f3\u30d0\u30fc\u3092\u62db\u5f85\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002\u305d\u306e\u969b\u306b\u76f8\u624b\u306b\u62db\u5f85URL\u3092\u9023\u7d61\u3059\u308b\u306e\u3067\u3059\u304c\u3001\u3042\u307e\u308a\u306b\u9577\u3044\u306e\u3067\u6587\u7ae0&#8230;","protected":false},"author":5,"featured_media":3308,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"cybocfi_hide_featured_image":""},"categories":[5],"tags":[39,12],"_links":{"self":[{"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/posts\/3302"}],"collection":[{"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/comments?post=3302"}],"version-history":[{"count":6,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/posts\/3302\/revisions"}],"predecessor-version":[{"id":3311,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/posts\/3302\/revisions\/3311"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/media\/3308"}],"wp:attachment":[{"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/media?parent=3302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/categories?post=3302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/tags?post=3302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}