Last active
May 20, 2022 12:23
-
Star
(185)
You must be signed in to star a gist -
Fork
(8)
You must be signed in to fork a gist
Revisions
-
rummelonp revised this gist
Mar 7, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -97,7 +97,7 @@ end params = { :name => 'nyanco', :picture => Faraday::UploadIO.new('nyanco.jpg', 'image/jpeg') } conn.put '/api/nyan.json', params ``` -
rummelonp revised this gist
May 24, 2012 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,11 @@ # Ruby の HTTP クライアントライブラリ Faraday が便利そう Ruby の HTTP クライアントライブラリ Faraday が便利そう API ラッパの開発には [RestClient gem][rest_client_gem] だとか OAuth の必要なものは [Net/HTTP][net_http] + [OAuth gem][oauth_gem] を使ってた [Twitter gem][twitter_gem] や [Instagram gem][instagram_gem] など API ライブラリのソースを読んでみると [Faraday gem][faraday_gem] というものがよく使われてた なんとなく気になったので Faraday について調べてみた -
rummelonp revised this gist
May 24, 2012 . 1 changed file with 9 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,6 @@ # Ruby での HTTP クライアントライブラリ Faraday が便利そう Ruby での HTTP クライアントライブラリ Faraday が便利そう API ラッパの開発には [RestClient gem][rest_client_gem] だとか OAuth の必要なものは [Net/HTTP][net_http] + [OAuth gem][oauth_gem] を使ってた @@ -19,7 +21,9 @@ OAuth の必要なものは [Net/HTTP][net_http] + [OAuth gem][oauth_gem] を使 Faraday は HTTP クライアントライブラリ Net::HTTP やその他の多くのアダプタに共通のインターフェースを提供し また Rack ミドルウェアのようなインターフェースでリクエスト/レスポンスのサイクルを処理することが出来る --- ## Faraday でサポートしてるアダプターは? @@ -29,6 +33,8 @@ Net::HTTP やその他の多くのアダプタに共通のインターフェー * Patron * EventMachine --- ## 使い方は? こんな感じで使える @@ -214,7 +220,7 @@ API によって gem を使い分けるみたいな面倒くさいことしな --- ## 参考 [Faraday gem wiki][faraday_wiki] [Faraday Middleware gem wiki][faraday_middleware_wiki] -
rummelonp revised this gist
May 24, 2012 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ API ラッパの開発には [RestClient gem][rest_client_gem] だとか OAuth の必要なものは [Net/HTTP][net_http] + [OAuth gem][oauth_gem] を使ってた [Twitter gem][twitter_gem] や [Instagram gem][instagram_gem] のなど API ライブラリのソースを読んでみると [Faraday gem][faraday_gem] というものがよく使われてた なんとなく気になったので Faraday について調べてみた @@ -233,6 +233,7 @@ API によって gem を使い分けるみたいな面倒くさいことしな [faraday_wiki]: https://github.com/technoweenie/faraday/wiki [faraday_middleware_gem]: https://github.com/pengwynn/faraday_middleware [faraday_middleware_wiki]: https://github.com/pengwynn/faraday_middleware/wiki [tumblife_gem]: https://github.com/mitukiii/tumblife-for-ruby [tumblife_parse_json]: https://github.com/mitukiii/tumblife-for-ruby/blob/master/lib/tumblife/response/parse_json.rb [tumblife_raise_client_error]: https://github.com/mitukiii/tumblife-for-ruby/blob/master/lib/tumblife/response/raise_client_error.rb [tumblife_raise_server_error]: https://github.com/mitukiii/tumblife-for-ruby/blob/master/lib/tumblife/response/raise_server_error.rb -
rummelonp revised this gist
May 24, 2012 . 1 changed file with 16 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,13 @@ # Ruby での API ライブラリの開発に Faraday が便利 API ラッパの開発には [RestClient gem][rest_client_gem] だとか OAuth の必要なものは [Net/HTTP][net_http] + [OAuth gem][oauth_gem] を使ってた [Twitter gem][twitter_gem] や [Instagram gem][instagran_gem] のなど API ライブラリのソースを読んでみると [Faraday gem][faraday_gem] というものがよく使われてた なんとなく気になったので Faraday について調べてみた ついでに [Tumblife gem][tumblife_gem] を Faraday を使うようにした 以下そのメモ @@ -110,18 +110,18 @@ conn.put '/api/nyan.json', params * response * :raise_error (4xx, 5xx エラー時に例外を投げる) 他に [FaradayMiddleware gem][faraday_middleware_gem] というナイスなライブラリがあって これを使うと色々な便利ミドルウェアが使えるようになる * request * :oauth (OAuth 対応 / [SimpleAuth gem][simple_auth_gem] 依存) * :oauth2 (OAuth2 対応) * response * :caching (レスポンスをキャッシュする) * :parse_json (レスポンスの JSON を Hash 形式で返す / [JSON gem][json_gem] 依存) * :parse_xml (レスポンスの XML を Hash 形式で返す / [MultiXML gem][multi_xml_gem] 依存) * :mashify (レスポンスの Hash を Hashie::Mash 形式で返す / [Hashie gem][hashie_gem] 依存) * :follow_redirects (リダイレクトを追う) 便利そうなのはこんなところか @@ -184,11 +184,11 @@ class MyResponseMiddleware < Faraday::Response::Middleware end ``` 因みに [Tumblife gem][tumblife_gem] では下記のミドルウェアを自分で拡張したので参考までに * [JSON gem の代わりに MultiJson gem で JSON をパース][tumblife_parse_json] * [クライアントエラー(HTTP Status 4xx)時に例外を投げる][tumblife_raise_client_error] * [サーバーエラー(HTTP Status 5xx)時に例外を投げる][tumblife_raise_client_error] --- @@ -216,8 +216,8 @@ API によって gem を使い分けるみたいな面倒くさいことしな ## 以下参考 [Faraday gem wiki][faraday_wiki] [Faraday Middleware gem wiki][faraday_middleware_wiki] [net_http]: http://doc.ruby-lang.org/ja/1.9.2/library/net=2fhttp.html -
rummelonp revised this gist
May 23, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -69,7 +69,7 @@ end POST はこう書く ```ruby conn.post '/api/wan.json', {:color => :black} # POST "color=black" to http://example.com/api/nyan.json conn.post do |req| req.url '/api/wan.json' -
rummelonp revised this gist
May 23, 2012 . 1 changed file with 17 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -114,19 +114,20 @@ conn.put '/api/nyan.json', params これを使うと色々な便利ミドルウェアが使えるようになる * request * :oauth (OAuth 対応 / [SimpleAuth gem](simple_auth_gem) 依存) * :oauth2 (OAuth2 対応) * response * :caching (レスポンスをキャッシュする) * :parse_json (レスポンスの JSON を Hash 形式で返す / [JSON gem](json_gem) 依存) * :parse_xml (レスポンスの XML を Hash 形式で返す / [MultiXML gem](multi_xml_gem) 依存) * :mashify (レスポンスの Hash を Hashie::Mash 形式で返す / [Hashie gem](hashie_gem) 依存) * :follow_redirects (リダイレクトを追う) 便利そうなのはこんなところか 因みにミドルウェアへパラメータを渡す時は、ミドルウェアの宣言時の第二引数以降で渡す ミドルウェアは宣言した順番と逆に呼び出されるので書く順番には注意 ```ruby conn = Faraday.new(options) do |builder| @@ -157,8 +158,6 @@ class MyMiddlewre < Faraday::Middleware @app.call(env).on_complete do # レスポンスに対して何かしたい場合はここに書く end end end ``` @@ -187,7 +186,7 @@ end 因みに [Tumblife gem](tumblife_gem) では下記のミドルウェアを自分で拡張したので参考までに * [JSON gem の代わりに MultiJson gem で JSON をパース](tumblife_parse_json) * [クライアントエラー(HTTP Status 4xx)時に例外を投げる](tumblife_raise_client_error) * [サーバーエラー(HTTP Status 5xx)時に例外を投げる](tumblife_raise_client_error) @@ -198,7 +197,9 @@ end * HTTP リクエスト部分が宣言的に美しく書ける * リクエスト/レスポンスが柔軟に設定出来る 自分で使ってて感じたのはこのくらいか 有名な API ライブラリが使ってるから...という感じは否めない でも慣れてきたら便利な気がする あと OAuth / OAuth2 両方に対応してるので API によって gem を使い分けるみたいな面倒くさいことしなくても良い @@ -215,12 +216,17 @@ API によって gem を使い分けるみたいな面倒くさいことしな ## 以下参考 [Faraday gem wiki](faraday_wiki) [Faraday Middleware gem wiki](faraday_middleware_wiki) [net_http]: http://doc.ruby-lang.org/ja/1.9.2/library/net=2fhttp.html [rest_client_gem]: https://github.com/adamwiggins/rest-client [oauth_gem]: http://oauth.rubyforge.org/ [simple_auth_gem]: https://github.com/optimis/simple-auth [json_gem]: http://flori.github.com/json/ [multi_xml_gem]: https://github.com/sferik/multi_xml [hashie_gem]: https://github.com/intridea/hashie [twitter_gem]: https://github.com/jnunemaker/twitter [instagram_gem]: https://github.com/Instagram/instagram-ruby-gem [faraday_gem]: https://github.com/technoweenie/faraday -
rummelonp created this gist
May 23, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,232 @@ # Ruby での API ライブラリの開発に Faraday が便利 API ラッパの開発には [RestClient gem](rest_client_gem) だとか OAuth の必要なものは [Net/HTTP](net_http) + [OAuth gem](oauth_gem) を使ってた [Twitter gem](twitter_gem) や [Instagram gem](instagran_gem) のなど API ライブラリのソースを読んでみると [Faraday gem](faraday_gem) というものがよく使われてた なんとなく気になったので Faraday について調べてみた ついでに [Tumblife gem](tumblife_gem) を Faraday を使うようにした 以下そのメモ --- ## Faraday って何? > Faraday is an HTTP client lib that provides a common interface over many adapters (such as Net::HTTP) and embraces the concept of Rack middleware when processing the request/response cycle. Faraday は HTTP クライアントライブラリ Net::HTTP やその他の多くのアダプタに共通のインターフェースを提供し また Rack ミドルウェアのようなインターフェースでリクエスト/レスポンスのサイクルを処理することが出来ます ## Faraday でサポートしてるアダプターは? * Net/HTTP * Excon * Typhoeus * Patron * EventMachine ## 使い方は? こんな感じで使える builder.use で使用するミドルウェアを宣言する ```ruby conn = Faraday::Connection.new(:url => 'http://example.com') do |builder| builder.use Faraday::Request::UrlEncoded # リクエストパラメータを URL エンコードする builder.use Faraday::Response::Logger # リクエストを標準出力に出力する builder.use Faraday::Adapter::NetHttp # Net/HTTP をアダプターに使う end response = conn.get '/api/nyan.json' # GET http://example.com/api/nyan.json puts response.body ``` あるいは Faraday::Connection.new ... の部分はこうとも書ける ```ruby conn = Faraday.new(:url => 'http://example.com') do |builder| builder.request :url_encoded builder.response :logger builder.adapter :net_http end ``` また GET リクエストの送信部分もこう書くことも出来る ```ruby conn.get '/api/nyan.json', {:color => :black} # GET http://example.com/api/nyan.json?color=black response = conn.get do |req| # GET http://example.com/api/nyan.json?color=white&size=big req.url '/api/nyan.json', {:color => :white} req.params[:size] = :big end ``` POST はこう書く ```ruby conn.post '/api/wan.json', {:color => :black} # POST http://example.com/api/nyan.json?color=black conn.post do |req| req.url '/api/wan.json' req.body = { :color => :black, :size => :big } end ``` ファイルのアップロードも出来る ```ruby conn = Faraday.new(:url => 'http://example.com') do |builder| builder.request :multipart # マルチパートでデータを送信 builder.request :url_encoded builder.adapter :net_http end params = { :name => 'nyanco', :picture => Faraday::UploadUI.new('nyanco.jpg', 'image/jpeg') } conn.put '/api/nyan.json', params ``` --- ## ミドルウェアを使う 上記の他にこんなミドルウェアがある 主要(と思われる)ものだけ抜粋した * request * :basic_authentication (ベーシック認証) * :retry (リクエスト失敗時に指定回数まで再接続する) * response * :raise_error (4xx, 5xx エラー時に例外を投げる) 他に [FaradayMiddleware gem](faraday_middleware_gem) というナイスなライブラリがあって これを使うと色々な便利ミドルウェアが使えるようになる * request * :oauth (OAuth 対応 / SimpleOAuth gem 依存) * :oauth2 (OAuth2 対応) * response * :caching (レスポンスをキャッシュする) * :parse_json (レスポンスの JSON を Hash 形式で返す / JSON gem 依存) * :parse_xml (レスポンスの XML を Hash 形式で返す / MultiXML gem 依存) * :mashify (レスポンスの Hash を Hashie::Mash 形式で返す / Hashie gem 依存) * :follow_redirects (リダイレクトを追う) 便利そうなのはこんなところか 因みにミドルウェアへパラメータを渡す時は、ミドルウェアの宣言時の第二引数以降で渡す ```ruby conn = Faraday.new(options) do |builder| credentials = { :consumer_key => consumer_key, :consumer_secret => consumer_secret, :token => oauth_token, :token_secret => oauth_token_secret } builder.request :oauth, credentials end ``` その他詳しいオプションとか使い方は各公式ドキュメント見てくれ --- ## ミドルウェアを自分で拡張する call メソッドが実装されたクラスを作る Faraday::Middleware を継承すると面倒見てくれるので便利 ```ruby class MyMiddlewre < Faraday::Middleware def call(env) # リクエストに対して何かしたい場合はここに書く @app.call(env).on_complete do # レスポンスに対して何かしたい場合はここに書く end @app.call(env) # 次のミドルウェアを呼び出す end end ``` env には以下のものが Hash で入ってる * request phase * :method * :url * :body * response phase * :status * :body * :response_headers レスポンスだけ弄りたい場合は Faraday::Response::Middleware を継承すれば良い ```ruby class MyResponseMiddleware < Faraday::Response::Middleware def on_complete(env) # レスポンスに対して何かする end end ``` 因みに [Tumblife gem](tumblife_gem) では下記のミドルウェアを自分で拡張したので参考までに * [Json gem の代わりに MultiJson gem で JSON をパース](tumblife_parse_json) * [クライアントエラー(HTTP Status 4xx)時に例外を投げる](tumblife_raise_client_error) * [サーバーエラー(HTTP Status 5xx)時に例外を投げる](tumblife_raise_client_error) --- ## 結局 Faraday を使うと何が嬉しいの? * HTTP リクエスト部分が宣言的に美しく書ける * リクエスト/レスポンスが柔軟に設定出来る 自分で使ってて感じたのはこのくらいか あと OAuth / OAuth2 両方に対応してるので API によって gem を使い分けるみたいな面倒くさいことしなくても良い --- ## その他 使用できるアダプターにある EventMachine がちょっと気になってる これを使うと並列かつ非同期でリクエストが送れたりするのだろうか クローラ等作るのに便利そうなので気が向いたら調べてみよう --- ## 以下参考 [Faraday gem wiki](faraday_wiki) [Faraday Middleware gem wiki](faraday_middleware_wiki) [net_http]: http://doc.ruby-lang.org/ja/1.9.2/library/net=2fhttp.html [oauth_gem]: http://oauth.rubyforge.org/ [rest_client_gem]: https://github.com/adamwiggins/rest-client [twitter_gem]: https://github.com/jnunemaker/twitter [instagram_gem]: https://github.com/Instagram/instagram-ruby-gem [faraday_gem]: https://github.com/technoweenie/faraday [faraday_wiki]: https://github.com/technoweenie/faraday/wiki [faraday_middleware_gem]: https://github.com/pengwynn/faraday_middleware [faraday_middleware_wiki]: https://github.com/pengwynn/faraday_middleware/wiki [tumblife_parse_json]: https://github.com/mitukiii/tumblife-for-ruby/blob/master/lib/tumblife/response/parse_json.rb [tumblife_raise_client_error]: https://github.com/mitukiii/tumblife-for-ruby/blob/master/lib/tumblife/response/raise_client_error.rb [tumblife_raise_server_error]: https://github.com/mitukiii/tumblife-for-ruby/blob/master/lib/tumblife/response/raise_server_error.rb