Class: Mayu::Fetch
Overview
This class implements a simplified version of the Fetch API. https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
Defined Under Namespace
Classes: Response
Instance Method Summary collapse
- #fetch(url, method: :GET, headers: {}, body: nil) ⇒ Object
-
#initialize ⇒ Fetch
constructor
A new instance of Fetch.
Constructor Details
#initialize ⇒ Fetch
Returns a new instance of Fetch.
48 49 50 |
# File 'lib/mayu/fetch.rb', line 48 def initialize @internet = T.let(Async::HTTP::Internet.new, Async::HTTP::Internet) end |
Instance Method Details
#fetch(url, method: :GET, headers: {}, body: nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mayu/fetch.rb', line 60 def fetch(url, method: :GET, headers: {}, body: nil) puts "\e[35mFETCHING #{url}\e[0m" res = @internet.call(method, url, headers.to_a, body) puts "\e[34mFETCHED #{url}\e[0m" Response.new( url:, body: res.read, headers: res.headers.to_h, status: res.status, status_text: Rack::Utils::HTTP_STATUS_CODES[res.status], ok: res.success?, redirected: res.redirection? ) rescue => e puts "\e[32mFAILED ON #{url}\e[0m" raise end |