Redcarpet の辛いところ

TOC(Table Of Contents) を生成する機能があるのだけど、 見出しに日本語が含まれていると、完全にスルーされてしまう点。

## これは概要です

これを redcarpet のレンダリング処理にかませると・・・

<h2 name="">これは概要です</h2>

といった具合。

## This is overview

だと

<h2 name="this-is-overview">これは概要です</h2>

となる。。。

これは辛いのでJSで h1 等から自動的にTOCを組み立てるライブラリも試してみたものの、 かなりのライブラリが日本語をスルーしてしまう・・ この辺りはどう乗り切ってるのかな

204 No Content について

前回の記事 prmd の辛いところ で「手続きクラスをコールするだけのAPIの返り値は No Content で構わない」と書いたものの、 もし今後なんらかの JSON を返却する必要がでてきたら?と考えると、 最初から HTTP/1.1 200 OK としておくべきな気がしてきた。

Response は empty としかったのはドキュメント上でも、 レスポンスサンプルが

{}

だけとなってしまって、意図してこうなっているのかわかりづらいので、 意味のない JSON を返すくらいなら、実体としては No Content なのだから、 HTTP/1.1 204 No Content でいいじゃない、という発想だったのだけど、 もし JSON の返却要件が追加されたら、当然ステータスコード200201 になりうるので、 その時点でステータスコードが変わってしまうのはイマイチ君な気がする。 だったら最初から 200 を返しつつ、空 JSON をレスポンスにしておくのが正しい気がする。

200 にしたけど、やっぱり実装が変わって、  何かリソースを作成、リダイレクトしたいから 201 にするべきってなったらどうするんだというモヤモヤが新たに・・)

prmd の辛いところ

  • HTTP/1.1 204 No Content
  • HTTP/1.1 200 OK + Response は empty

みたいな表現ができない。 いや、正確にはカスタム erb テンプレートを全部自分で用意すればできるのだけど、 templates 全体をコピーして持ってくる必要があったりして、保守観点でやや懸念が残る。

手続きクラスをコールするだけのAPIの返り値は No Content で構わないのだけど、 prmdNo Content を表現すると、rel: 'empty' とするしかなくて、 これをやるとステータスコード202 となってしまう。

202Heroku における定義 だと、 202equest accepted, but the processing has not been completed となっている。 元々 202 ってそういうものだよね、とは思いつつ 手続きクラスがコールされた時点で処理は完了しているのに、 202 を返すところにモヤモヤを感じてしまう。

overview で、うちの 202 はこういう定義だって言い張るのありっちゃありだろうけど、 本当の意味で 202 を返したい場合はどうするやってなってしまうしなぁ・・・

う〜ん やっぱり適切なステータスコードを返すべきな気がするのでカスタムテンプレートでいくべきだ。

prmd と jdoc で JSON Schema の解釈が微妙に違う問題

要するに JSON Schema を解釈して何かするライブラリによって方言が存在する状態になっている。

jdoc204 を表現できるけど、prmd202 となっている他、 status code の表現に使う場所が

    def has_response_body?
      @raw_link.media_type != "null"
    end

    # @return [Fixnum] Preferred respone status code for this endpoint
    def response_status
      case
      when method == "POST"
        201
      when has_response_body?
        200
      else
        204
      end
    end
HTTP/1.1 <%=
  case link['rel']
  when 'create'
    '201 Created'
  when 'empty'
    '202 Accepted'
  else
    '200 OK'
  end %>

みたいな状態。

jdocmedia_type 属性に null を指定すれば 204 だし、 prmdrel 属性に empty を指定すれば 202 ・・・

prmd のほうでは 202 の代わりに 204 にしないの?みたいな話が上がってたり

結局 JSON Schema の仕様が完璧じゃないから、実装によってその辺りがブレるんじゃねっていう状態なのかも・・

とりあえず、利用者も多そうだし、Heroku がメンテしているということで、prmd に寄せていくことにする。

Mac (El Capitan) で nokogiri のインストール @Ruby 2.3.1 でハマった

いつものように

$ ~/.rbenv/plugins/ruby-build

$ git fetch origin

$ git rebase origin/master

$ rbenv install 2.3.1

$ rbenv global 2.3.1

$ gem install bundler

$ rbenv rehash

ここまでは全く問題なし。

つづいて Rails ディレクトリへ、

$ ~/projext_hogehoge/

$ rm -rf vendor/bundle

$ bundle install

....
....
Installing nokogiri 1.6.7.2 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/nyangry/workspace/some_project/vendor/bundle/gems/nokogiri-1.6.7.2/ext/nokogiri
/Users/nyangry/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20160513-85661-v1vt0k.rb extconf.rb --use-system-libraries
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
libxml2 version 2.6.21 or later is required!
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/Users/nyangry/.rbenv/versions/2.3.1/bin/$(RUBY_BASE_NAME)
        --help
        --clean
        --use-system-libraries
        --with-zlib-dir
        --without-zlib-dir
        --with-zlib-include
        --without-zlib-include=${zlib-dir}/include
        --with-zlib-lib
        --without-zlib-lib=${zlib-dir}/lib
        --with-xml2-dir
        --without-xml2-dir
        --with-xml2-include
        --without-xml2-include=${xml2-dir}/include
        --with-xml2-lib
        --without-xml2-lib=${xml2-dir}/lib
        --with-libxml-2.0-config
        --without-libxml-2.0-config
        --with-pkg-config
        --without-pkg-config
        --with-xslt-dir
        --without-xslt-dir
        --with-xslt-include
        --without-xslt-include=${xslt-dir}/include
        --with-xslt-lib
        --without-xslt-lib=${xslt-dir}/lib
        --with-libxslt-config
        --without-libxslt-config
        --with-exslt-dir
        --without-exslt-dir
        --with-exslt-include
        --without-exslt-include=${exslt-dir}/include
        --with-exslt-lib
        --without-exslt-lib=${exslt-dir}/lib
        --with-libexslt-config
        --without-libexslt-config

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/nyangry/workspace/some_project/vendor/bundle/extensions/x86_64-darwin-15/2.3.0-static/nokogiri-1.6.7.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/nyangry/workspace/some_project/vendor/bundle/gems/nokogiri-1.6.7.2 for inspection.
Results logged to /Users/nyangry/workspace/some_project/vendor/bundle/extensions/x86_64-darwin-15/2.3.0-static/nokogiri-1.6.7.2/gem_make.out
An error occurred while installing nokogiri (1.6.7.2), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.6.7.2'` succeeds before bundling.

libxml2 あたりっぽい・・・当然設定は

$ cat ~/.bundle/config
---
BUNDLE_BUILD__NOKOGIRI: "--use-system-libraries"
BUNDLE_BUILD__LIBV8: "--with-system-v8"
BUNDLE_PATH: vendor/bundle
BUNDLE_BIN: vendor/bundle_bin
BUNDLE_JOBS: '8'

$ cat .bundle/config
---
BUNDLE_DISABLE_SHARED_GEMS: true

--use-system-libraries しているし、

$ brew list | grep lib
libevent
libiconv
libpng
libtiff
libtool
libxml2
libxslt

libxml2 も入っている。

いろいろ試して結局

 brew link --force libxml2

で解決。

少し前に El Capitan にアップデートした際にシンボリックリンク周りが壊れたっぽい。(アップデート /usr/local/ 周りが壊れるのはよくある・・)