😀
Faradayでproxyサーバを指定する
概要
Faradayのオフィシャルドキュメントに書いてないので防備録。
irb(main):004:0> Faraday.new('http://example.com', proxy: 'http://103.238.225.61:3128/' ).get('')
=> #<Faraday::Response:0x007ff80b23ba80 @on_complete_callbacks=[], @env=#<Faraday::Env @method=:get @body="<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 50px;\n background-color: #fff;\n border-radius: 1em;\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n body {\n background-color: #fff;\n }\n div {\n width: auto;\n margin: 0 auto;\n border-radius: 0;\n padding: 1em;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.</p>\n <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n" @url=#<URI::HTTP http://example.com/> @request=#<Faraday::RequestOptions proxy=#<Faraday::ProxyOptions uri=#<URI::HTTP http://103.238.225.61:3128/>>> @request_headers={"User-Agent"=>"Faraday v0.12.0.1"} @ssl=#<Faraday::SSLOptions (empty)> @response=#<Faraday::Response:0x007ff80b23ba80 ...> @response_headers={"accept-ranges"=>"bytes", "cache-control"=>"max-age=604800", "content-type"=>"text/html", "date"=>"Thu, 16 Nov 2017 09:08:11 GMT", "etag"=>"\"359670651+gzip\"", "expires"=>"Thu, 23 Nov 2017 09:08:11 GMT", "last-modified"=>"Fri, 09 Aug 2013 23:54:35 GMT", "server"=>"ECS (rhv/818F)", "vary"=>"Accept-Encoding", "x-cache"=>"HIT, MISS from www.54sdqz.com", "content-length"=>"606", "x-cache-lookup"=>"MISS from www.54sdqz.com:3128", "via"=>"1.0 www.54sdqz.com (squid/3.1.23)", "connection"=>"close"} @status=200 @reason_phrase="OK">>
open proxyのアドレスはここから適当に拾ってきました。
Discussion