Rubyにpatchをn送ってみよう!

subtitle

2018-07-29 at 株式会社VOYAGE GROUP

subtitle

TokyuRuby会議12

author

うなすけ

theme

unasuke-white

自己紹介

align=“right” relative_margin_right=“-10” relative_margin_top=“42”}

最近

寝転がったままパソコンを使う | うなすけとあれこれ

tqrk12

tqrk12

はい

Rubyにpatchを送る

Rubyにpatchを送る

Rubyにpatchを送った

{:.center} {::tag name=“x-large”}ぼくのばあい{:/tag}

bank/karakuri_ai-ruby

bank/karakuri_ai-ruby's gemspec

spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", "~> 5.0"
spec.add_development_dependency "webmock"

karakuri_ai-ruby/karakuri_ai.gemspec at master · bank/karakuri_ai-ruby · GitHub

net/http のみで実装するAPI Client Gem

net/http のみで実装するAPI Client Gem

Net::HTTPResponse#value

class Net::HTTPResponse (Ruby 2.5.0)

レスポンスが 2xx(成功)でなかった場合に、対応する 例外を発生させます。

オッ、便利か〜〜???

Net::HTTPResponse#value

class Net::HTTPResponse (Ruby 2.5.0)

[EXCEPTION] HTTPServerException: レスポンスが 4xx である場合に発生します。

{::tag name=“x-large”}🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔{:/tag}

HTTP status code

The 4xx class of status code is intended for cases in which the client seems to have erred.The 4xx class of status code is intended for cases in which the client seems to have erred.

www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

400系はクライアントが悪い場合……だよね?

Net::HTTPResponse#value

class Net::HTTPResponse (Ruby 2.5.0)

[EXCEPTION] HTTPServerException: レスポンスが 4xx である場合に発生します。

困ったときの

困ったときの Asakusa.rb

usa-san says…

これは仕様のバグですね

なおす

なおす

やっていきましょう

なおし手順

  1. cloneする

  2. grepする

  3. みつける

  4. 書き換える

  5. testを回す

  6. bugs.ruby-lang.orgに投稿

  7. 待つ

なおし手順 - cloneする

なおし手順 - grepする

$ git grep HTTPServerException

lib/net/http/exceptions.rb が怪しい!

# lib/net/http/exceptions.rb
module Net::HTTPExceptions
  def initialize(msg, res)   #:nodoc:
    super msg
    @response = res
  end
  attr_reader :response
  alias data response    #:nodoc: obsolete
end
### snip ###

なおし手順 - みつける

class Net::HTTPServerException < Net::ProtoServerError
  # We cannot use the name "HTTPServerError", it is the name of the response.
  include Net::HTTPExceptions
end

github.com/ruby/ruby/blob/v2_5_1/lib/net/http/exceptions.rb#l19-l22

ここだ〜〜〜〜

なおし手順 - 書き換える

なおし手順 - 書き換える

しかしこういうことはできない!

alias Net::NiceExceptionName Net::HTTPServerException

これならできる

module Net
  alias NiceExceptionName HTTPServerException
end

なおし手順 - 書き換える

結局こうした

# for compatibility
Net::HTTPClientException = Net::HTTPServerException

{::tag name=“x-small”}usaさんが代入すればいい的なことをおっしゃってたので……{:/tag}

なおし手順 - testを回す

# test/net/http/test_httpresponse.rb
     res = Net::HTTPResponse.read_new(io)
     assert_equal(nil, res.message)
-    assert_raise Net::HTTPServerException do
+    assert_raise Net::HTTPClientException do
       res.error!
     end
   end

なおし手順 - bugs.ruby-lang.orgに投稿

  1. ユーザー登録する

  2. チケット → 「新しいチケット」をクリック

  3. 思いの丈をぶつける

Feature #14688: Net::HTTPResponse#value raises “Net::HTTPServerException” in 4xx response - Ruby trunk - Ruby Issue Tracking System

なおし手順 - 待つ

my patch approved!

Rubyにpatchを送る

Rubyにpatchを送る

Rubyにpatchを送る

twitter.com/mametter/status/1022848181590052864

Rubyにpatchを送る

テストやリファクタリングを頑張るコミッタはあんまいない

Rubyにpatchを送る

Rubyにpatchを送る

Ruby communityにpatchを送る

「Rubyist」という言葉は造語で、Rubyに対して単なるお客さん以上の気持を持っている人がRubyistです。

まとめ