Module: Rager::Utils::Http
- Extended by:
- T::Sig
- Defined in:
- lib/rager/utils/http.rb
Class Method Summary collapse
- .download(url, http_adapter: nil) ⇒ Object
- .download_to_file(url, path, http_adapter: nil) ⇒ Object
- .log_remote(endpoint, body, error = false) ⇒ Object
Class Method Details
.download(url, http_adapter: nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rager/utils/http.rb', line 22 def self.download(url, http_adapter: nil) adapter = http_adapter || Rager.config.http_adapter response = adapter.make_request( Rager::Http::Request.new(url: url, headers: {}) ) return nil unless response.success? body = response.body return nil if body.nil? if body.is_a?(String) body elsif body.respond_to?(:each) body.to_a.join else body.to_s end end |
.download_to_file(url, path, http_adapter: nil) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/rager/utils/http.rb', line 48 def self.download_to_file(url, path, http_adapter: nil) content = download(url, http_adapter: http_adapter) return nil if content.nil? FileUtils.mkdir_p(File.dirname(path)) File.write(path, content, mode: "wb") path end |
.log_remote(endpoint, body, error = false) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rager/utils/http.rb', line 64 def self.log_remote(endpoint, body, error = false) logger = Rager.config.logger case Rager.config.log_strategy when Rager::LogStrategy::Stdout error ? logger.error(body) : logger.info(body) when Rager::LogStrategy::Remote http_adapter = Rager.config.http_adapter url = Rager.config.url api_key = Rager.config.api_key unless url && api_key raise Rager::Errors::CredentialsError.new("Rager Cloud", details: "Missing url or api_key for remote logging") end headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{api_key}" } request = Rager::Http::Request.new( url: "#{url}/#{endpoint}", verb: Rager::Http::Verb::Post, headers: headers, body: body ) begin response = http_adapter.make_request(request) unless response.success? raise Rager::Errors::HttpError.new( http_adapter, request.url, response.status, body: T.cast(response.body, String), details: "HTTP request failed during remote logging" ) end rescue Rager::Error => e if Rager.config.log_raise raise e else Rager.config.logger.error(e.) end end end end |