5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

method_missing を利用して動的にAPIのエンドポイントにアクセスする

Posted at

mail chimpのrubyライブラリーのgibbonの中でAPIのエンドポイントにアクセスするために個別にメソッドを定義するのではなく、method_missingを利用してAPIコールを行なっています。まだ見れていませんが、railsのfind_by_等のメソッド呼び出しもこれを利用してるのでしょうか?
自分のコードにも応用できそうです。

def method_missing(method, *args)
method = method.to_s.gsub(//(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
method = method[0].chr.downcase + method[1..-1].gsub(/aim$/i, 'AIM')
 call(method, *args)
end

5
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?